Files
Nas-Notification/scripts/run-migration.js

71 lines
2.4 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const mysql = require('mysql2/promise');
// Database configuration - you should update these with your actual database credentials
const dbConfig = {
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 3306,
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || '',
database: process.env.DB_NAME || 'corrad_af_2024',
multipleStatements: true
};
async function runMigration() {
let connection;
try {
console.log('🔄 Connecting to database...');
connection = await mysql.createConnection(dbConfig);
console.log('✅ Connected to database successfully');
// Read the migration file
const migrationPath = path.join(__dirname, '..', 'database', 'migrations', '004_create_notification_logs_table.sql');
const migrationSQL = fs.readFileSync(migrationPath, 'utf8');
console.log('📄 Running migration: 004_create_notification_logs_table.sql');
// Execute the migration
const [results] = await connection.execute(migrationSQL);
console.log('✅ Migration completed successfully');
console.log('🎉 Notification logs table created');
// Optional: Check if the table was created
const [tables] = await connection.execute('SHOW TABLES LIKE "notification_logs"');
if (tables.length > 0) {
console.log('✅ Table "notification_logs" confirmed to exist');
// Show table structure
const [columns] = await connection.execute('DESCRIBE notification_logs');
console.log('📋 Table structure:');
columns.forEach(col => {
console.log(` - ${col.Field}: ${col.Type} ${col.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${col.Key ? `(${col.Key})` : ''}`);
});
} else {
console.log('❌ Table "notification_logs" was not found');
}
} catch (error) {
console.error('❌ Migration failed:', error.message);
console.error('Stack trace:', error.stack);
process.exit(1);
} finally {
if (connection) {
await connection.end();
console.log('🔌 Database connection closed');
}
}
}
// Run the migration
console.log('🚀 Starting database migration for notification logs...');
runMigration().then(() => {
console.log('✅ Migration process completed successfully');
}).catch(error => {
console.error('❌ Migration process failed:', error);
process.exit(1);
});