'use strict'; module.exports = { async up(queryInterface, Sequelize) { const { BIGINT, STRING, DATE, INTEGER, JSON } = Sequelize; const BIGINT_UNSIGNED = BIGINT.UNSIGNED; const [ tables ] = await queryInterface.sequelize.query( "SHOW TABLES LIKE 'wms_notification_send_log'" ); if (tables.length === 0) { await queryInterface.createTable('wms_notification_send_log', { id: { type: BIGINT_UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, trace_id: { type: STRING(100), allowNull: false, comment: '通知追踪ID', }, webhook_id: { type: BIGINT_UNSIGNED, allowNull: true, comment: 'Webhook 配置ID', }, store_id: { type: BIGINT_UNSIGNED, allowNull: true, comment: '触发主体ID', }, warehouse_id: { type: BIGINT_UNSIGNED, allowNull: true, comment: '仓库ID', }, owner_store_id: { type: BIGINT_UNSIGNED, allowNull: true, comment: '货主ID', }, business_name: { type: STRING(100), allowNull: false, comment: '业务名称', }, event: { type: STRING(100), allowNull: false, comment: '事件名', }, hook_type: { type: STRING(50), allowNull: true, comment: 'Webhook 类型', }, hook_url: { type: STRING(500), allowNull: true, comment: 'Webhook 地址', }, request_payload: { type: JSON, allowNull: true, comment: '请求载荷', }, response_payload: { type: JSON, allowNull: true, comment: '返回载荷', }, http_status: { type: INTEGER(11), allowNull: true, comment: 'HTTP 状态码', }, status: { type: STRING(20), allowNull: false, defaultValue: 'failed', comment: '发送状态', }, error_message: { type: STRING(1000), allowNull: true, comment: '失败原因', }, retry_count: { type: INTEGER(11), allowNull: false, defaultValue: 0, comment: '重试次数', }, created_by: { type: BIGINT_UNSIGNED, allowNull: true, comment: '创建人', }, created_at: { type: DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, }, { engine: 'InnoDB', charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '统一通知发送失败日志表', }); } const safeCreateIndex = async (tableName, fields, options) => { const [ existingIndexes ] = await queryInterface.sequelize.query( `SHOW INDEX FROM ${tableName} WHERE Key_name = '${options.name}'` ); if (existingIndexes.length === 0) { await queryInterface.addIndex(tableName, fields, options); } }; await safeCreateIndex('wms_notification_send_log', [ 'trace_id' ], { name: 'idx_notification_send_log_trace_id', }); await safeCreateIndex('wms_notification_send_log', [ 'event', 'status' ], { name: 'idx_notification_send_log_event_status', }); await safeCreateIndex('wms_notification_send_log', [ 'webhook_id' ], { name: 'idx_notification_send_log_webhook_id', }); await safeCreateIndex('wms_notification_send_log', [ 'created_at' ], { name: 'idx_notification_send_log_created_at', }); }, async down(queryInterface) { const [ tables ] = await queryInterface.sequelize.query( "SHOW TABLES LIKE 'wms_notification_send_log'" ); if (tables.length > 0) { await queryInterface.dropTable('wms_notification_send_log'); } }, };