'use strict'; /** * 为运单表 wms_order_express 增加 idx_warehouse_store(store_id, warehouse_id)索引。 * * 背景: * - 生产库已手工创建该索引(SHOW INDEX 可见 `idx_warehouse_store`,Non_unique=1, * 列序 store_id → warehouse_id),用于支撑运单相关的「货主 + 仓库」组合过滤查询 * (运单列表/详情的货主隔离、发货单与运单的关联校验等),避免只能走 * idx_warehouse_id_status 或 idx_warehouse_id_create_at 的 warehouse_id 前导列, * 再逐行回表过滤 store_id。 * - 手工变更未纳入版本管理,其他环境(开发/测试)缺失该索引,会造成同一条查询在不同 * 环境执行计划不一致;本迁移把生产变更固化为迁移脚本,保证各环境结构一致。 * - 索引名沿用生产名称 idx_warehouse_store(与仓库内 idx_warehouse_store 命名习惯一致), * 不按 idx_{表名}_ 前缀重命名,否则生产会因名称不同再建一份功能重复的索引。 * * 执行方式:ALTER TABLE ... ADD INDEX ..., ALGORITHM=INPLACE, LOCK=NONE * (MySQL 5.6+ 在线加二级索引,不重建表、不锁写)。 * 幂等:索引已存在时跳过;生产环境执行本迁移预期输出 skip/exists。 * * ⚠️ 本文件未对任何环境执行,仅由迁移流程(npm run migration:single <文件名>)在确认后落库。 */ const TABLE_NAME = 'wms_order_express'; const INDEX_NAME = 'idx_warehouse_store'; const INDEX_FIELDS = [ 'store_id', 'warehouse_id' ]; /** * 判断表是否存在指定列。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} tableName 表名。 * @param {string} columnName 列名。 * @return {Promise} 是否存在。 */ async function columnExists(queryInterface, tableName, columnName) { const [ rows ] = await queryInterface.sequelize.query( 'SELECT COLUMN_NAME AS columnName FROM INFORMATION_SCHEMA.COLUMNS ' + 'WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?', { replacements: [ tableName, columnName ] } ); return (rows || []).length > 0; } /** * 判断索引是否已存在。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} tableName 表名。 * @param {string} indexName 索引名。 * @return {Promise} 是否存在。 */ async function indexExists(queryInterface, tableName, indexName) { const indexes = await queryInterface.showIndex(tableName); return (indexes || []).some(index => { const name = String(index && (index.name || index.Key_name || index.key_name) || ''); return name.toLowerCase() === String(indexName).toLowerCase(); }); } module.exports = { /** * 执行迁移:为 wms_order_express 增加 idx_warehouse_store 索引。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ async up(queryInterface) { for (const columnName of INDEX_FIELDS) { if (!(await columnExists(queryInterface, TABLE_NAME, columnName))) { console.log(`[${INDEX_NAME}] skip: ${TABLE_NAME}.${columnName} 列不存在`); return; } } if (await indexExists(queryInterface, TABLE_NAME, INDEX_NAME)) { console.log(`[${INDEX_NAME}] skip: ${TABLE_NAME} 已存在该索引`); return; } const fieldSql = INDEX_FIELDS.map(field => `\`${field}\``).join(', '); await queryInterface.sequelize.query( `ALTER TABLE \`${TABLE_NAME}\` ADD INDEX \`${INDEX_NAME}\` (${fieldSql}), ALGORITHM=INPLACE, LOCK=NONE` ); console.log(`[${INDEX_NAME}] added on ${TABLE_NAME} (${INDEX_FIELDS.join(', ')})`); }, /** * 回滚迁移:移除 idx_warehouse_store 索引。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ async down(queryInterface) { if (!(await indexExists(queryInterface, TABLE_NAME, INDEX_NAME))) { return; } await queryInterface.sequelize.query( `ALTER TABLE \`${TABLE_NAME}\` DROP INDEX \`${INDEX_NAME}\`, ALGORITHM=INPLACE, LOCK=NONE` ); }, };