'use strict'; const TABLE_NAME = 'wms_print_platform'; /** * 获取当前数据库已存在的表名集合。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise>} 表名集合。 */ 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>} 索引名集合。 */ 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)); }; /** * 创建打印平台元数据表。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {import('sequelize')} Sequelize Sequelize 实例。 * @return {Promise} 无返回值。 */ const createPrintPlatformTable = async (queryInterface, Sequelize) => { await queryInterface.createTable(TABLE_NAME, { id: { type: Sequelize.BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, platform_code: { type: Sequelize.STRING(50), allowNull: false, comment: '打印平台编码', }, platform_name: { type: Sequelize.STRING(100), allowNull: false, comment: '打印平台名称', }, status: { type: Sequelize.STRING(20), allowNull: false, defaultValue: 'ACTIVE', comment: '状态', }, remark: { type: Sequelize.STRING(255), allowNull: true, comment: '备注', }, create_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, update_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), comment: '更新时间', }, deleted: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '软删除标记:0-未删除,1-已删除', }, }, { engine: 'InnoDB', charset: 'utf8mb4', collate: 'utf8mb4_general_ci', comment: '打印平台元数据表', }); }; /** * 确保打印平台元数据表索引存在。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ const ensurePrintPlatformIndexes = async queryInterface => { const indexNameSet = await getIndexNameSet(queryInterface, TABLE_NAME); if (!indexNameSet.has('uk_platform_code')) { await queryInterface.addIndex(TABLE_NAME, [ 'platform_code' ], { name: 'uk_platform_code', unique: true, }); } if (!indexNameSet.has('idx_status')) { await queryInterface.addIndex(TABLE_NAME, [ 'status' ], { name: 'idx_status', }); } }; module.exports = { /** * 创建打印平台元数据表及索引。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {import('sequelize')} Sequelize Sequelize 实例。 * @return {Promise} 无返回值。 */ async up(queryInterface, Sequelize) { const existingTables = await getExistingTableSet(queryInterface); if (!existingTables.has(TABLE_NAME)) { await createPrintPlatformTable(queryInterface, Sequelize); } await ensurePrintPlatformIndexes(queryInterface); }, /** * 回滚删除打印平台元数据表。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {import('sequelize')} Sequelize Sequelize 实例。 * @return {Promise} 无返回值。 */ async down(queryInterface, Sequelize) { void Sequelize; const existingTables = await getExistingTableSet(queryInterface); if (existingTables.has(TABLE_NAME)) { await queryInterface.dropTable(TABLE_NAME); } }, };