85 lines
3.2 KiB
JavaScript
85 lines
3.2 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 intercept locked quantity snapshots to wms_storage_transaction
|
||
* - before_intercept_locked_quantity: INT NULL COMMENT '变更前拦截锁定数量'
|
||
* - after_intercept_locked_quantity: INT NULL COMMENT '变更后拦截锁定数量'
|
||
*
|
||
* 与既有 before/after_locked_quantity 快照字段保持一致:
|
||
* 可空(NULL)、无默认值,由写入逻辑维护。
|
||
*
|
||
* Idempotent: uses queryInterface.describeTable to only add if missing.
|
||
*/
|
||
|
||
module.exports = {
|
||
/**
|
||
* 执行正向迁移,按需新增拦截锁定数量快照字段。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {import('sequelize')} Sequelize Sequelize 实例。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
up: async (queryInterface, Sequelize) => {
|
||
const table = 'wms_storage_transaction';
|
||
await logMigrationDbContext(queryInterface, 'up', table);
|
||
const columns = await queryInterface.describeTable(table);
|
||
|
||
if (!columns.before_intercept_locked_quantity) {
|
||
await queryInterface.addColumn(table, 'before_intercept_locked_quantity', {
|
||
type: Sequelize.INTEGER,
|
||
allowNull: true,
|
||
comment: '变更前拦截锁定数量',
|
||
});
|
||
}
|
||
|
||
if (!columns.after_intercept_locked_quantity) {
|
||
await queryInterface.addColumn(table, 'after_intercept_locked_quantity', {
|
||
type: Sequelize.INTEGER,
|
||
allowNull: true,
|
||
comment: '变更后拦截锁定数量',
|
||
});
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 执行回滚迁移,按需删除拦截锁定数量快照字段。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
down: async queryInterface => {
|
||
const table = 'wms_storage_transaction';
|
||
await logMigrationDbContext(queryInterface, 'down', table);
|
||
const columns = await queryInterface.describeTable(table);
|
||
|
||
if (columns.before_intercept_locked_quantity) {
|
||
await queryInterface.removeColumn(table, 'before_intercept_locked_quantity');
|
||
}
|
||
|
||
if (columns.after_intercept_locked_quantity) {
|
||
await queryInterface.removeColumn(table, 'after_intercept_locked_quantity');
|
||
}
|
||
},
|
||
};
|