'use strict'; const TABLE_NAME = 'wms_order_express'; /** * 获取指定表的索引名集合。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {string} tableName 表名。 * @return {Promise>} 索引名集合。 */ const getIndexNameSet = async (queryInterface, tableName) => { const indexes = await queryInterface.showIndex(tableName); return new Set(indexes.map(index => String(index && index.name ? index.name : '').trim()).filter(Boolean)); }; module.exports = { /** * 执行迁移:为运单表新增运单列表高频查询索引(幂等)。 * * 新增索引: * - idx_warehouse_id_status:仓库+状态筛选(最常见查询组合) * - idx_logistics_code:物流公司筛选 * - idx_logistics_interface_id:快递接口ID筛选 * - idx_warehouse_id_create_at:仓库+时间范围排序分页 * * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {import('sequelize')} Sequelize Sequelize 实例。 * @return {Promise} 无返回值。 */ async up(queryInterface, Sequelize) { const indexNameSet = await getIndexNameSet(queryInterface, TABLE_NAME); if (!indexNameSet.has('idx_warehouse_id_status')) { await queryInterface.addIndex(TABLE_NAME, { name: 'idx_warehouse_id_status', fields: [ 'warehouse_id', 'status' ], }); } if (!indexNameSet.has('idx_logistics_code')) { await queryInterface.addIndex(TABLE_NAME, { name: 'idx_logistics_code', fields: [ 'logistics_code' ], }); } if (!indexNameSet.has('idx_logistics_interface_id')) { await queryInterface.addIndex(TABLE_NAME, { name: 'idx_logistics_interface_id', fields: [ 'logistics_interface_id' ], }); } if (!indexNameSet.has('idx_warehouse_id_create_at')) { await queryInterface.addIndex(TABLE_NAME, { name: 'idx_warehouse_id_create_at', fields: [ 'warehouse_id', 'create_at' ], }); } }, /** * 回滚迁移:移除运单列表查询索引。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {import('sequelize')} Sequelize Sequelize 实例。 * @return {Promise} 无返回值。 */ async down(queryInterface, Sequelize) { const indexNameSet = await getIndexNameSet(queryInterface, TABLE_NAME); if (indexNameSet.has('idx_warehouse_id_status')) { await queryInterface.removeIndex(TABLE_NAME, 'idx_warehouse_id_status'); } if (indexNameSet.has('idx_logistics_code')) { await queryInterface.removeIndex(TABLE_NAME, 'idx_logistics_code'); } if (indexNameSet.has('idx_logistics_interface_id')) { await queryInterface.removeIndex(TABLE_NAME, 'idx_logistics_interface_id'); } if (indexNameSet.has('idx_warehouse_id_create_at')) { await queryInterface.removeIndex(TABLE_NAME, 'idx_warehouse_id_create_at'); } }, };