022_createDeviceAlertsTable.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
  2. import { DeleteStatus } from "../../client/share/types.ts";
  3. import { AlertStatus } from "../../client/share/monitorTypes.ts";
  4. // 设备告警记录表迁移
  5. const createDeviceAlertsTable: MigrationLiveDefinition = {
  6. name: 'create_device_alerts_table',
  7. up: async (api) => {
  8. await api.schema.createTable('device_alerts', (table) => {
  9. table.increments('id').primary();
  10. table.integer('device_id').unsigned().comment('关联的设备ID')
  11. .references('id').inTable('device_instances').onDelete('CASCADE');
  12. table.string('device_name').comment('设备名称');
  13. table.string('metric_type').comment('监控指标类型');
  14. table.decimal('metric_value', 10, 2).comment('触发值');
  15. table.integer('alert_level').comment('告警等级');
  16. table.string('alert_message').comment('告警消息');
  17. table.string('status').defaultTo(AlertStatus.PENDING).comment('状态');
  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('status');
  25. table.index('is_deleted');
  26. table.index('created_at');
  27. });
  28. },
  29. down: async (api) => {
  30. await api.schema.dropTable('device_alerts');
  31. }
  32. };
  33. export default createDeviceAlertsTable