025_createInspectionTables.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
  2. import { DeleteStatus } from "../../client/share/types.ts";
  3. // 巡检相关表迁移
  4. const createInspectionTables: MigrationLiveDefinition = {
  5. name: 'create_inspection_tables',
  6. up: async (api) => {
  7. await api.schema.createTable('inspection_templates', (table) => {
  8. table.increments('id').primary();
  9. table.string('name').notNullable().comment('模板名称');
  10. table.string('description').nullable().comment('模板描述');
  11. table.jsonb('device_types').notNullable().comment('适用的设备类型');
  12. table.jsonb('metrics').notNullable().comment('检查指标配置');
  13. table.boolean('is_active').defaultTo(true).comment('是否启用');
  14. table.timestamps(true, true);
  15. table.index('is_active');
  16. });
  17. await api.schema.createTable('inspection_tasks', (table) => {
  18. table.increments('id').primary();
  19. table.integer('template_id').unsigned().comment('关联的模板ID')
  20. .references('id').inTable('inspection_templates').onDelete('CASCADE');
  21. table.string('name').notNullable().comment('任务名称');
  22. table.string('schedule_type').notNullable().comment('调度类型');
  23. table.string('cron_expression').nullable().comment('Cron表达式');
  24. table.integer('created_by').unsigned().comment('创建人ID')
  25. .references('id').inTable('users').onDelete('SET NULL');
  26. table.string('status').defaultTo('pending').comment('任务状态');
  27. table.timestamps(true, true);
  28. table.index('template_id');
  29. table.index('created_by');
  30. table.index('status');
  31. });
  32. await api.schema.createTable('inspection_results', (table) => {
  33. table.increments('id').primary();
  34. table.integer('task_id').unsigned().comment('关联的任务ID')
  35. .references('id').inTable('inspection_tasks').onDelete('CASCADE');
  36. table.integer('device_id').unsigned().comment('关联的设备ID')
  37. .references('id').inTable('device_instances').onDelete('CASCADE');
  38. table.jsonb('metrics_data').notNullable().comment('指标数据');
  39. table.string('overall_status').notNullable().comment('整体状态');
  40. table.timestamps(true, true);
  41. table.index('task_id');
  42. table.index('device_id');
  43. table.index('overall_status');
  44. });
  45. await api.schema.createTable('inspection_report_receivers', (table) => {
  46. table.increments('id').primary();
  47. table.integer('user_id').unsigned().comment('接收人ID')
  48. .references('id').inTable('users').onDelete('CASCADE');
  49. table.string('receive_method').notNullable().comment('接收方式');
  50. table.timestamps(true, true);
  51. table.index('user_id');
  52. });
  53. },
  54. down: async (api) => {
  55. await api.schema.dropTable('inspection_report_receivers');
  56. await api.schema.dropTable('inspection_results');
  57. await api.schema.dropTable('inspection_tasks');
  58. await api.schema.dropTable('inspection_templates');
  59. }
  60. };
  61. export default createInspectionTables