175 lines
5.3 KiB
JavaScript
175 lines
5.3 KiB
JavaScript
'use strict';
|
||
|
||
const TABLE = 'wms_order_delivery';
|
||
|
||
/**
|
||
* 与 `app/model/order_delivery.js`、`order_delivery_factory.js` 中 DDL 一致。
|
||
* `up` 仅对缺失列执行 ADD;`down` 按逆序删除下列列(若存在)——若某列在迁移前已存在且含数据,请勿执行 down。
|
||
*
|
||
* 列顺序与工厂建表一致,便于 `AFTER` 链式追加时相对位置稳定。
|
||
*/
|
||
const STATUS_MACHINE_COLUMNS = [
|
||
{
|
||
name: 'sync_status',
|
||
ddl: "enum('padding','fin','error') DEFAULT 'padding'",
|
||
comment: '同步状态',
|
||
after: 'oaid',
|
||
},
|
||
{
|
||
name: 'post_status',
|
||
ddl: "enum('padding','fin','timeout','error','none') DEFAULT 'none'",
|
||
comment: '分配快递号状态机',
|
||
after: 'wave_id',
|
||
},
|
||
{
|
||
name: 'storage_status',
|
||
ddl: "enum('padding','fin','error','none') DEFAULT 'none'",
|
||
comment: '分配储位状态机',
|
||
after: 'post_status',
|
||
},
|
||
{
|
||
name: 'pick_status',
|
||
ddl: "enum('padding','fin','error','none') DEFAULT 'none'",
|
||
comment: '拣货状态机',
|
||
after: 'storage_status',
|
||
},
|
||
{
|
||
name: 'pack_status',
|
||
ddl: "enum('none','padding','fin','error') DEFAULT 'none'",
|
||
comment: '打包状态机',
|
||
after: 'pick_status',
|
||
},
|
||
{
|
||
name: 'inspection_status',
|
||
ddl: "enum('none','padding','running','fin','error') DEFAULT 'none'",
|
||
comment: '检验状态机',
|
||
after: 'pack_status',
|
||
},
|
||
{
|
||
name: 'print_status',
|
||
ddl: "enum('none','padding','fin','error') DEFAULT NULL",
|
||
comment: '打印状态机',
|
||
after: 'step',
|
||
},
|
||
{
|
||
name: 'claim_status',
|
||
ddl: "enum('none','padding','fin','error') DEFAULT 'none'",
|
||
comment: '领取状态机',
|
||
after: 'print_status',
|
||
},
|
||
{
|
||
name: 'outbound_status',
|
||
ddl: "enum('none','padding','fin','error') DEFAULT 'none'",
|
||
comment: '出库状态机',
|
||
after: 'claim_status',
|
||
},
|
||
{
|
||
name: 'lock_status',
|
||
ddl: "enum('none','warehouse','store') DEFAULT NULL",
|
||
comment: '锁定状态机',
|
||
after: 'outbound_status',
|
||
},
|
||
{
|
||
name: 'intercept_status',
|
||
ddl:
|
||
"enum('none','requesting','requested','intercepting','intercepted_in_transit','intercepted_at_warehouse','returning','returned','delivered_by_mistake','timeout','cancelled','failed','exception') DEFAULT NULL",
|
||
comment: '拦截状态机',
|
||
after: 'lock_status',
|
||
},
|
||
];
|
||
|
||
/**
|
||
* @param {import('sequelize').QueryInterface} queryInterface Sequelize 查询接口。
|
||
* @param {string} tableName 数据表名。
|
||
* @param {string} columnName 字段名。
|
||
* @return {Promise<boolean>} 是否存在指定字段。
|
||
*/
|
||
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 columns.length > 0;
|
||
}
|
||
|
||
/**
|
||
* @param {import('sequelize').QueryInterface} queryInterface Sequelize 查询接口。
|
||
* @param {string} tableName 数据表名。
|
||
* @return {Promise<boolean>} 数据表是否存在。
|
||
*/
|
||
async function tableExists(queryInterface, tableName) {
|
||
const [ rows ] = await queryInterface.sequelize.query(
|
||
`
|
||
SELECT COUNT(*) AS cnt
|
||
FROM INFORMATION_SCHEMA.TABLES
|
||
WHERE TABLE_SCHEMA = DATABASE()
|
||
AND TABLE_NAME = :tableName
|
||
`,
|
||
{ replacements: { tableName } }
|
||
);
|
||
return Number(rows[0].cnt) > 0;
|
||
}
|
||
|
||
/**
|
||
* @param {import('sequelize').QueryInterface} queryInterface Sequelize 查询接口。
|
||
* @param {string} tableName 数据表名。
|
||
* @param {string} preferredAfter 优先追加到其后的字段名。
|
||
* @return {Promise<string|null>} 可直接拼接到 SQL 中的 AFTER 片段,或空字符串。
|
||
*/
|
||
async function resolveAfterClause(queryInterface, tableName, preferredAfter) {
|
||
if (preferredAfter && (await hasColumn(queryInterface, tableName, preferredAfter))) {
|
||
return `AFTER \`${preferredAfter}\``;
|
||
}
|
||
return '';
|
||
}
|
||
|
||
module.exports = {
|
||
/**
|
||
* 仅为基表 `wms_order_delivery` 按需补状态机相关列;已存在则跳过。
|
||
*
|
||
* @param {import('sequelize').QueryInterface} queryInterface Sequelize 查询接口。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async up(queryInterface) {
|
||
if (!(await tableExists(queryInterface, TABLE))) {
|
||
return;
|
||
}
|
||
|
||
for (const col of STATUS_MACHINE_COLUMNS) {
|
||
if (await hasColumn(queryInterface, TABLE, col.name)) {
|
||
continue;
|
||
}
|
||
const afterPart = await resolveAfterClause(queryInterface, TABLE, col.after);
|
||
await queryInterface.sequelize.query(
|
||
`ALTER TABLE \`${TABLE}\` ADD COLUMN \`${col.name}\` ${col.ddl} COMMENT '${col.comment}' ${afterPart}`.trim()
|
||
);
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 逆序删除本文件声明的状态机列(若存在)。见文件头注释。
|
||
*
|
||
* @param {import('sequelize').QueryInterface} queryInterface Sequelize 查询接口。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async down(queryInterface) {
|
||
if (!(await tableExists(queryInterface, TABLE))) {
|
||
return;
|
||
}
|
||
const reversed = [ ...STATUS_MACHINE_COLUMNS ].reverse();
|
||
for (const col of reversed) {
|
||
if (await hasColumn(queryInterface, TABLE, col.name)) {
|
||
await queryInterface.sequelize.query(
|
||
`ALTER TABLE \`${TABLE}\` DROP COLUMN \`${col.name}\``
|
||
);
|
||
}
|
||
}
|
||
},
|
||
};
|