oms/backend/database/migrations/20261117100000-create-waybill-fetch-idempotencies.js
liufumingren 4c2cfe1793 修复取号回调的订单级幂等与重放
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-17 17:16:14 +08:00

70 lines
2.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 为取号增加订单级幂等键,并在取号历史中保存包裹序号与完整返回结果。
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn('waybill_fetch_records', 'package_index', {
type: Sequelize.INTEGER.UNSIGNED,
allowNull: true,
comment: '包裹序号(0 基);历史记录可能为空',
after: 'express_code',
});
await queryInterface.addColumn('waybill_fetch_records', 'result_data', {
type: Sequelize.TEXT('medium'),
allowNull: true,
comment: '完整的逐单返回结果(JSON)',
after: 'waybill_codes',
});
await queryInterface.createTable('waybill_fetch_idempotencies', {
id: { type: Sequelize.BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
principal_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '主体ID' },
warehouse_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '仓库ID' },
delivery_order_code: { type: Sequelize.STRING(100), allowNull: false, comment: 'OMS 发货单号' },
express_code: { type: Sequelize.STRING(50), allowNull: false, comment: 'WMS 传入的快递编码' },
package_index: { type: Sequelize.INTEGER.UNSIGNED, allowNull: false, comment: '包裹序号(0 基)' },
request_id: { type: Sequelize.STRING(100), allowNull: true, comment: '首次取号的 request_id' },
status: {
type: Sequelize.TINYINT,
allowNull: false,
defaultValue: 0,
comment: '状态:0处理中 1成功 2结果待核实',
},
result_data: { type: Sequelize.TEXT('medium'), allowNull: true, comment: '成功时完整的逐单返回结果(JSON)' },
error_message: { type: Sequelize.TEXT, allowNull: true, comment: '结果待核实时的原因' },
created_at: { type: Sequelize.DATE, allowNull: true, comment: '创建时间' },
updated_at: { type: Sequelize.DATE, allowNull: true, comment: '更新时间' },
});
await queryInterface.addIndex('waybill_fetch_idempotencies', [
'principal_id',
'warehouse_id',
'delivery_order_code',
'express_code',
'package_index',
], {
unique: true,
name: 'uk_waybill_fetch_idempotency',
});
await queryInterface.addIndex('waybill_fetch_records', [
'principal_id',
'warehouse_id',
'delivery_order_code',
'express_code',
'package_index',
'status',
], {
name: 'idx_waybill_fetch_idempotency_lookup',
});
},
async down(queryInterface) {
await queryInterface.removeIndex('waybill_fetch_records', 'idx_waybill_fetch_idempotency_lookup');
await queryInterface.dropTable('waybill_fetch_idempotencies');
await queryInterface.removeColumn('waybill_fetch_records', 'result_data');
await queryInterface.removeColumn('waybill_fetch_records', 'package_index');
},
};