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() 记录进入配送表时间
This commit is contained in:
parent
6939d9600a
commit
c5150bc35b
@ -425,7 +425,7 @@ function buildTableAttributes(app, type) {
|
||||
attributes.id = { ...attributes.id, autoIncrement: false };
|
||||
|
||||
if (isDispatchType(modelType)) {
|
||||
const { ENUM } = app.Sequelize;
|
||||
const { ENUM, DATE } = app.Sequelize;
|
||||
attributes.dispatch_lane = {
|
||||
type: ENUM('m4', 'm5'),
|
||||
field: 'dispatch_lane',
|
||||
@ -433,6 +433,12 @@ function buildTableAttributes(app, type) {
|
||||
defaultValue: 'm4',
|
||||
comment: '配送工作台子域(M4正常/M5异常)',
|
||||
};
|
||||
attributes.dispath_time = {
|
||||
type: DATE,
|
||||
field: 'dispath_time',
|
||||
allowNull: true,
|
||||
comment: '订单迁移进配送表的时间',
|
||||
};
|
||||
}
|
||||
|
||||
if (isHistoryType(modelType)) {
|
||||
|
||||
@ -305,13 +305,21 @@ class OrderDeliveryMigrationService extends MainService {
|
||||
/**
|
||||
* 构造配送表插入行。
|
||||
*
|
||||
* 写入配送表时追加 dispath_time(当前时间),用于记录订单迁移进配送表的时间;
|
||||
* 仅配送表写入该字段,源表数据不受影响。
|
||||
*
|
||||
* @param {Object} row 当前订单工作台数据。
|
||||
* @return {Object} 可写入配送表的数据。
|
||||
*/
|
||||
buildDispatchRow(row) {
|
||||
const payload = { ...(row || {}) };
|
||||
delete payload.dispatch_lane;
|
||||
return { ...payload, dispatch_lane: DISPATCH_LANE };
|
||||
|
||||
return {
|
||||
...payload,
|
||||
dispatch_lane: DISPATCH_LANE,
|
||||
dispath_time: new Date(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -0,0 +1,102 @@
|
||||
'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));
|
||||
},
|
||||
};
|
||||
@ -7,7 +7,7 @@
|
||||
* - PowerShell: `$env:DISPATCH_FACTORY_TEST_MYSQL='1'; npm run test:local -- test/app/model/order_delivery_dispatch_factory.test.js`
|
||||
* - 保留表便于 `SHOW TABLES`:`$env:KEEP_DISPATCH_FACTORY_TABLES='1'` 同上一起设。
|
||||
*
|
||||
* 目标表名(单表,M4/M5 用列 `dispatch_lane` 区分):`wms_order_delivery_dispatch_100000`
|
||||
* 目标表名(单表,M4/M5 用列 `dispatch_lane` 区分):`wms_order_delivery_100000_dispatch`
|
||||
*/
|
||||
|
||||
const assert = require('node:assert/strict');
|
||||
@ -40,6 +40,7 @@ function createSequelizeTypes() {
|
||||
INTEGER: length => makeType(`INT(${length})`),
|
||||
CHAR: length => makeType(`CHAR(${length})`),
|
||||
SMALLINT: length => makeType(`SMALLINT(${length})`),
|
||||
TINYINT: length => makeType(`TINYINT(${length})`),
|
||||
JSON: makeType('JSON'),
|
||||
};
|
||||
}
|
||||
@ -140,9 +141,9 @@ describe('test/app/model/order_delivery_dispatch_factory.test.js dispatch 模式
|
||||
assert.equal(factory.LANES.M5, 'm5');
|
||||
});
|
||||
|
||||
it('should resolve dispatch table name as wms_order_delivery_dispatch_{warehouseId}', async () => {
|
||||
it('should resolve dispatch table name as wms_order_delivery_{warehouseId}_dispatch', async () => {
|
||||
const name = await factory.getDispatchTableName(TEST_WH);
|
||||
assert.equal(name, `wms_order_delivery_dispatch_${TEST_WH}`);
|
||||
assert.equal(name, `wms_order_delivery_${TEST_WH}_dispatch`);
|
||||
});
|
||||
|
||||
it('should resolve outsourced table name with the same base schema source', async () => {
|
||||
@ -154,7 +155,7 @@ describe('test/app/model/order_delivery_dispatch_factory.test.js dispatch 模式
|
||||
assert.ok(!outsourcedAttributes.dispatch_lane, 'outsourced 不应附带 dispatch_lane');
|
||||
});
|
||||
|
||||
it('should reuse order delivery current schema and only append dispatch_lane in dispatch mode', () => {
|
||||
it('should reuse order delivery current schema and only append dispatch_lane and dispath_time in dispatch mode', () => {
|
||||
const orderFactory = createOrderDeliveryFactory({
|
||||
Sequelize: createSequelizeTypes(),
|
||||
model: {
|
||||
@ -195,8 +196,9 @@ describe('test/app/model/order_delivery_dispatch_factory.test.js dispatch 模式
|
||||
];
|
||||
|
||||
assert.equal(baseColumns.length, Object.keys(orderDeliveryModel.rawAttributes).length);
|
||||
assert.equal(dispatchColumns.length, baseColumns.length + 1);
|
||||
assert.equal(dispatchColumns.length, baseColumns.length + 2);
|
||||
assert.ok(dispatchColumns.some(line => line.includes('dispatch_lane')));
|
||||
assert.ok(dispatchColumns.some(line => line.includes('dispath_time')));
|
||||
assert.deepEqual(dispatchIndexes, expectedDispatchIndexLines);
|
||||
assert.deepEqual(dispatchIndexOptions, expectedDispatchIndexOptions);
|
||||
});
|
||||
|
||||
@ -412,6 +412,12 @@ function createHarness(options = {}) {
|
||||
async listManagedTableNames() {
|
||||
return [ 'wms_order_delivery_100000' ];
|
||||
},
|
||||
async ensureLocationSequenceTable() {
|
||||
return undefined;
|
||||
},
|
||||
async updateOrderLocation() {
|
||||
return undefined;
|
||||
},
|
||||
},
|
||||
OrderItemsFactory: {
|
||||
async getCurrentModel() {
|
||||
@ -510,6 +516,7 @@ describe('test/app/service/systems/order_delivery_migration.test.js', () => {
|
||||
assert.equal(capture.bulkCreateRows[0].modelId, 'targetOrder');
|
||||
assert.equal(capture.bulkCreateRows[1].modelId, 'targetItem');
|
||||
assert.equal(capture.bulkCreateRows[0].rows[0].dispatch_lane, 'm4');
|
||||
assert.ok(capture.bulkCreateRows[0].rows[0].dispath_time instanceof Date, '配送表行应写入 dispath_time 迁移时间');
|
||||
assert.equal(capture.bulkCreateRows[0].rows[0].id, 1);
|
||||
assert.equal(sourceOrderModel.rows.length, 2);
|
||||
assert.equal(targetOrderModel.rows.length, 3);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user