023_createAlertNotifyConfigsTable.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
  2. import { DeleteStatus, EnableStatus} from "../../client/share/types.ts";
  3. // 告警通知配置表迁移
  4. const createAlertNotifyConfigsTable: MigrationLiveDefinition = {
  5. name: 'create_alert_notify_configs_table',
  6. up: async (api) => {
  7. await api.schema.createTable('alert_notify_configs', (table) => {
  8. table.increments('id').primary();
  9. table.integer('device_id').unsigned().comment('关联的设备ID')
  10. .references('id').inTable('device_instances').onDelete('CASCADE');
  11. table.integer('alert_level').comment('告警等级');
  12. table.string('notify_type').comment('通知类型');
  13. table.string('notify_template').nullable().comment('通知模板');
  14. table.jsonb('notify_users').comment('通知用户ID列表');
  15. table.integer('is_enabled').defaultTo(EnableStatus.ENABLED).comment('是否启用 (0否 1是)');
  16. table.integer('is_deleted').defaultTo(DeleteStatus.NOT_DELETED).comment('是否删除 (0否 1是)');
  17. table.timestamps(true, true);
  18. // 添加索引
  19. table.index('device_id');
  20. table.index('alert_level');
  21. table.index('notify_type');
  22. table.index('is_enabled');
  23. table.index('is_deleted');
  24. // 同一设备的相同告警级别和通知类型应该是唯一的
  25. // table.unique(['device_id', 'alert_level', 'notify_type', 'is_deleted']);
  26. });
  27. },
  28. down: async (api) => {
  29. await api.schema.dropTable('alert_notify_configs');
  30. }
  31. };
  32. export default createAlertNotifyConfigsTable