WMS_MAIN/database/migrations/20260520101000-rename-wms-review-task-to-recheck-task.js
liufumingren 8cf2c5befa 1
2026-05-20 16:31:29 +08:00

46 lines
1.6 KiB
JavaScript

'use strict';
/**
* 获取当前数据库已存在的表名集合(兼容 showAllTables 返回字符串或对象)。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<Set<string>>} 表名集合。
*/
const getExistingTableSet = async queryInterface => {
const tables = await queryInterface.showAllTables();
return new Set(
tables.map(table => (typeof table === 'string' ? table : table.tableName))
);
};
/**
* 升级迁移:将 wms_review_task 重命名为 wms_recheck_task。
* 仅在旧表存在且新表不存在时执行,其他情况安全跳过。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
module.exports.up = async queryInterface => {
const sourceTable = 'wms_review_task';
const targetTable = 'wms_recheck_task';
const tableSet = await getExistingTableSet(queryInterface);
if (tableSet.has(sourceTable) && !tableSet.has(targetTable)) {
await queryInterface.renameTable(sourceTable, targetTable);
}
};
/**
* 回滚迁移:将 wms_recheck_task 重命名回 wms_review_task。
* 仅在旧表存在且新表不存在时执行,其他情况安全跳过。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
module.exports.down = async queryInterface => {
const sourceTable = 'wms_recheck_task';
const targetTable = 'wms_review_task';
const tableSet = await getExistingTableSet(queryInterface);
if (tableSet.has(sourceTable) && !tableSet.has(targetTable)) {
await queryInterface.renameTable(sourceTable, targetTable);
}
};