WMS_MAIN/database/migrations/20260907120000-add-dispath-time-to-order-delivery-dispatch.js
liufumingren c5150bc35b feat: 配送表新增 dispath_time 迁移时间字段
- 新增迁移 20260907120000:为全部配送分表 wms_order_delivery_{wh}_dispatch 幂等新增 dispath_time(datetime, nullable),不触基表/当前分表/history/outsourced

- 配送表模型 schema dispatch 分支同步新增 dispath_time,drift 同步保持一致

- 订单迁移链路 buildDispatchRow 写入 dispath_time=new Date() 记录进入配送表时间
2026-09-07 16:43:35 +08:00

103 lines
3.7 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';
/**
* 配送表增加 dispath_time(迁移进配送表时间)字段。
*
* 背景:出库订单从当前表迁移进配送表时整行复制源数据,
* 缺少订单进入配送域的落库时间点;业务需要直接记录迁移时刻供查询与审计。
*
* 取值约定:DATETIME,可空;迁移链路
* app/service/systems/order_delivery_migration.js 的 buildDispatchRow
* 在写入配送表时设置 new Date(),源表不写该字段。
*
* 覆盖范围(仅配送表独有,不加基表/当前分表/history/outsourced):
* - 全部配送分表 wms_order_delivery_{wh}_dispatch。
*
* 幂等:列已存在则跳过。配送表模型由 app/model/order_delivery_factory.js
* 运行时 drift 同步兜底,本迁移提前补齐避免运行时 ALTER。
*/
const DISPATCH_SHARD_PATTERN = /^wms_order_delivery_(\d+)_dispatch$/;
const COLUMN_NAME = 'dispath_time';
/**
* 列举库内全部配送分表。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<string[]>} 配送分表名列表。
*/
async function listDispatchShards(queryInterface) {
const [ rows ] = await queryInterface.sequelize.query(
'SELECT TABLE_NAME AS tableName FROM INFORMATION_SCHEMA.TABLES '
+ 'WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME LIKE \'wms_order_delivery\\_%\\_dispatch\' '
+ 'ORDER BY TABLE_NAME'
);
return (rows || [])
.map(row => String(row.tableName || row.TABLE_NAME || ''))
.filter(name => DISPATCH_SHARD_PATTERN.test(name));
}
/**
* 判断列是否已存在。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {string} tableName 表名。
* @param {string} columnName 列名。
* @return {Promise<boolean>} 是否存在。
*/
async function columnExists(queryInterface, tableName, columnName) {
const described = await queryInterface.describeTable(tableName);
return Boolean(described && described[columnName]);
}
module.exports = {
/**
* 执行迁移:为全部配送分表增加 dispath_time 列。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 构造器。
* @return {Promise<void>} 无返回值。
*/
async up(queryInterface, Sequelize) {
const tables = await listDispatchShards(queryInterface);
const results = [];
for (const tableName of tables) {
if (await columnExists(queryInterface, tableName, COLUMN_NAME)) {
results.push({ tableName, action: 'skip', reason: 'exists' });
continue;
}
await queryInterface.addColumn(tableName, COLUMN_NAME, {
type: Sequelize.DATE,
allowNull: true,
defaultValue: null,
comment: '订单迁移进配送表的时间',
});
results.push({ tableName, action: 'added' });
}
console.log('[20260907120000-add-dispath-time-to-order-delivery-dispatch] up results:', JSON.stringify(results));
},
/**
* 回滚迁移:移除全部配送分表的 dispath_time 列。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
async down(queryInterface) {
const tables = await listDispatchShards(queryInterface);
const results = [];
for (const tableName of tables) {
if (!await columnExists(queryInterface, tableName, COLUMN_NAME)) {
results.push({ tableName, action: 'skip', reason: 'missing' });
continue;
}
await queryInterface.removeColumn(tableName, COLUMN_NAME);
results.push({ tableName, action: 'removed' });
}
console.log('[20260907120000-add-dispath-time-to-order-delivery-dispatch] down results:', JSON.stringify(results));
},
};