Dealer_Onboarding_Backend/scripts/create-system-audit-log-table.ts

41 lines
1.2 KiB
TypeScript

/**
* Create System Audit Log Table
*
* Bootstraps the new `system_audit_logs` table on environments where the
* full `migrate.ts` (sequelize.sync({ force: true })) cannot be run because
* the database already holds production / shared data.
*
* Safe to re-run: uses `SystemAuditLog.sync()` (no `force`, no `alter`),
* which is a no-op once the table exists.
*
* Run: npx tsx scripts/create-system-audit-log-table.ts
*/
import 'dotenv/config';
import db from '../src/database/models/index.js';
async function run() {
console.log('🔄 Ensuring system_audit_logs table exists...');
try {
await db.sequelize.authenticate();
console.log('📡 Database connection OK');
await db.SystemAuditLog.sync();
const [rows] = await db.sequelize.query(
`SELECT COUNT(*)::int AS total FROM system_audit_logs`
);
const total = (rows as any[])[0]?.total ?? 0;
console.log('✅ system_audit_logs is ready');
console.log(` Existing rows: ${total}`);
process.exit(0);
} catch (err: any) {
console.error('❌ Failed to ensure system_audit_logs table:', err.message || err);
if (err.stack) console.error(err.stack);
process.exit(1);
}
}
run();