const { Sequelize, DataTypes } = require('sequelize'); const fs = require('fs'); const path = require('path'); const DB_CONFIG = { host: '127.0.0.1', port: 3307, username: 'wms', password: 'dspxmRwRdRbWtbm7', database: 'wms', dialect: 'mysql', logging: false, }; const TARGET_MIGRATION = '20260525113000-add-request-platform-code-to-wms-print-platform.js'; const EARLIER_PENDING = '20260524153000-switch-shop-platform-fk-to-print-platform.js'; async function run() { const sequelize = new Sequelize(DB_CONFIG.database, DB_CONFIG.username, DB_CONFIG.password, { host: DB_CONFIG.host, port: DB_CONFIG.port, dialect: DB_CONFIG.dialect, logging: DB_CONFIG.logging, }); const queryInterface = sequelize.getQueryInterface(); try { const migrationsDir = path.join(__dirname, 'database', 'migrations'); // 1. Manually apply EARLIER_PENDING const results = await sequelize.query('SELECT name FROM SequelizeMeta', { type: sequelize.QueryTypes.SELECT }); const appliedResults = Array.isArray(results) ? results : []; const appliedSet = new Set(appliedResults.map(r => r.name)); if (!appliedSet.has(EARLIER_PENDING)) { console.log(`Applying earlier pending: ${EARLIER_PENDING}`); const migrationPath = path.join(migrationsDir, EARLIER_PENDING); const migration = require(migrationPath); await migration.up(queryInterface, Sequelize); await sequelize.query('INSERT INTO SequelizeMeta (name) VALUES (?)', { replacements: [ EARLIER_PENDING ], type: sequelize.QueryTypes.INSERT, }); } // Now proceed with normal logic for TARGET_MIGRATION const allFiles = fs.readdirSync(migrationsDir).filter(f => f.endsWith('.js')).sort(); const results2 = await sequelize.query('SELECT name FROM SequelizeMeta', { type: sequelize.QueryTypes.SELECT }); const appliedResults2 = Array.isArray(results2) ? results2 : []; const appliedSet2 = new Set(appliedResults2.map(r => r.name)); const targetIndex = allFiles.indexOf(TARGET_MIGRATION); const earlierPending = allFiles.slice(0, targetIndex).filter(f => !appliedSet2.has(f)); const beforeResult = { connection: { database: DB_CONFIG.database, host: DB_CONFIG.host, port: DB_CONFIG.port }, meta: appliedResults2.map(r => r.name), earlierPending, }; const [ columnsBefore ] = await sequelize.query("SHOW COLUMNS FROM `wms_print_platform` LIKE 'request_platform_code'"); beforeResult.columnCheck = columnsBefore; console.log('--- BEFORE ---'); console.log(JSON.stringify(beforeResult, null, 2)); if (earlierPending.length > 0) { console.log('STOPPING: Earlier pending migrations still found.'); return; } if (!appliedSet2.has(TARGET_MIGRATION)) { console.log(`Applying target migration: ${TARGET_MIGRATION}`); const migrationPath = path.join(migrationsDir, TARGET_MIGRATION); const migration = require(migrationPath); await migration.up(queryInterface, Sequelize); await sequelize.query('INSERT INTO SequelizeMeta (name) VALUES (?)', { replacements: [ TARGET_MIGRATION ], type: sequelize.QueryTypes.INSERT, }); } const finalResults = await sequelize.query('SELECT name FROM SequelizeMeta', { type: sequelize.QueryTypes.SELECT }); const appliedFinal = Array.isArray(finalResults) ? finalResults : []; const [ columnsAfter ] = await sequelize.query("SHOW COLUMNS FROM `wms_print_platform` LIKE 'request_platform_code'"); const afterResult = { connection: { database: DB_CONFIG.database, host: DB_CONFIG.host, port: DB_CONFIG.port }, meta: appliedFinal.map(r => r.name), columnCheck: columnsAfter, earlierPending: [], }; console.log('--- AFTER ---'); console.log(JSON.stringify(afterResult, null, 2)); } catch (error) { console.error('Error:', error); } finally { await sequelize.close(); } } run();