94 lines
3.3 KiB
JavaScript
94 lines
3.3 KiB
JavaScript
'use strict';
|
|
|
|
const PLATFORM_TABLE_NAME = 'wms_platform';
|
|
const PRINT_PLATFORM_TABLE_NAME = 'wms_print_platform';
|
|
const COLUMN_NAME = 'auth_base_url';
|
|
|
|
/**
|
|
* 获取当前数据库已存在的表名集合。
|
|
* @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 {import('sequelize').QueryInterface} queryInterface 查询接口。
|
|
* @param {Set<string>} existingTableSet 已存在表名集合。
|
|
* @param {string} tableName 表名。
|
|
* @return {Promise<Object|null>} 字段定义;表不存在时返回 null。
|
|
*/
|
|
const getTableColumns = async (queryInterface, existingTableSet, tableName) => {
|
|
if (!existingTableSet.has(tableName)) {
|
|
return null;
|
|
}
|
|
|
|
return queryInterface.describeTable(tableName);
|
|
};
|
|
|
|
/**
|
|
* 构建授权基础地址字段定义。
|
|
* @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。
|
|
* @return {Object} 字段定义。
|
|
*/
|
|
const buildAuthBaseUrlColumn = Sequelize => ({
|
|
type: Sequelize.STRING(255),
|
|
allowNull: true,
|
|
comment: '授权基础地址',
|
|
});
|
|
|
|
module.exports = {
|
|
/**
|
|
* 正向迁移:将授权基础地址字段迁移到打印平台表。
|
|
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
|
* @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。
|
|
* @return {Promise<void>} 无返回值。
|
|
*/
|
|
async up(queryInterface, Sequelize) {
|
|
const existingTableSet = await getExistingTableSet(queryInterface);
|
|
const printPlatformColumns = await getTableColumns(queryInterface, existingTableSet, PRINT_PLATFORM_TABLE_NAME);
|
|
const platformColumns = await getTableColumns(queryInterface, existingTableSet, PLATFORM_TABLE_NAME);
|
|
|
|
if (printPlatformColumns && !printPlatformColumns[COLUMN_NAME]) {
|
|
await queryInterface.addColumn(
|
|
PRINT_PLATFORM_TABLE_NAME,
|
|
COLUMN_NAME,
|
|
buildAuthBaseUrlColumn(Sequelize)
|
|
);
|
|
}
|
|
|
|
if (platformColumns && platformColumns[COLUMN_NAME]) {
|
|
await queryInterface.removeColumn(PLATFORM_TABLE_NAME, COLUMN_NAME);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 回滚迁移:将授权基础地址字段恢复到平台表。
|
|
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
|
* @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。
|
|
* @return {Promise<void>} 无返回值。
|
|
*/
|
|
async down(queryInterface, Sequelize) {
|
|
const existingTableSet = await getExistingTableSet(queryInterface);
|
|
const printPlatformColumns = await getTableColumns(queryInterface, existingTableSet, PRINT_PLATFORM_TABLE_NAME);
|
|
const platformColumns = await getTableColumns(queryInterface, existingTableSet, PLATFORM_TABLE_NAME);
|
|
|
|
if (platformColumns && !platformColumns[COLUMN_NAME]) {
|
|
await queryInterface.addColumn(
|
|
PLATFORM_TABLE_NAME,
|
|
COLUMN_NAME,
|
|
buildAuthBaseUrlColumn(Sequelize)
|
|
);
|
|
}
|
|
|
|
if (printPlatformColumns && printPlatformColumns[COLUMN_NAME]) {
|
|
await queryInterface.removeColumn(PRINT_PLATFORM_TABLE_NAME, COLUMN_NAME);
|
|
}
|
|
},
|
|
};
|