WMS_MAIN/database/migrations/20260826120000-allow-null-account-id-in-wms-pack-task-packer.js

51 lines
1.9 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';
const TABLE_NAME = 'wms_pack_task_packer';
/**
* 允许 account_id 为 NULL。
* 背景:打包任务写入多名打包人时,PDA 上传的 wms_account_id 可能为 null,
* 旧代码统一兜底为 0,多人场景下多条 (pack_task_id, account_id=0) 违反
* 唯一索引 uix_pack_task_account,导致 bulkCreate 抛 UniqueConstraintError,
* 进而中断 _packOrder 后续步骤(OSD 下发、操作日志)。
* MySQL 唯一索引对 NULL 不生效,故未知账号改为写 NULL。
*/
module.exports = {
up: async (queryInterface, Sequelize) => {
const tableInfo = await queryInterface.describeTable(TABLE_NAME);
const col = tableInfo.account_id;
if (!col) {
throw new Error(`[Migration] ${TABLE_NAME}.account_id 不存在`);
}
if (col.allowNull === false) {
await queryInterface.changeColumn(TABLE_NAME, 'account_id', {
type: Sequelize.BIGINT(20).UNSIGNED,
allowNull: true,
comment: '账号ID,关联 wms_account.id;未知账号为 NULL',
});
console.log(`[Migration] ${TABLE_NAME}.account_id 已改为允许 NULL`);
} else {
console.log(`[Migration] ${TABLE_NAME}.account_id 已允许 NULL,跳过`);
}
},
down: async (queryInterface, Sequelize) => {
const tableInfo = await queryInterface.describeTable(TABLE_NAME);
const col = tableInfo.account_id;
if (!col) return;
if (col.allowNull === true) {
// 回滚前把 NULL 归位为 0,避免改回 NOT NULL 失败
await queryInterface.sequelize.query(
`UPDATE ${TABLE_NAME} SET account_id = 0 WHERE account_id IS NULL`
);
await queryInterface.changeColumn(TABLE_NAME, 'account_id', {
type: Sequelize.BIGINT(20).UNSIGNED,
allowNull: false,
comment: '账号ID,关联 wms_account.id',
});
console.log(`[Migration] ${TABLE_NAME}.account_id 已恢复为 NOT NULL`);
}
},
};