WMS_MAIN/database/migrations/20260703000000-add-binding-fields-to-wms-inbound-direct-log.js
liufumingren 478ad2ab0d 1
2026-07-03 16:10:46 +08:00

101 lines
3.2 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_inbound_direct_log';
module.exports = {
up: async (queryInterface, Sequelize) => {
const tables = await queryInterface.showAllTables();
const exists = tables.some(t => (typeof t === 'string' ? t : t.tableName) === TABLE_NAME);
if (!exists) return;
const columns = await queryInterface.describeTable(TABLE_NAME);
// 新增 entry_item_id 字段
if (!columns.entry_item_id) {
await queryInterface.addColumn(TABLE_NAME, 'entry_item_id', {
type: Sequelize.BIGINT(20).UNSIGNED,
allowNull: true,
defaultValue: null,
comment: '入库明细ID',
});
}
// 新增 receiving_detail_id 字段
if (!columns.receiving_detail_id) {
await queryInterface.addColumn(TABLE_NAME, 'receiving_detail_id', {
type: Sequelize.INTEGER(11),
allowNull: true,
defaultValue: null,
comment: '收货明细ID,绑定收货时使用',
});
}
// 新增 status 字段
if (!columns.status) {
await queryInterface.addColumn(TABLE_NAME, 'status', {
type: Sequelize.ENUM('pending', 'committed', 'released'),
allowNull: true,
defaultValue: null,
comment: '绑定状态:pending=待提交 committed=已提交 released=已释放,非绑定模式为NULL',
});
}
// 新增 updated_at 字段
if (!columns.updated_at) {
await queryInterface.addColumn(TABLE_NAME, 'updated_at', {
type: Sequelize.DATE,
allowNull: true,
defaultValue: null,
comment: '更新时间',
});
}
// 新增 deleted_at 字段(软删除)
if (!columns.deleted_at) {
await queryInterface.addColumn(TABLE_NAME, 'deleted_at', {
type: Sequelize.DATE,
allowNull: true,
defaultValue: null,
comment: '软删除时间',
});
}
// 新增索引 idx_entry_item_id
await queryInterface.addIndex(TABLE_NAME, [ 'entry_item_id' ], {
name: 'idx_entry_item_id',
});
// 新增索引 idx_receiving_detail_id
await queryInterface.addIndex(TABLE_NAME, [ 'receiving_detail_id' ], {
name: 'idx_receiving_detail_id',
});
// 新增索引 idx_status
await queryInterface.addIndex(TABLE_NAME, [ 'status' ], {
name: 'idx_status',
});
// 新增唯一约束 uk_entry_receiving_binding
await queryInterface.addIndex(TABLE_NAME, [ 'entry_item_id', 'receiving_detail_id', 'status' ], {
name: 'uk_entry_receiving_binding',
unique: true,
});
},
down: async queryInterface => {
// 移除唯一约束
await queryInterface.removeIndex(TABLE_NAME, 'uk_entry_receiving_binding');
// 移除索引
await queryInterface.removeIndex(TABLE_NAME, 'idx_status');
await queryInterface.removeIndex(TABLE_NAME, 'idx_receiving_detail_id');
await queryInterface.removeIndex(TABLE_NAME, 'idx_entry_item_id');
// 移除字段
await queryInterface.removeColumn(TABLE_NAME, 'deleted_at');
await queryInterface.removeColumn(TABLE_NAME, 'updated_at');
await queryInterface.removeColumn(TABLE_NAME, 'status');
await queryInterface.removeColumn(TABLE_NAME, 'receiving_detail_id');
await queryInterface.removeColumn(TABLE_NAME, 'entry_item_id');
},
};