016_createDeviceInstancesTable.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
  2. import { DeleteStatus , EnableStatus} from "../../client/share/types.ts";
  3. import { DeviceProtocolType } from "../../client/share/monitorTypes.ts";
  4. // 定义设备表迁移
  5. const createDeviceInstancesTable: MigrationLiveDefinition = {
  6. name: "create_device_instances_table",
  7. up: async (api) => {
  8. await api.schema.createTable('device_instances', (table) => {
  9. table.integer('id').unsigned().primary(); // 确保与zichan_info表的id字段类型一致
  10. table.foreign('id').references('id').inTable('zichan_info').onDelete('CASCADE');
  11. table.integer('type_id').unsigned().notNullable().comment('设备类型ID'); // 确保与device_types表的id字段类型一致
  12. table.foreign('type_id').references('id').inTable('device_types');
  13. table.enum('protocol', Object.values(DeviceProtocolType)).notNullable().comment('通信协议'); // 通信协议
  14. table.string('address').notNullable().comment('通信地址'); // 通信地址
  15. table.integer('collect_interval').defaultTo(60).comment('采集间隔(秒)'); // 采集间隔(秒)
  16. table.timestamp('last_collect_time').comment('最后采集时间'); // 最后采集时间
  17. table.text('remark').comment('备注'); // 备注
  18. table.integer('is_enabled').defaultTo(EnableStatus.ENABLED).comment('是否启用 (0否 1是)');
  19. table.integer('is_deleted').defaultTo(DeleteStatus.NOT_DELETED).comment('是否被删除 (0否 1是)');
  20. table.timestamps(true, true);
  21. // 添加索引
  22. table.index('id'); // 确保id列有索引
  23. table.index('type_id');
  24. table.index('protocol');
  25. table.index('is_enabled');
  26. table.index('is_deleted');
  27. });
  28. },
  29. down: async (api) => {
  30. await api.schema.dropTable('device_instances');
  31. }
  32. };
  33. export default createDeviceInstancesTable