- 订单表新增 intercept_time 拦截时间字段(迁移覆盖基表与全部分表),全拦截链路写入拦截时间、解除拦截清空 - 新增自动库内拦截确定定时任务(1h 间隔,扫描 requested+未打印订单,超时自动库内拦截确定) - 复核链路自动移除波次内未打印拦截订单回池,拦截解除后可重新抓取发货 - 打印解绑车判定排除拦截订单,修复波次含拦截订单时车无法解锁 - 同步 methods 文档与变更记录;含 wave_scope 回填、workbench token 过期等前序迁移
64 lines
2.8 KiB
JavaScript
64 lines
2.8 KiB
JavaScript
'use strict';
|
||
|
||
const TABLE_NAME = 'wms_workbench';
|
||
|
||
/**
|
||
* Migration: add token_updated_at to wms_workbench
|
||
* - token_updated_at: DATETIME NULL,记录工作台 token 最后更新时间。
|
||
*
|
||
* 背景:定时任务将扫描工作台,若 token 超过 3 个月未更新则强制清除 token 使设备下线,
|
||
* 因此需要记录每个工作台 token 的最后更新时间。
|
||
*
|
||
* 回填策略:
|
||
* - 有 token 的行:token_updated_at = updated_at。token 列由 20260903100000 迁移新增,
|
||
* 绑定/刷新 token 时同步更新 updated_at,取 updated_at 是当前最接近「token 最后变更」的时间;
|
||
* - 无 token 的行:保持 NULL(从未绑定 token,不参与过期扫描)。
|
||
* - 回填语句同时显式赋值 updated_at = updated_at:表结构上 updated_at 带
|
||
* ON UPDATE CURRENT_TIMESTAMP,显式赋值可阻止其被回填动作顶替为迁移执行时间。
|
||
*
|
||
* Idempotent: 通过 describeTable 判断后仅补齐缺失部分,回填按幂等条件覆盖。
|
||
*/
|
||
module.exports = {
|
||
/**
|
||
* 执行正向迁移:新增 token_updated_at 列并回填存量数据。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {import('sequelize')} Sequelize Sequelize 实例。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
up: async (queryInterface, Sequelize) => {
|
||
const columns = await queryInterface.describeTable(TABLE_NAME);
|
||
|
||
if (!columns.token_updated_at) {
|
||
await queryInterface.addColumn(TABLE_NAME, 'token_updated_at', {
|
||
type: Sequelize.DATE,
|
||
allowNull: true,
|
||
comment: '工作台 token 最后更新时间(无 token 时为 NULL)',
|
||
});
|
||
console.log('[token_updated_at] 列已新增');
|
||
} else {
|
||
console.log('[token_updated_at] 列已存在,跳过新增');
|
||
}
|
||
|
||
const result = await queryInterface.sequelize.query(
|
||
`UPDATE ${TABLE_NAME} SET token_updated_at = updated_at, updated_at = updated_at WHERE (token IS NOT NULL AND token != '') AND token_updated_at IS NULL`
|
||
);
|
||
const affected = Array.isArray(result)
|
||
? Number((result[1] && (result[1].affectedRows || result[1].affectedCount || result[1].rowCount)) || 0)
|
||
: Number((result && (result.affectedRows || result.affectedCount || result.rowCount)) || 0);
|
||
console.log(`[token_updated_at] 回填完成,影响行数: ${affected}`);
|
||
},
|
||
|
||
/**
|
||
* 执行回滚迁移:删除 token_updated_at 列(回填数据随之删除)。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
down: async queryInterface => {
|
||
const columns = await queryInterface.describeTable(TABLE_NAME);
|
||
if (columns.token_updated_at) {
|
||
await queryInterface.removeColumn(TABLE_NAME, 'token_updated_at');
|
||
console.log('[token_updated_at] 列已删除');
|
||
}
|
||
},
|
||
};
|