2
0

MigrateNotWorkingToPreWorking1737260000000.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { MigrationInterface, QueryRunner } from 'typeorm';
  2. /**
  3. * TypeORM 迁移:移除 not_working 工作状态
  4. *
  5. * 此迁移执行以下操作:
  6. * 1. 将所有 order_person 表中 work_status = 'not_working' 的记录更新为 'pre_working'
  7. * 2. 重建数据库枚举类型 order_person_work_status_enum,移除 'not_working' 值
  8. *
  9. * 迁移原因:
  10. * - Story 13.29 已从代码中移除 not_working 状态
  11. * - 数据库中仍有 65 条记录使用此状态
  12. * - 需要保持数据库与代码一致
  13. *
  14. * 回滚支持:
  15. * - down() 方法会重新添加 not_working 值到枚举中
  16. */
  17. export class MigrateNotWorkingToPreWorking1737260000000 implements MigrationInterface {
  18. name = 'MigrateNotWorkingToPreWorking1737260000000';
  19. public async up(queryRunner: QueryRunner): Promise<void> {
  20. // 步骤 1: 更新数据 - 将所有 not_working 改为 pre_working
  21. await queryRunner.query(`
  22. UPDATE order_person
  23. SET work_status = 'pre_working'
  24. WHERE work_status = 'not_working'
  25. `);
  26. console.debug('Migration: Updated records from not_working to pre_working');
  27. // 步骤 2: 重建枚举类型(PostgreSQL 不支持直接删除枚举值)
  28. // 2.1 先移除 DEFAULT 约束(因为默认值引用了旧枚举类型)
  29. await queryRunner.query(`
  30. ALTER TABLE order_person
  31. ALTER COLUMN work_status DROP DEFAULT
  32. `);
  33. // 2.2 重命名旧枚举
  34. await queryRunner.query(`
  35. ALTER TYPE order_person_work_status_enum
  36. RENAME TO order_person_work_status_enum_old
  37. `);
  38. // 2.3 创建新枚举(不包含 not_working)
  39. await queryRunner.query(`
  40. CREATE TYPE order_person_work_status_enum AS ENUM
  41. ('pre_working', 'working', 'resigned')
  42. `);
  43. // 2.4 修改列类型到新枚举(使用 USING 子句进行类型转换)
  44. await queryRunner.query(`
  45. ALTER TABLE order_person
  46. ALTER COLUMN work_status
  47. TYPE order_person_work_status_enum
  48. USING work_status::text::order_person_work_status_enum
  49. `);
  50. // 2.5 重新添加 DEFAULT 约束(使用新枚举类型和有效值)
  51. await queryRunner.query(`
  52. ALTER TABLE order_person
  53. ALTER COLUMN work_status SET DEFAULT 'pre_working'
  54. `);
  55. // 2.6 删除旧枚举类型
  56. await queryRunner.query(`
  57. DROP TYPE order_person_work_status_enum_old
  58. `);
  59. console.debug('Migration: Successfully removed not_working from enum');
  60. }
  61. public async down(queryRunner: QueryRunner): Promise<void> {
  62. // 回滚:重新添加 not_working 值到枚举中
  63. // 1. 先移除 DEFAULT 约束
  64. await queryRunner.query(`
  65. ALTER TABLE order_person
  66. ALTER COLUMN work_status DROP DEFAULT
  67. `);
  68. // 2. 重命名当前枚举
  69. await queryRunner.query(`
  70. ALTER TYPE order_person_work_status_enum
  71. RENAME TO order_person_work_status_enum_old
  72. `);
  73. // 3. 创建包含 not_working 的新枚举
  74. await queryRunner.query(`
  75. CREATE TYPE order_person_work_status_enum AS ENUM
  76. ('not_working', 'pre_working', 'working', 'resigned')
  77. `);
  78. // 4. 修改列类型到新枚举
  79. await queryRunner.query(`
  80. ALTER TABLE order_person
  81. ALTER COLUMN work_status
  82. TYPE order_person_work_status_enum
  83. USING work_status::text::order_person_work_status_enum
  84. `);
  85. // 5. 重新添加 DEFAULT 约束(使用 not_working 作为默认值)
  86. await queryRunner.query(`
  87. ALTER TABLE order_person
  88. ALTER COLUMN work_status SET DEFAULT 'not_working'
  89. `);
  90. // 6. 删除旧枚举类型
  91. await queryRunner.query(`
  92. DROP TYPE order_person_work_status_enum_old
  93. `);
  94. console.debug('Migration rollback: Added not_working back to enum');
  95. }
  96. }