196 lines
5.5 KiB
JavaScript
196 lines
5.5 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* 获取打印记录表字段定义。
|
|
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
|
* @return {Promise<Object>} 字段定义对象。
|
|
*/
|
|
const getTableColumns = async queryInterface => {
|
|
return queryInterface.describeTable('wms_print_record');
|
|
};
|
|
|
|
/**
|
|
* 获取打印记录表索引名称集合。
|
|
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
|
* @return {Promise<Set<string>>} 索引名称集合。
|
|
*/
|
|
const getIndexNameSet = async queryInterface => {
|
|
const indexes = await queryInterface.showIndex('wms_print_record');
|
|
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>} 无返回值。
|
|
*/
|
|
module.exports.up = async (queryInterface, Sequelize) => {
|
|
const tableName = 'wms_print_record';
|
|
const columns = await getTableColumns(queryInterface);
|
|
|
|
if (!columns.send_status) {
|
|
await queryInterface.addColumn(tableName, 'send_status', {
|
|
type: Sequelize.STRING(32),
|
|
allowNull: false,
|
|
defaultValue: 'pending',
|
|
comment: '发送状态',
|
|
});
|
|
}
|
|
|
|
if (!columns.print_status) {
|
|
await queryInterface.addColumn(tableName, 'print_status', {
|
|
type: Sequelize.STRING(32),
|
|
allowNull: false,
|
|
defaultValue: 'unknown',
|
|
comment: '打印状态',
|
|
});
|
|
}
|
|
|
|
if (!columns.retry_count) {
|
|
await queryInterface.addColumn(tableName, 'retry_count', {
|
|
type: Sequelize.INTEGER,
|
|
allowNull: false,
|
|
defaultValue: 0,
|
|
comment: '已重试次数',
|
|
});
|
|
}
|
|
|
|
if (!columns.max_retry_count) {
|
|
await queryInterface.addColumn(tableName, 'max_retry_count', {
|
|
type: Sequelize.INTEGER,
|
|
allowNull: false,
|
|
defaultValue: 3,
|
|
comment: '最大重试次数',
|
|
});
|
|
}
|
|
|
|
if (!columns.next_retry_at) {
|
|
await queryInterface.addColumn(tableName, 'next_retry_at', {
|
|
type: Sequelize.DATE,
|
|
allowNull: true,
|
|
comment: '下次重试时间',
|
|
});
|
|
}
|
|
|
|
if (!columns.last_retry_at) {
|
|
await queryInterface.addColumn(tableName, 'last_retry_at', {
|
|
type: Sequelize.DATE,
|
|
allowNull: true,
|
|
comment: '最后重试时间',
|
|
});
|
|
}
|
|
|
|
if (!columns.fail_code) {
|
|
await queryInterface.addColumn(tableName, 'fail_code', {
|
|
type: Sequelize.STRING(64),
|
|
allowNull: true,
|
|
comment: '失败编码',
|
|
});
|
|
}
|
|
|
|
if (!columns.retry_of_print_id) {
|
|
await queryInterface.addColumn(tableName, 'retry_of_print_id', {
|
|
type: Sequelize.BIGINT(20).UNSIGNED,
|
|
allowNull: true,
|
|
comment: '补偿来源打印记录ID',
|
|
});
|
|
}
|
|
|
|
const indexNameSet = await getIndexNameSet(queryInterface);
|
|
|
|
if (!indexNameSet.has('idx_send_status')) {
|
|
await queryInterface.addIndex(tableName, [ 'send_status' ], {
|
|
name: 'idx_send_status',
|
|
});
|
|
}
|
|
|
|
if (!indexNameSet.has('idx_print_status')) {
|
|
await queryInterface.addIndex(tableName, [ 'print_status' ], {
|
|
name: 'idx_print_status',
|
|
});
|
|
}
|
|
|
|
if (!indexNameSet.has('idx_next_retry_at')) {
|
|
await queryInterface.addIndex(tableName, [ 'next_retry_at' ], {
|
|
name: 'idx_next_retry_at',
|
|
});
|
|
}
|
|
|
|
if (!indexNameSet.has('idx_retry_of_print_id')) {
|
|
await queryInterface.addIndex(tableName, [ 'retry_of_print_id' ], {
|
|
name: 'idx_retry_of_print_id',
|
|
});
|
|
}
|
|
|
|
if (!indexNameSet.has('idx_print_status_next_retry')) {
|
|
await queryInterface.addIndex(tableName, [ 'print_status', 'next_retry_at' ], {
|
|
name: 'idx_print_status_next_retry',
|
|
});
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 回滚迁移:移除打印记录表补偿重试字段与索引(幂等)。
|
|
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
|
* @return {Promise<void>} 无返回值。
|
|
*/
|
|
module.exports.down = async queryInterface => {
|
|
const tableName = 'wms_print_record';
|
|
const indexNameSet = await getIndexNameSet(queryInterface);
|
|
|
|
if (indexNameSet.has('idx_print_status_next_retry')) {
|
|
await queryInterface.removeIndex(tableName, 'idx_print_status_next_retry');
|
|
}
|
|
|
|
if (indexNameSet.has('idx_retry_of_print_id')) {
|
|
await queryInterface.removeIndex(tableName, 'idx_retry_of_print_id');
|
|
}
|
|
|
|
if (indexNameSet.has('idx_next_retry_at')) {
|
|
await queryInterface.removeIndex(tableName, 'idx_next_retry_at');
|
|
}
|
|
|
|
if (indexNameSet.has('idx_print_status')) {
|
|
await queryInterface.removeIndex(tableName, 'idx_print_status');
|
|
}
|
|
|
|
if (indexNameSet.has('idx_send_status')) {
|
|
await queryInterface.removeIndex(tableName, 'idx_send_status');
|
|
}
|
|
|
|
const columns = await getTableColumns(queryInterface);
|
|
|
|
if (columns.retry_of_print_id) {
|
|
await queryInterface.removeColumn(tableName, 'retry_of_print_id');
|
|
}
|
|
|
|
if (columns.fail_code) {
|
|
await queryInterface.removeColumn(tableName, 'fail_code');
|
|
}
|
|
|
|
if (columns.last_retry_at) {
|
|
await queryInterface.removeColumn(tableName, 'last_retry_at');
|
|
}
|
|
|
|
if (columns.next_retry_at) {
|
|
await queryInterface.removeColumn(tableName, 'next_retry_at');
|
|
}
|
|
|
|
if (columns.max_retry_count) {
|
|
await queryInterface.removeColumn(tableName, 'max_retry_count');
|
|
}
|
|
|
|
if (columns.retry_count) {
|
|
await queryInterface.removeColumn(tableName, 'retry_count');
|
|
}
|
|
|
|
if (columns.print_status) {
|
|
await queryInterface.removeColumn(tableName, 'print_status');
|
|
}
|
|
|
|
if (columns.send_status) {
|
|
await queryInterface.removeColumn(tableName, 'send_status');
|
|
}
|
|
};
|