108 lines
4.0 KiB
JavaScript
108 lines
4.0 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 订单表增加 confirm_print_status(确认打印状态)字段。
|
||
*
|
||
* 背景:订单表已有 print_status(打印状态机,由打印回执/波次标记更新),
|
||
* 现增加确认打印状态:打印完成后,操作员真实扫描面单(运单号)进行确认,
|
||
* 确认后写入 confirm_print_status='fin',用于记录「面单已实际打印并核对」。
|
||
*
|
||
* 取值约定:
|
||
* - none = 未确认打印(默认);
|
||
* - fin = 已确认打印(扫描面单确认)。
|
||
*
|
||
* 覆盖范围:
|
||
* - 全部仓库分表 wms_order_delivery_{wh}(current)及
|
||
* _{wh}_history / _{wh}_dispatch / _{wh}_outsourced。
|
||
*
|
||
* 说明:基表 wms_order_delivery 业务不使用(订单均写入分表),本迁移
|
||
* 不对基表加列。基表上仍存在的兜底查询均带 attributes 白名单,不会
|
||
* 因模型新增字段触发 Unknown column。
|
||
*
|
||
* 幂等:列已存在则跳过。分表模型由 app/model/order_delivery_factory.js
|
||
* 运行时 drift 同步兜底,本迁移提前补齐避免运行时 ALTER。
|
||
*/
|
||
|
||
const SHARD_PATTERN = /^wms_order_delivery_(\d+)(?:_(history|dispatch|outsourced))?$/;
|
||
|
||
const COLUMN_NAME = 'confirm_print_status';
|
||
|
||
/**
|
||
* 列举库内全部订单分表。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @return {Promise<string[]>} 分表名列表。
|
||
*/
|
||
async function listOrderShards(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\\_%\' '
|
||
+ 'ORDER BY TABLE_NAME'
|
||
);
|
||
return (rows || [])
|
||
.map(row => String(row.tableName || row.TABLE_NAME || ''))
|
||
.filter(name => 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 = {
|
||
/**
|
||
* 执行迁移:为基表与全部分表增加 confirm_print_status 列。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {import('sequelize')} Sequelize Sequelize 构造器。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async up(queryInterface, Sequelize) {
|
||
const tables = await listOrderShards(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.ENUM('none', 'fin'),
|
||
allowNull: true,
|
||
defaultValue: 'none',
|
||
comment: '确认打印状态机(打印完成后真实扫描面单确认)',
|
||
});
|
||
results.push({ tableName, action: 'added' });
|
||
}
|
||
|
||
console.log('[20260920100000-add-confirm-print-status-to-order-delivery] up results:', JSON.stringify(results));
|
||
},
|
||
|
||
/**
|
||
* 回滚迁移:移除基表与全部分表的 confirm_print_status 列。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async down(queryInterface) {
|
||
const tables = await listOrderShards(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('[20260920100000-add-confirm-print-status-to-order-delivery] down results:', JSON.stringify(results));
|
||
},
|
||
};
|