|
|
@@ -28,19 +28,25 @@ export class MigrateNotWorkingToPreWorking1737260000000 implements MigrationInte
|
|
|
console.debug('Migration: Updated records from not_working to pre_working');
|
|
|
|
|
|
// 步骤 2: 重建枚举类型(PostgreSQL 不支持直接删除枚举值)
|
|
|
- // 2.1 重命名旧枚举
|
|
|
+ // 2.1 先移除 DEFAULT 约束(因为默认值引用了旧枚举类型)
|
|
|
+ await queryRunner.query(`
|
|
|
+ ALTER TABLE order_person
|
|
|
+ ALTER COLUMN work_status DROP DEFAULT
|
|
|
+ `);
|
|
|
+
|
|
|
+ // 2.2 重命名旧枚举
|
|
|
await queryRunner.query(`
|
|
|
ALTER TYPE order_person_work_status_enum
|
|
|
RENAME TO order_person_work_status_enum_old
|
|
|
`);
|
|
|
|
|
|
- // 2.2 创建新枚举(不包含 not_working)
|
|
|
+ // 2.3 创建新枚举(不包含 not_working)
|
|
|
await queryRunner.query(`
|
|
|
CREATE TYPE order_person_work_status_enum AS ENUM
|
|
|
('pre_working', 'working', 'resigned')
|
|
|
`);
|
|
|
|
|
|
- // 2.3 修改列类型到新枚举(使用 USING 子句进行类型转换)
|
|
|
+ // 2.4 修改列类型到新枚举(使用 USING 子句进行类型转换)
|
|
|
await queryRunner.query(`
|
|
|
ALTER TABLE order_person
|
|
|
ALTER COLUMN work_status
|
|
|
@@ -48,7 +54,13 @@ export class MigrateNotWorkingToPreWorking1737260000000 implements MigrationInte
|
|
|
USING work_status::text::order_person_work_status_enum
|
|
|
`);
|
|
|
|
|
|
- // 2.4 删除旧枚举类型
|
|
|
+ // 2.5 重新添加 DEFAULT 约束(使用新枚举类型和有效值)
|
|
|
+ await queryRunner.query(`
|
|
|
+ ALTER TABLE order_person
|
|
|
+ ALTER COLUMN work_status SET DEFAULT 'pre_working'
|
|
|
+ `);
|
|
|
+
|
|
|
+ // 2.6 删除旧枚举类型
|
|
|
await queryRunner.query(`
|
|
|
DROP TYPE order_person_work_status_enum_old
|
|
|
`);
|
|
|
@@ -59,19 +71,25 @@ export class MigrateNotWorkingToPreWorking1737260000000 implements MigrationInte
|
|
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
|
|
// 回滚:重新添加 not_working 值到枚举中
|
|
|
|
|
|
- // 1. 重命名当前枚举
|
|
|
+ // 1. 先移除 DEFAULT 约束
|
|
|
+ await queryRunner.query(`
|
|
|
+ ALTER TABLE order_person
|
|
|
+ ALTER COLUMN work_status DROP DEFAULT
|
|
|
+ `);
|
|
|
+
|
|
|
+ // 2. 重命名当前枚举
|
|
|
await queryRunner.query(`
|
|
|
ALTER TYPE order_person_work_status_enum
|
|
|
RENAME TO order_person_work_status_enum_old
|
|
|
`);
|
|
|
|
|
|
- // 2. 创建包含 not_working 的新枚举
|
|
|
+ // 3. 创建包含 not_working 的新枚举
|
|
|
await queryRunner.query(`
|
|
|
CREATE TYPE order_person_work_status_enum AS ENUM
|
|
|
('not_working', 'pre_working', 'working', 'resigned')
|
|
|
`);
|
|
|
|
|
|
- // 3. 修改列类型到新枚举
|
|
|
+ // 4. 修改列类型到新枚举
|
|
|
await queryRunner.query(`
|
|
|
ALTER TABLE order_person
|
|
|
ALTER COLUMN work_status
|
|
|
@@ -79,7 +97,13 @@ export class MigrateNotWorkingToPreWorking1737260000000 implements MigrationInte
|
|
|
USING work_status::text::order_person_work_status_enum
|
|
|
`);
|
|
|
|
|
|
- // 4. 删除旧枚举类型
|
|
|
+ // 5. 重新添加 DEFAULT 约束(使用 not_working 作为默认值)
|
|
|
+ await queryRunner.query(`
|
|
|
+ ALTER TABLE order_person
|
|
|
+ ALTER COLUMN work_status SET DEFAULT 'not_working'
|
|
|
+ `);
|
|
|
+
|
|
|
+ // 6. 删除旧枚举类型
|
|
|
await queryRunner.query(`
|
|
|
DROP TYPE order_person_work_status_enum_old
|
|
|
`);
|