'use strict'; const TABLE_NAME = 'wms_express_setting'; const UNIQUE_INDEX_NAME = 'platform_code'; const NEW_INDEX_NAME = 'idx_express_warehouse'; const FK_NAME = 'wms_express_setting_ibfk_1'; /** * 检查指定表中是否存在指定名称的索引(通过 SHOW INDEX 做幂等判断)。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} tableName 表名。 * @param {string} indexName 索引名。 * @returns {Promise} 索引是否存在。 */ const hasIndex = async (queryInterface, tableName, indexName) => { const [rows] = await queryInterface.sequelize.query( `SHOW INDEX FROM \`${tableName}\` WHERE Key_name = :indexName`, { replacements: { indexName } } ); return rows && rows.length > 0; }; /** * 检查指定外键约束是否存在(通过 information_schema 做幂等判断)。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} tableName 表名。 * @param {string} constraintName 约束名。 * @returns {Promise} 外键是否存在。 */ const hasForeignKey = async (queryInterface, tableName, constraintName) => { const [rows] = await queryInterface.sequelize.query( `SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND TABLE_NAME = :tableName AND CONSTRAINT_NAME = :constraintName AND CONSTRAINT_TYPE = 'FOREIGN KEY'`, { replacements: { tableName, constraintName } } ); return rows && rows.length > 0; }; module.exports = { /** * 正向迁移:先删除外键依赖,再删除唯一索引,最后添加普通索引替代。 * 原因:同一系统快递编码(express_code)允许对应多个自定义编码(code), * 原 UNIQUE KEY `platform_code` ON (`express_code`, `warehouse_id`) 约束过紧, * 且该索引被外键 `wms_express_setting_ibfk_1`(express_code → wms_express.code)依赖。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @returns {Promise} 无返回值。 */ async up(queryInterface) { // 1. 先删除依赖该索引的外键约束 if (await hasForeignKey(queryInterface, TABLE_NAME, FK_NAME)) { await queryInterface.sequelize.query( `ALTER TABLE \`${TABLE_NAME}\` DROP FOREIGN KEY \`${FK_NAME}\`;` ); } // 2. 删除原来的唯一索引 if (await hasIndex(queryInterface, TABLE_NAME, UNIQUE_INDEX_NAME)) { await queryInterface.sequelize.query( `ALTER TABLE \`${TABLE_NAME}\` DROP INDEX \`${UNIQUE_INDEX_NAME}\`;` ); } // 3. 添加普通索引替代(用于查询性能) if (!(await hasIndex(queryInterface, TABLE_NAME, NEW_INDEX_NAME))) { await queryInterface.sequelize.query( `ALTER TABLE \`${TABLE_NAME}\` ADD INDEX \`${NEW_INDEX_NAME}\` (express_code, warehouse_id);` ); } }, /** * 回滚迁移:恢复唯一索引与外键约束。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @returns {Promise} 无返回值。 */ async down(queryInterface) { // 1. 删除替代的普通索引 if (await hasIndex(queryInterface, TABLE_NAME, NEW_INDEX_NAME)) { await queryInterface.sequelize.query( `ALTER TABLE \`${TABLE_NAME}\` DROP INDEX \`${NEW_INDEX_NAME}\`;` ); } // 2. 恢复唯一索引(幂等:已存在则跳过) if (!(await hasIndex(queryInterface, TABLE_NAME, UNIQUE_INDEX_NAME))) { await queryInterface.sequelize.query( `ALTER TABLE \`${TABLE_NAME}\` ADD UNIQUE INDEX \`${UNIQUE_INDEX_NAME}\` (express_code, warehouse_id);` ); } // 3. 恢复外键约束(幂等:已存在则跳过) if (!(await hasForeignKey(queryInterface, TABLE_NAME, FK_NAME))) { await queryInterface.sequelize.query( `ALTER TABLE \`${TABLE_NAME}\` ADD CONSTRAINT \`${FK_NAME}\` FOREIGN KEY (express_code) REFERENCES wms_express (code);` ); } }, };