update
This commit is contained in:
40
sync-database.js
Normal file
40
sync-database.js
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Sync all models to database (create tables)
|
||||
* Run once to initialize database schema
|
||||
*/
|
||||
const { sequelize } = require('./config/database');
|
||||
const { setupRelationships } = require('./models');
|
||||
|
||||
async function syncDatabase() {
|
||||
try {
|
||||
console.log('🔄 Starting database synchronization...');
|
||||
|
||||
// Test connection first
|
||||
await sequelize.authenticate();
|
||||
console.log('✅ Database connection OK');
|
||||
|
||||
// Setup relationships
|
||||
setupRelationships();
|
||||
console.log('✅ Model relationships configured');
|
||||
|
||||
// Sync all models (creates tables if not exist)
|
||||
await sequelize.sync({ alter: false, force: false });
|
||||
console.log('✅ All models synced successfully');
|
||||
|
||||
// Show created tables
|
||||
const [tables] = await sequelize.query('SHOW TABLES');
|
||||
console.log(`\n📊 Total tables: ${tables.length}`);
|
||||
tables.forEach((table, index) => {
|
||||
console.log(` ${index + 1}. ${Object.values(table)[0]}`);
|
||||
});
|
||||
|
||||
console.log('\n✅ Database schema initialization complete!');
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error('❌ Error syncing database:', error.message);
|
||||
console.error(error.stack);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
syncDatabase();
|
||||
Reference in New Issue
Block a user