WMS_MAIN/database/migrations/20260525113000-add-request-platform-code-to-wms-print-platform.js
liufumingren abf2c4d94a 1
2026-05-29 16:47:38 +08:00

69 lines
2.1 KiB
JavaScript

'use strict';
const TABLE_NAME = 'wms_print_platform';
const COLUMN_NAME = 'request_platform_code';
/**
* 获取当前数据库已存在的表名集合。
* @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))
);
};
/**
* 构建打印平台请求平台编码字段定义。
* @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。
* @return {Object} 字段定义。
*/
const buildRequestPlatformCodeColumn = Sequelize => ({
type: Sequelize.STRING(50),
allowNull: true,
comment: '请求平台编码',
});
module.exports = {
/**
* 执行迁移:为打印平台表新增 request_platform_code 字段。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。
* @return {Promise<void>} 无返回值。
*/
async up(queryInterface, Sequelize) {
const existingTableSet = await getExistingTableSet(queryInterface);
if (!existingTableSet.has(TABLE_NAME)) {
return;
}
const columns = await queryInterface.describeTable(TABLE_NAME);
if (columns[COLUMN_NAME]) {
return;
}
await queryInterface.addColumn(TABLE_NAME, COLUMN_NAME, buildRequestPlatformCodeColumn(Sequelize));
},
/**
* 回滚迁移:删除打印平台表的 request_platform_code 字段。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
async down(queryInterface) {
const existingTableSet = await getExistingTableSet(queryInterface);
if (!existingTableSet.has(TABLE_NAME)) {
return;
}
const columns = await queryInterface.describeTable(TABLE_NAME);
if (!columns[COLUMN_NAME]) {
return;
}
await queryInterface.removeColumn(TABLE_NAME, COLUMN_NAME);
},
};