Dealer_Onboarding_Backend/scripts/migrate.ts

42 lines
1.5 KiB
TypeScript

/**
* Database Migration Script
* Synchronizes all Sequelize models with the database
* This script will DROP all existing tables and recreate them.
*
* Run: npx tsx scripts/migrate.ts
*/
import 'dotenv/config';
import db from '../src/database/models/index.js';
async function runMigrations() {
console.log('🔄 Starting database synchronization (Fresh Startup)...\n');
console.log('⚠️ WARNING: This will drop all existing tables in the database.\n');
try {
// Authenticate with the database
await db.sequelize.authenticate();
console.log('📡 Connected to the database successfully.');
// Synchronize models (force: true drops existing tables)
// This ensures that the schema exactly matches the Sequelize models
await db.sequelize.sync({ force: true });
console.log('\n✅ All tables created and synchronized successfully!');
console.log('----------------------------------------------------');
const modelNames = Object.keys(db).filter(k => k !== 'sequelize' && k !== 'Sequelize');
console.log(`Available Models (${modelNames.length}): ${modelNames.join(', ')}`);
console.log('----------------------------------------------------');
process.exit(0);
} catch (error: any) {
console.error('\n❌ Migration failed:', error.message);
if (error.stack) {
console.error('\nStack Trace:\n', error.stack);
}
process.exit(1);
}
}
runMigrations();