forked from UKSOURCE/cms.hailearning.edu.vn
121 lines
3.5 KiB
JavaScript
121 lines
3.5 KiB
JavaScript
require('dotenv').config();
|
|
const connectDB = require('../config/database');
|
|
const migrationHelper = require('../utils/migrationHelper');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
/**
|
|
* Tự động phát hiện tất cả các file script trong thư mục scripts
|
|
* Loại trừ các file quản lý migration và file không phải .js
|
|
*/
|
|
function discoverMigrations() {
|
|
const scriptsDir = __dirname;
|
|
const files = fs.readdirSync(scriptsDir);
|
|
|
|
// Danh sách các file quản lý migration cần loại trừ
|
|
const excludeFiles = [
|
|
'migrate-all.js',
|
|
'migrate-status.js',
|
|
'migrate-rollback.js',
|
|
'migrate-fresh.js',
|
|
'make-migration.js',
|
|
'MIGRATION_README.md'
|
|
];
|
|
|
|
const migrations = files
|
|
.filter(file => {
|
|
// Lấy tất cả file .js, trừ các file quản lý
|
|
return file.endsWith('.js') && !excludeFiles.includes(file);
|
|
})
|
|
.map(file => file.replace('.js', ''))
|
|
.sort();
|
|
|
|
return migrations;
|
|
}
|
|
|
|
// Tự động phát hiện migrations
|
|
const availableMigrations = discoverMigrations();
|
|
|
|
/**
|
|
* Hiển thị trạng thái của tất cả migrations
|
|
*/
|
|
async function showStatus() {
|
|
const mongoose = require('mongoose');
|
|
let ownConn = false;
|
|
|
|
try {
|
|
const wasConnected = mongoose.connection.readyState === 1;
|
|
await connectDB();
|
|
if (!wasConnected) ownConn = true;
|
|
|
|
console.log('\nMigration Status:\n');
|
|
|
|
const ranMigrations = await migrationHelper.getRanMigrations();
|
|
const ranMap = new Map();
|
|
ranMigrations.forEach(m => ranMap.set(m.name, m));
|
|
|
|
// Tính toán độ rộng cột
|
|
const maxNameLength = Math.max(...availableMigrations.map(name => name.length), 20);
|
|
const statusWidth = 10;
|
|
const batchWidth = 6;
|
|
const ranAtWidth = 20;
|
|
const totalWidth = maxNameLength + statusWidth + batchWidth + ranAtWidth + 11; // 11 = spaces and separators
|
|
|
|
// Header
|
|
console.log('='.repeat(totalWidth));
|
|
console.log(
|
|
`${'Migration Name'.padEnd(maxNameLength)} | ${'Status'.padEnd(statusWidth)} | ${'Batch'.padEnd(batchWidth)} | Ran At`
|
|
);
|
|
console.log('='.repeat(totalWidth));
|
|
|
|
let pendingCount = 0;
|
|
let ranCount = 0;
|
|
|
|
for (const migrationName of availableMigrations) {
|
|
const migration = ranMap.get(migrationName);
|
|
if (migration) {
|
|
const ranAt = new Date(migration.ranAt).toLocaleString('vi-VN');
|
|
console.log(
|
|
`${migrationName.padEnd(maxNameLength)} | ${'Ran'.padEnd(statusWidth)} | ${String(migration.batch).padEnd(batchWidth)} | ${ranAt}`
|
|
);
|
|
ranCount++;
|
|
} else {
|
|
console.log(
|
|
`${migrationName.padEnd(maxNameLength)} | ${'Pending'.padEnd(statusWidth)} | ${'-'.padEnd(batchWidth)} | -`
|
|
);
|
|
pendingCount++;
|
|
}
|
|
}
|
|
|
|
console.log('='.repeat(totalWidth));
|
|
console.log(`\nSummary:`);
|
|
console.log(` Ran: ${ranCount} migration(s)`);
|
|
console.log(` Pending: ${pendingCount} migration(s)`);
|
|
|
|
const lastBatch = await migrationHelper.getLastBatch();
|
|
if (lastBatch > 0) {
|
|
console.log(` Last batch: ${lastBatch}`);
|
|
}
|
|
|
|
console.log('');
|
|
|
|
if (ownConn && mongoose.connection.readyState === 1) {
|
|
await mongoose.disconnect();
|
|
}
|
|
} catch (error) {
|
|
console.error('Error:', error.message);
|
|
if (ownConn && mongoose.connection.readyState === 1) {
|
|
await mongoose.disconnect();
|
|
}
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// Chạy nếu được gọi trực tiếp
|
|
if (require.main === module) {
|
|
showStatus();
|
|
}
|
|
|
|
module.exports = { showStatus };
|
|
|