Files
multy-stub/server/routers/procurement/scripts/run-migrations.js
2025-10-27 19:37:21 +03:00

106 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const mongoose = require('mongoose');
const { migrateCompanies } = require('./migrate-companies');
const { migrateMessages } = require('./migrate-messages');
const { recreateTestUser } = require('./recreate-test-user');
require('dotenv').config();
const mongoUrl = process.env.MONGODB_URI || 'mongodb://localhost:27017/procurement_db';
// Migration history model
const migrationSchema = new mongoose.Schema({
name: { type: String, unique: true, required: true },
executedAt: { type: Date, default: Date.now },
status: { type: String, enum: ['completed', 'failed'], default: 'completed' },
message: String
}, { collection: 'migrations' });
const Migration = mongoose.model('Migration', migrationSchema);
const migrations = [
{ name: 'migrate-companies', fn: migrateCompanies },
{ name: 'migrate-messages', fn: migrateMessages },
{ name: 'recreate-test-user', fn: recreateTestUser }
];
async function runMigrations() {
let mongooseConnected = false;
try {
console.log('\n' + '='.repeat(60));
console.log('🚀 Starting Database Migrations');
console.log('='.repeat(60) + '\n');
console.log('[Migrations] Connecting to MongoDB...');
await mongoose.connect(mongoUrl, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000,
connectTimeoutMS: 5000,
});
mongooseConnected = true;
console.log('[Migrations] ✅ Connected to MongoDB\n');
for (const migration of migrations) {
console.log(`[${migration.name}] Starting...`);
try {
// Check if already executed
const existing = await Migration.findOne({ name: migration.name });
if (existing) {
console.log(`[${migration.name}] Already executed at: ${existing.executedAt.toISOString()}`);
console.log(`[${migration.name}] Status: ${existing.status}`);
if (existing.message) console.log(`[${migration.name}] Message: ${existing.message}`);
console.log('');
continue;
}
// Run migration
await migration.fn();
// Record successful migration
await Migration.create({
name: migration.name,
status: 'completed',
message: `${migration.name} executed successfully`
});
console.log(`[${migration.name}] ✅ Completed and recorded\n`);
} catch (error) {
console.error(`[${migration.name}] ❌ Error: ${error.message}\n`);
// Record failed migration
await Migration.create({
name: migration.name,
status: 'failed',
message: error.message
});
}
}
console.log('='.repeat(60));
console.log('✅ All migrations processed');
console.log('='.repeat(60) + '\n');
} catch (error) {
console.error('\n❌ Fatal migration error:', error.message);
console.error(error);
process.exit(1);
} finally {
if (mongooseConnected) {
await mongoose.connection.close();
console.log('[Migrations] Disconnected from MongoDB\n');
}
}
}
module.exports = { runMigrations, Migration };
// Run directly if called as script
if (require.main === module) {
runMigrations().catch(err => {
console.error('Migration failed:', err);
process.exit(1);
});
}