WMS_MAIN/database/migrations/20260820120000-alter-wms-wave-batch-item-detail-machine-to-text.js
liufumingren 90d5701a53 feat(wave): item_detail_machine 改 TEXT 并新增波次自动终结
- wms_wave_batch.item_detail_machine VARCHAR(1024) -> TEXT, 兼容多商品长结构标识
- 新增 wave_batch_closure 服务: 扫描活动波次(0/1)中订单已全部离开待处理表的波次并收敛 wave_status=5
- 新增 sync_finished_wave_batch 定时任务与执行日志缓存
2026-08-21 02:20:17 +08:00

121 lines
4.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 将 wms_wave_batch.item_detail_machine 列从 VARCHAR(1024) 改为 TEXT。
*
* 背景:
* buildMachineStructureToken 生成的结构标识为
* `v1|item_part_type|item_part|distinguish_express|logistics_name|item_snapshot_key`,
* 其中 item_part 与 item_snapshot_key 均经 encodeURIComponent 编码,
* 多商品订单下编码后的长度可能远超 1024,抓取分组 INSERT 时报
* "Data too long for column 'item_detail_machine' at row 1"。
*
* 变更:
* 1. wms_wave_batch.item_detail_machine: VARCHAR(1024) → TEXT
* 2. 索引 idx_wave_item_machine_prefix 维持前缀索引
* (item_detail_machine(191)),TEXT 列前缀索引仍合法;
* 若存量环境存在全列索引则先删除。
* 3. 模型定义同步改为 TEXT(app/model/wave_batch.js)。
*/
const TABLE_NAME = 'wms_wave_batch';
const COLUMN_NAME = 'item_detail_machine';
const INDEX_NAME = 'idx_wave_item_machine_prefix';
async function hasColumn(queryInterface, tableName, columnName) {
const [ columns ] = await queryInterface.sequelize.query(
`
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = :tableName
AND COLUMN_NAME = :columnName
`,
{ replacements: { tableName, columnName } }
);
return (Array.isArray(columns) ? columns : []).length > 0;
}
async function listColumnIndexes(queryInterface, tableName, columnName) {
const [ rows ] = await queryInterface.sequelize.query(
`
SELECT DISTINCT INDEX_NAME, SUB_PART
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = :tableName
AND COLUMN_NAME = :columnName
`,
{ replacements: { tableName, columnName } }
);
return Array.isArray(rows) ? rows : [];
}
module.exports = {
up: async queryInterface => {
if (!(await hasColumn(queryInterface, TABLE_NAME, COLUMN_NAME))) {
console.log(`[migrate] ${TABLE_NAME}.${COLUMN_NAME} 列不存在,跳过`);
return;
}
// ── 1. 全列索引对 TEXT 无效(无前缀),先删除存量全列索引 ──
const existingIndexes = await listColumnIndexes(queryInterface, TABLE_NAME, COLUMN_NAME);
for (const idx of existingIndexes) {
const indexName = String(idx.INDEX_NAME || '').trim();
const subPart = idx.SUB_PART === null || idx.SUB_PART === undefined
? null
: Number(idx.SUB_PART);
if (!indexName || subPart) {
continue;
}
try {
await queryInterface.removeIndex(TABLE_NAME, indexName);
console.log(`[migrate] 已移除全列索引 ${indexName}(TEXT 列要求前缀索引)`);
} catch (e) {
const msg = String(e && e.message || '');
if (!msg.includes('check that it exists') && !msg.includes('exist')) {
console.warn(`[migrate] 移除全列索引 ${indexName} 时出现非预期错误: ${msg}`);
}
}
}
// ── 2. 修改列类型为 TEXT ──
await queryInterface.sequelize.query(
`ALTER TABLE \`${TABLE_NAME}\` MODIFY \`${COLUMN_NAME}\` TEXT NULL COMMENT '抓取结构token:v1|item_part_type|item_part|distinguish_express|logistics_name|item_snapshot_key'`
);
console.log(`[migrate] ${TABLE_NAME}.${COLUMN_NAME} VARCHAR(1024) → TEXT`);
// ── 3. 确保前缀索引存在 ──
const remainingIndexes = await listColumnIndexes(queryInterface, TABLE_NAME, COLUMN_NAME);
if (remainingIndexes.length === 0) {
try {
await queryInterface.sequelize.query(
`ALTER TABLE \`${TABLE_NAME}\` ADD INDEX \`${INDEX_NAME}\` (\`${COLUMN_NAME}\`(191))`
);
console.log(`[migrate] 已创建前缀索引 ${INDEX_NAME}(${COLUMN_NAME}(191))`);
} catch (e) {
const msg = String(e && e.message || '');
if (!msg.includes('Duplicate')) {
throw e;
}
console.log(`[migrate] 前缀索引 ${INDEX_NAME} 已存在,跳过`);
}
}
},
down: async queryInterface => {
if (!(await hasColumn(queryInterface, TABLE_NAME, COLUMN_NAME))) {
console.log(`[migrate:down] ${TABLE_NAME}.${COLUMN_NAME} 列不存在,跳过`);
return;
}
// 注意:若已有数据超过 1024 字符,回滚会失败,需先人工截断数据
await queryInterface.sequelize.query(
`ALTER TABLE \`${TABLE_NAME}\` MODIFY \`${COLUMN_NAME}\` VARCHAR(1024) NULL COMMENT '抓取结构token:v1|item_part_type|item_part|distinguish_express|logistics_name|item_snapshot_key'`
);
console.log(`[migrate:down] ${TABLE_NAME}.${COLUMN_NAME} TEXT → VARCHAR(1024)`);
},
};