WMS_MAIN/database/migrations/20260629-add-waiting-storage-status.js

77 lines
4.5 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';
/**
* 打印迁移阶段对应的数据库连接上下文信息。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {'up'|'down'} phase 迁移阶段。
* @param {string} table 迁移目标表名。
* @return {Promise<void>} 无返回值。
*/
const logMigrationDbContext = async (queryInterface, phase, table) => {
try {
const [ rows ] = await queryInterface.sequelize.query(
'SELECT DATABASE() AS db_name, @@hostname AS db_host, @@port AS db_port, CURRENT_USER() AS db_user'
);
const dbContext = Array.isArray(rows) && rows.length > 0 ? rows[0] : {};
console.log(
`[Migration DB Context] phase=${phase}; table=${table}; db_name=${dbContext.db_name || 'unknown'}; db_host=${dbContext.db_host || 'unknown'}; db_port=${dbContext.db_port || 'unknown'}; db_user=${dbContext.db_user || 'unknown'}`
);
} catch (error) {
console.warn(
`[Migration DB Context] 获取连接信息失败: phase=${phase}; table=${table}; reason=${error.message}`
);
}
};
/**
* Migration: 为 wms_receiving 和 wms_receiving_detail 表的 storage_status 列新增 waiting 枚举值
*
* 正向 SQL:
* ALTER TABLE wms_receiving MODIFY COLUMN storage_status ENUM('none','waiting','partial','all') NOT NULL DEFAULT 'none' COMMENT '入库状态:none-未入库, waiting-等待入库, partial-部分入库, all-全部入库';
* ALTER TABLE wms_receiving_detail MODIFY COLUMN storage_status ENUM('none','waiting','partial','all') NOT NULL DEFAULT 'none' COMMENT '入库状态:none-未入库, waiting-等待入库, partial-部分入库, all-全部入库';
*
* 回滚 SQL:
* ALTER TABLE wms_receiving MODIFY COLUMN storage_status ENUM('none','partial','all') NOT NULL DEFAULT 'none' COMMENT '入库状态:none-未入库, partial-部分入库, all-全部入库';
* ALTER TABLE wms_receiving_detail MODIFY COLUMN storage_status ENUM('none','partial','all') NOT NULL DEFAULT 'none' COMMENT '入库状态:none-未入库, partial-部分入库, all-全部入库';
*/
const UP_SQL_RECEIVING = 'ALTER TABLE wms_receiving MODIFY COLUMN storage_status ENUM(\'none\',\'waiting\',\'partial\',\'all\') NOT NULL DEFAULT \'none\' COMMENT \'入库状态:none-未入库, waiting-等待入库, partial-部分入库, all-全部入库\'';
const DOWN_SQL_RECEIVING = 'ALTER TABLE wms_receiving MODIFY COLUMN storage_status ENUM(\'none\',\'partial\',\'all\') NOT NULL DEFAULT \'none\' COMMENT \'入库状态:none-未入库, partial-部分入库, all-全部入库\'';
const UP_SQL_DETAIL = 'ALTER TABLE wms_receiving_detail MODIFY COLUMN storage_status ENUM(\'none\',\'waiting\',\'partial\',\'all\') NOT NULL DEFAULT \'none\' COMMENT \'入库状态:none-未入库, waiting-等待入库, partial-部分入库, all-全部入库\'';
const DOWN_SQL_DETAIL = 'ALTER TABLE wms_receiving_detail MODIFY COLUMN storage_status ENUM(\'none\',\'partial\',\'all\') NOT NULL DEFAULT \'none\' COMMENT \'入库状态:none-未入库, partial-部分入库, all-全部入库\'';
module.exports = {
/**
* 执行正向迁移,为 storage_status 列新增 waiting 枚举值。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
up: async (queryInterface, Sequelize) => {
await logMigrationDbContext(queryInterface, 'up', 'wms_receiving');
await queryInterface.sequelize.query(UP_SQL_RECEIVING);
console.log('[Migration] wms_receiving.storage_status 已新增 waiting 枚举值');
await logMigrationDbContext(queryInterface, 'up', 'wms_receiving_detail');
await queryInterface.sequelize.query(UP_SQL_DETAIL);
console.log('[Migration] wms_receiving_detail.storage_status 已新增 waiting 枚举值');
},
/**
* 执行回滚迁移,移除 waiting 枚举值,恢复为原有枚举。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
down: async (queryInterface, Sequelize) => {
await logMigrationDbContext(queryInterface, 'down', 'wms_receiving');
await queryInterface.sequelize.query(DOWN_SQL_RECEIVING);
console.log('[Migration] wms_receiving.storage_status 已移除 waiting 枚举值');
await logMigrationDbContext(queryInterface, 'down', 'wms_receiving_detail');
await queryInterface.sequelize.query(DOWN_SQL_DETAIL);
console.log('[Migration] wms_receiving_detail.storage_status 已移除 waiting 枚举值');
},
};