020_createDeviceMonitorDataTable.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
  2. import { DeleteStatus} from "../../client/share/types.ts";
  3. import { DeviceStatus } from "../../client/share/monitorTypes.ts";
  4. // 设备监控数据表迁移
  5. const createDeviceMonitorDataTable: MigrationLiveDefinition = {
  6. name: 'create_device_monitor_data_table',
  7. up: async (api) => {
  8. await api.schema.createTable('device_monitor_data', (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('metric_type').comment('监控指标类型(temperature/humidity/cpu_usage等)');
  13. table.decimal('metric_value', 10, 2).comment('监控值');
  14. table.string('unit').comment('单位');
  15. table.integer('status').defaultTo(DeviceStatus.NORMAL).comment('状态');
  16. table.timestamp('collect_time').comment('采集时间');
  17. table.integer('is_deleted').defaultTo(DeleteStatus.NOT_DELETED).comment('是否删除 (0否 1是)');
  18. table.timestamps(true, true);
  19. // 添加索引
  20. table.index('device_id');
  21. table.index('metric_type');
  22. table.index('collect_time');
  23. table.index('status');
  24. table.index('is_deleted');
  25. });
  26. },
  27. down: async (api) => {
  28. await api.schema.dropTable('device_monitor_data');
  29. }
  30. };
  31. export default createDeviceMonitorDataTable