WMS_MAIN/database/migrations/20260519210000-create-wms-print-task-map.js
liufumingren 8cf2c5befa 1
2026-05-20 16:31:29 +08:00

106 lines
3.2 KiB
JavaScript

'use strict';
/**
* 获取当前数据库已存在的表名集合。
* @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<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));
};
/**
* 创建打印任务去重映射表。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
const createPrintTaskMapTable = async (queryInterface, Sequelize) => {
await queryInterface.createTable('wms_print_task_map', {
id: {
type: Sequelize.BIGINT(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '主键ID',
},
task_code: {
type: Sequelize.STRING(128),
allowNull: false,
comment: '打印任务编码',
},
print_record_id: {
type: Sequelize.BIGINT(20).UNSIGNED,
allowNull: true,
comment: '关联打印记录ID',
},
provider: {
type: Sequelize.STRING(64),
allowNull: true,
comment: 'provider返回任务ID或标识',
},
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
comment: '创建时间',
},
}, {
comment: '打印任务唯一映射表',
engine: 'InnoDB',
charset: 'utf8mb4',
collate: 'utf8mb4_general_ci',
});
};
/**
* 新增打印任务唯一映射表(幂等)。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
module.exports.up = async (queryInterface, Sequelize) => {
const tableName = 'wms_print_task_map';
const tableSet = await getExistingTableSet(queryInterface);
if (!tableSet.has(tableName)) {
await createPrintTaskMapTable(queryInterface, Sequelize);
}
const indexNameSet = await getIndexNameSet(queryInterface, tableName);
if (!indexNameSet.has('uix_task_code')) {
await queryInterface.addIndex(tableName, [ 'task_code' ], {
name: 'uix_task_code',
unique: true,
});
}
};
/**
* 回滚删除打印任务唯一映射表(幂等)。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
module.exports.down = async queryInterface => {
const tableName = 'wms_print_task_map';
const tableSet = await getExistingTableSet(queryInterface);
if (!tableSet.has(tableName)) {
return;
}
await queryInterface.dropTable(tableName);
};