111 lines
3.0 KiB
JavaScript
111 lines
3.0 KiB
JavaScript
|
|
const { testConnection, syncDatabase, dropDatabase } = require('../config/database');
|
||
|
|
const logger = require('./logger');
|
||
|
|
|
||
|
|
const initializeDatabase = async (force = false) => {
|
||
|
|
try {
|
||
|
|
console.log('🚀 Initializing database...');
|
||
|
|
|
||
|
|
// Test connection
|
||
|
|
console.log('📡 Testing database connection...');
|
||
|
|
const connected = await testConnection();
|
||
|
|
if (!connected) {
|
||
|
|
throw new Error('Database connection failed');
|
||
|
|
}
|
||
|
|
console.log('✅ Database connection successful');
|
||
|
|
|
||
|
|
// Sync database (create tables)
|
||
|
|
console.log('🔧 Synchronizing database schema...');
|
||
|
|
const synced = await syncDatabase(force);
|
||
|
|
if (!synced) {
|
||
|
|
throw new Error('Database synchronization failed');
|
||
|
|
}
|
||
|
|
console.log('✅ Database schema synchronized');
|
||
|
|
|
||
|
|
console.log('🎉 Database initialization completed successfully!');
|
||
|
|
return true;
|
||
|
|
} catch (error) {
|
||
|
|
console.error('❌ Database initialization failed:', error.message);
|
||
|
|
logger.error('Database initialization error:', error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const resetDatabase = async () => {
|
||
|
|
try {
|
||
|
|
console.log('🔄 Resetting database...');
|
||
|
|
|
||
|
|
// Drop all tables
|
||
|
|
console.log('🗑️ Dropping all tables...');
|
||
|
|
const dropped = await dropDatabase();
|
||
|
|
if (!dropped) {
|
||
|
|
throw new Error('Database drop failed');
|
||
|
|
}
|
||
|
|
console.log('✅ All tables dropped');
|
||
|
|
|
||
|
|
// Reinitialize
|
||
|
|
console.log('🔧 Reinitializing database...');
|
||
|
|
const initialized = await initializeDatabase(true);
|
||
|
|
if (!initialized) {
|
||
|
|
throw new Error('Database reinitialization failed');
|
||
|
|
}
|
||
|
|
console.log('✅ Database reset completed');
|
||
|
|
|
||
|
|
return true;
|
||
|
|
} catch (error) {
|
||
|
|
console.error('❌ Database reset failed:', error.message);
|
||
|
|
logger.error('Database reset error:', error);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const checkDatabaseStatus = async () => {
|
||
|
|
try {
|
||
|
|
console.log('🔍 Checking database status...');
|
||
|
|
|
||
|
|
const connected = await testConnection();
|
||
|
|
if (!connected) {
|
||
|
|
console.log('❌ Database connection failed');
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('✅ Database is connected and ready');
|
||
|
|
return true;
|
||
|
|
} catch (error) {
|
||
|
|
console.error('❌ Database status check failed:', error.message);
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// Run initialization if this file is executed directly
|
||
|
|
if (require.main === module) {
|
||
|
|
const args = process.argv.slice(2);
|
||
|
|
const command = args[0] || 'init';
|
||
|
|
|
||
|
|
switch (command) {
|
||
|
|
case 'init':
|
||
|
|
initializeDatabase().then(success => {
|
||
|
|
process.exit(success ? 0 : 1);
|
||
|
|
});
|
||
|
|
break;
|
||
|
|
case 'reset':
|
||
|
|
resetDatabase().then(success => {
|
||
|
|
process.exit(success ? 0 : 1);
|
||
|
|
});
|
||
|
|
break;
|
||
|
|
case 'status':
|
||
|
|
checkDatabaseStatus().then(success => {
|
||
|
|
process.exit(success ? 0 : 1);
|
||
|
|
});
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
console.log('Usage: node databaseInit.js [init|reset|status]');
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = {
|
||
|
|
initializeDatabase,
|
||
|
|
resetDatabase,
|
||
|
|
checkDatabaseStatus
|
||
|
|
};
|