021_createDeviceAlertRulesTable.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
  2. import { DeleteStatus, EnableStatus} from "../../client/share/types.ts";
  3. // 设备告警规则表迁移
  4. const createDeviceAlertRulesTable: MigrationLiveDefinition = {
  5. name: 'create_device_alert_rules_table',
  6. up: async (api) => {
  7. await api.schema.createTable('device_alert_rules', (table) => {
  8. table.increments('id').primary();
  9. table.integer('device_id').unsigned().comment('关联的设备ID')
  10. .references('id').inTable('device_instances').onDelete('CASCADE');
  11. table.string('metric_type').comment('监控指标类型');
  12. table.decimal('min_value', 10, 2).nullable().comment('最小阈值');
  13. table.decimal('max_value', 10, 2).nullable().comment('最大阈值');
  14. table.integer('duration_seconds').defaultTo(60).comment('持续时间(秒)');
  15. table.integer('alert_level').comment('告警等级');
  16. table.string('alert_message').nullable().comment('告警消息模板');
  17. table.integer('is_enabled').defaultTo(EnableStatus.ENABLED).comment('是否启用 (0否 1是)');
  18. table.integer('is_deleted').defaultTo(DeleteStatus.NOT_DELETED).comment('是否删除 (0否 1是)');
  19. table.timestamps(true, true);
  20. // 添加索引
  21. table.index('device_id');
  22. table.index('metric_type');
  23. table.index('alert_level');
  24. table.index('is_enabled');
  25. table.index('is_deleted');
  26. // 为同一设备的相同指标类型添加唯一约束,指定更短的名称
  27. // table.unique(['device_id', 'metric_type', 'is_deleted'], 'uk_dev_alert_rules');
  28. });
  29. },
  30. down: async (api) => {
  31. await api.schema.dropTable('device_alert_rules');
  32. }
  33. };
  34. export default createDeviceAlertRulesTable