feat(db): 为 wms_order_express 补 idx_warehouse_store 索引迁移

生产库已手工创建 idx_warehouse_store (store_id, warehouse_id),
本次将其固化为幂等迁移(ALGORITHM=INPLACE, LOCK=NONE),
并在模型索引声明与运单模块设计文档中同步记录。

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-11-09 23:47:35 +08:00
parent eacf54091d
commit 083f2846e0
3 changed files with 105 additions and 0 deletions

View File

@ -177,6 +177,10 @@ module.exports = app => {
name: 'idx_wms_order_express_package_material_id',
fields: [ 'package_material_id' ],
},
{
name: 'idx_warehouse_store',
fields: [ 'store_id', 'warehouse_id' ],
},
],
});

View File

@ -64,6 +64,7 @@
| `idx_waybill_code` | `waybill_code` | 普通 |
| `idx_create_at` | `create_at` | 普通 |
| `idx_wms_order_express_package_material_id` | `package_material_id` | 普通 |
| `idx_warehouse_store` | `store_id, warehouse_id` | 普通(货主+仓库组合过滤,迁移 `20261109235000`) |
### 2.2 关联表
@ -556,6 +557,7 @@ POST /system { method: "waybill.warehouse.list", ... }
| **修改** | `methods/system.json` | 新增 `waybill.warehouse` group,注册 3 个 method 定义 |
| **新增** | `database/migrations/20260729140000-add-waybill-list-indexes.js` | 迁移文件:新增 4 个索引(✅ 已执行) |
| **新增** | `database/migrations/20260730000000-add-store-id-to-wms-order-express.js` | 迁移文件:新增 `store_id` 货主ID列(✅ 已执行) |
| **新增** | `database/migrations/20261109235000-add-warehouse-store-index-to-wms-order-express.js` | 迁移文件:新增 `idx_warehouse_store (store_id, warehouse_id)` 索引(生产已手工创建,待按流程执行记录) |
---

View File

@ -0,0 +1,99 @@
'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<boolean>} 是否存在。
*/
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<boolean>} 是否存在。
*/
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<void>} 无返回值。
*/
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<void>} 无返回值。
*/
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`
);
},
};