WMS_MAIN/database/migrations/archive/20260404113000-create-wms-notification-webhook.js

155 lines
4.6 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) {
const { BIGINT, STRING, TINYINT, DATE, JSON } = Sequelize;
const BIGINT_UNSIGNED = BIGINT.UNSIGNED;
const [ tables ] = await queryInterface.sequelize.query(
"SHOW TABLES LIKE 'wms_notification_webhook'"
);
const tableExists = tables.length > 0;
if (!tableExists) {
await queryInterface.createTable('wms_notification_webhook', {
id: {
type: BIGINT_UNSIGNED,
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '主键ID',
},
store_id: {
type: BIGINT_UNSIGNED,
allowNull: false,
comment: '主体ID,对应 wms_store.id',
references: {
model: 'wms_store',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
},
hook_name: {
type: STRING(100),
allowNull: true,
comment: 'Webhook 名称',
},
hook_type: {
type: STRING(50),
allowNull: false,
comment: 'Webhook 类型,用于分派不同消息结构处理类,如 dingtalk、wecom、feishu、custom',
},
hook_url: {
type: STRING(500),
allowNull: false,
comment: 'Webhook 链接地址',
},
events: {
type: JSON,
allowNull: true,
comment: '订阅事件列表',
},
extra_config: {
type: JSON,
allowNull: true,
comment: '扩展配置,如签名、请求头、模板映射等',
},
status: {
type: TINYINT(4),
allowNull: false,
defaultValue: 1,
comment: '状态:0-禁用,1-启用',
},
remark: {
type: STRING(255),
allowNull: true,
comment: '备注',
},
created_by: {
type: BIGINT_UNSIGNED,
allowNull: true,
comment: '创建人',
},
created_at: {
type: DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
comment: '创建时间',
},
updated_at: {
type: DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
comment: '更新时间',
},
deleted_at: {
type: DATE,
allowNull: true,
comment: '删除时间',
},
}, {
engine: 'InnoDB',
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci',
comment: '外部通知 WebHook 配置表',
});
} else {
console.log('Table wms_notification_webhook already exists, ensuring schema and indexes');
}
const [ hookUrlColumns ] = await queryInterface.sequelize.query("SHOW COLUMNS FROM `wms_notification_webhook` LIKE 'hook_url'");
if (hookUrlColumns.length > 0) {
const currentType = String(hookUrlColumns[0].Type || '').toLowerCase();
if (currentType.includes('varchar(1000)')) {
await queryInterface.changeColumn('wms_notification_webhook', 'hook_url', {
type: STRING(500),
allowNull: false,
comment: 'Webhook 链接地址',
});
}
}
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_webhook', [ 'store_id' ], {
name: 'idx_notification_webhook_store_id',
});
await safeCreateIndex('wms_notification_webhook', [ 'hook_type' ], {
name: 'idx_notification_webhook_hook_type',
});
await safeCreateIndex('wms_notification_webhook', [ 'status' ], {
name: 'idx_notification_webhook_status',
});
await safeCreateIndex('wms_notification_webhook', [ 'store_id', 'hook_type' ], {
name: 'idx_notification_webhook_store_type',
});
await safeCreateIndex('wms_notification_webhook', [ 'store_id', 'hook_type', 'hook_url' ], {
name: 'uk_notification_webhook_store_type_url',
unique: true,
});
},
async down(queryInterface) {
const [ tables ] = await queryInterface.sequelize.query(
"SHOW TABLES LIKE 'wms_notification_webhook'"
);
if (tables.length > 0) {
await queryInterface.dropTable('wms_notification_webhook');
}
},
};