WMS_MAIN/database/migrations/20260527233000-rename-wms-express-column-dyxd-to-dy.js
liufumingren abf2c4d94a 1
2026-05-29 16:47:38 +08:00

127 lines
4.0 KiB
JavaScript
Raw Permalink 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_express';
const OLD_COLUMN = 'DYXD';
const NEW_COLUMN = 'DY';
/**
* 规范化 showAllTables 返回的表名。
* @param {string|Object} table 表名或表对象。
* @return {string} 规范化后的表名。
*/
const normalizeTableName = table => {
if (typeof table === 'string') {
return table;
}
return table && typeof table === 'object'
? (table.tableName || table.name || '')
: '';
};
/**
* 检查目标表是否存在。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<boolean>} 表是否存在。
*/
const hasTargetTable = async queryInterface => {
const allTables = await queryInterface.showAllTables();
const tableSet = new Set((allTables || []).map(normalizeTableName));
return tableSet.has(TABLE_NAME);
};
/**
* 合并列值:当目标列为空时,用源列值补齐。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {Object} transaction Sequelize 事务对象。
* @param {string} targetColumn 目标列名。
* @param {string} sourceColumn 源列名。
* @return {Promise<void>} 无返回值。
*/
const mergeColumnValues = async (queryInterface, transaction, targetColumn, sourceColumn) => {
await queryInterface.sequelize.query(
`UPDATE ${TABLE_NAME}
SET ${targetColumn} = COALESCE(NULLIF(${targetColumn}, ''), ${sourceColumn})
WHERE ${sourceColumn} IS NOT NULL AND ${sourceColumn} <> ''`,
{
transaction,
}
);
};
/**
* 执行升级迁移:将 wms_express.DYXD 重命名为 DY(幂等)。
*
* 处理策略:
* 1. 仅存在 DYXD:使用 renameColumn 改名为 DY。
* 2. 同时存在 DY 与 DYXD:将 DYXD 的非空值补齐到 DY,再删除 DYXD。
* 3. 不存在 DYXD:视为已完成,直接跳过。
*
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。
* @return {Promise<void>} 无返回值。
*/
module.exports.up = async (queryInterface, Sequelize) => {
void Sequelize;
if (!(await hasTargetTable(queryInterface))) {
return;
}
const columns = await queryInterface.describeTable(TABLE_NAME);
const hasOldColumn = Object.prototype.hasOwnProperty.call(columns, OLD_COLUMN);
const hasNewColumn = Object.prototype.hasOwnProperty.call(columns, NEW_COLUMN);
if (!hasOldColumn) {
return;
}
if (!hasNewColumn) {
await queryInterface.renameColumn(TABLE_NAME, OLD_COLUMN, NEW_COLUMN);
return;
}
await queryInterface.sequelize.transaction(async transaction => {
await mergeColumnValues(queryInterface, transaction, NEW_COLUMN, OLD_COLUMN);
await queryInterface.removeColumn(TABLE_NAME, OLD_COLUMN, { transaction });
});
};
/**
* 执行回滚迁移:将 wms_express.DY 回滚为 DYXD(幂等)。
*
* 处理策略:
* 1. 仅存在 DY:使用 renameColumn 改名回 DYXD。
* 2. 同时存在 DY 与 DYXD:将 DY 的非空值补齐到 DYXD,再删除 DY。
* 3. 不存在 DY:视为已回滚,直接跳过。
*
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。
* @return {Promise<void>} 无返回值。
*/
module.exports.down = async (queryInterface, Sequelize) => {
void Sequelize;
if (!(await hasTargetTable(queryInterface))) {
return;
}
const columns = await queryInterface.describeTable(TABLE_NAME);
const hasOldColumn = Object.prototype.hasOwnProperty.call(columns, OLD_COLUMN);
const hasNewColumn = Object.prototype.hasOwnProperty.call(columns, NEW_COLUMN);
if (!hasNewColumn) {
return;
}
if (!hasOldColumn) {
await queryInterface.renameColumn(TABLE_NAME, NEW_COLUMN, OLD_COLUMN);
return;
}
await queryInterface.sequelize.transaction(async transaction => {
await mergeColumnValues(queryInterface, transaction, OLD_COLUMN, NEW_COLUMN);
await queryInterface.removeColumn(TABLE_NAME, NEW_COLUMN, { transaction });
});
};