91 lines
3.3 KiB
JavaScript
91 lines
3.3 KiB
JavaScript
'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: add box_remainder and box_weight to wms_receiving_detail
|
||
* - box_remainder: INT NOT NULL DEFAULT 0 COMMENT '箱余数'
|
||
* - box_weight: DECIMAL(10,2) NOT NULL DEFAULT '0.00' COMMENT '箱重量(kg)'
|
||
*
|
||
* Idempotent: uses queryInterface.describeTable to only add if missing.
|
||
*
|
||
* Raw SQL (MySQL 8+ supports IF NOT EXISTS):
|
||
* ALTER TABLE `wms_receiving_detail`
|
||
* ADD COLUMN IF NOT EXISTS `box_remainder` INT NOT NULL DEFAULT 0 COMMENT '箱余数',
|
||
* ADD COLUMN IF NOT EXISTS `box_weight` DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '箱重量(kg`)';
|
||
*
|
||
* If MySQL < 8 (no IF NOT EXISTS), use INFORMATION_SCHEMA checks before ALTER.
|
||
*/
|
||
|
||
module.exports = {
|
||
/**
|
||
* 执行正向迁移,按需新增收货明细箱字段。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {import('sequelize')} Sequelize Sequelize 实例。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
up: async (queryInterface, Sequelize) => {
|
||
const table = 'wms_receiving_detail';
|
||
await logMigrationDbContext(queryInterface, 'up', table);
|
||
const columns = await queryInterface.describeTable(table);
|
||
|
||
if (!columns.box_remainder) {
|
||
await queryInterface.addColumn(table, 'box_remainder', {
|
||
type: Sequelize.INTEGER,
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '箱余数',
|
||
});
|
||
}
|
||
|
||
if (!columns.box_weight) {
|
||
await queryInterface.addColumn(table, 'box_weight', {
|
||
type: Sequelize.DECIMAL(10, 2),
|
||
allowNull: false,
|
||
defaultValue: '0.00',
|
||
comment: '箱重量(kg)',
|
||
});
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 执行回滚迁移,按需删除收货明细箱字段。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {import('sequelize')} Sequelize Sequelize 实例。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
down: async (queryInterface, Sequelize) => {
|
||
const table = 'wms_receiving_detail';
|
||
await logMigrationDbContext(queryInterface, 'down', table);
|
||
const columns = await queryInterface.describeTable(table);
|
||
|
||
if (columns.box_remainder) {
|
||
await queryInterface.removeColumn(table, 'box_remainder');
|
||
}
|
||
if (columns.box_weight) {
|
||
await queryInterface.removeColumn(table, 'box_weight');
|
||
}
|
||
},
|
||
};
|