Dealer_Onboarding_Backend/scripts/migrate.js

41 lines
1.3 KiB
JavaScript

/**
* Database Migration Script
* Synchronizes all Sequelize models with the database
* This script will DROP all existing tables and recreate them.
*
* Run: node scripts/migrate.js
*/
require('dotenv').config();
const db = require('../src/database/models');
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('----------------------------------------------------');
console.log(`Available Models: ${Object.keys(db).filter(k => k !== 'sequelize' && k !== 'Sequelize').join(', ')}`);
console.log('----------------------------------------------------');
process.exit(0);
} catch (error) {
console.error('\n❌ Migration failed:', error.message);
if (error.stack) {
console.error('\nStack Trace:\n', error.stack);
}
process.exit(1);
}
}
runMigrations();