118 lines
3.6 KiB
JavaScript
118 lines
3.6 KiB
JavaScript
'use strict';
|
|
|
|
const TABLE_NAME = 'wms_print_platform';
|
|
const SORT_INDEX_NAME = 'idx_sort';
|
|
|
|
/**
|
|
* 获取当前数据库已存在的表名集合。
|
|
* @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 {string} tableName 表名。
|
|
* @return {Promise<Record<string, object>>} 字段定义映射。
|
|
*/
|
|
const getTableColumns = async (queryInterface, tableName) => {
|
|
return queryInterface.describeTable(tableName);
|
|
};
|
|
|
|
/**
|
|
* 获取指定表的索引名集合。
|
|
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
|
* @param {string} tableName 表名。
|
|
* @return {Promise<Set<string>>} 索引名集合。
|
|
*/
|
|
const getIndexNameSet = async (queryInterface, tableName) => {
|
|
const indexes = await queryInterface.showIndex(tableName);
|
|
return new Set(indexes.map(index => String(index && index.name ? index.name : '').trim()).filter(Boolean));
|
|
};
|
|
|
|
module.exports = {
|
|
/**
|
|
* 正向迁移:幂等删除打印平台表中的废弃字段与排序索引。
|
|
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
|
* @return {Promise<void>} 无返回值。
|
|
*/
|
|
async up(queryInterface) {
|
|
const existingTables = await getExistingTableSet(queryInterface);
|
|
if (!existingTables.has(TABLE_NAME)) {
|
|
return;
|
|
}
|
|
|
|
const columns = await getTableColumns(queryInterface, TABLE_NAME);
|
|
const indexNameSet = await getIndexNameSet(queryInterface, TABLE_NAME);
|
|
|
|
if (indexNameSet.has(SORT_INDEX_NAME)) {
|
|
await queryInterface.removeIndex(TABLE_NAME, SORT_INDEX_NAME);
|
|
}
|
|
|
|
if (columns.sort) {
|
|
await queryInterface.removeColumn(TABLE_NAME, 'sort');
|
|
}
|
|
|
|
if (columns.aliases) {
|
|
await queryInterface.removeColumn(TABLE_NAME, 'aliases');
|
|
}
|
|
|
|
if (columns.template_file) {
|
|
await queryInterface.removeColumn(TABLE_NAME, 'template_file');
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 回滚迁移:幂等恢复打印平台表中的废弃字段与排序索引。
|
|
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
|
* @param {typeof import('sequelize')} Sequelize Sequelize 类型集合。
|
|
* @return {Promise<void>} 无返回值。
|
|
*/
|
|
async down(queryInterface, Sequelize) {
|
|
const existingTables = await getExistingTableSet(queryInterface);
|
|
if (!existingTables.has(TABLE_NAME)) {
|
|
return;
|
|
}
|
|
|
|
const columns = await getTableColumns(queryInterface, TABLE_NAME);
|
|
|
|
if (!columns.aliases) {
|
|
await queryInterface.addColumn(TABLE_NAME, 'aliases', {
|
|
type: Sequelize.JSON,
|
|
allowNull: true,
|
|
comment: '平台别名数组',
|
|
});
|
|
}
|
|
|
|
if (!columns.template_file) {
|
|
await queryInterface.addColumn(TABLE_NAME, 'template_file', {
|
|
type: Sequelize.STRING(100),
|
|
allowNull: true,
|
|
comment: '默认模板文件名',
|
|
});
|
|
}
|
|
|
|
if (!columns.sort) {
|
|
await queryInterface.addColumn(TABLE_NAME, 'sort', {
|
|
type: Sequelize.INTEGER,
|
|
allowNull: false,
|
|
defaultValue: 0,
|
|
comment: '排序值',
|
|
});
|
|
}
|
|
|
|
const indexNameSet = await getIndexNameSet(queryInterface, TABLE_NAME);
|
|
if (!indexNameSet.has(SORT_INDEX_NAME)) {
|
|
await queryInterface.addIndex(TABLE_NAME, [ 'sort' ], {
|
|
name: SORT_INDEX_NAME,
|
|
});
|
|
}
|
|
},
|
|
};
|