@@ -4,55 +4,84 @@ export class AddIdToProjectQfRound1779182511001 implements MigrationInterface {
44 name = 'AddIdToProjectQfRound1779182511001' ;
55
66 public async up ( queryRunner : QueryRunner ) : Promise < void > {
7- // First, check if the table exists and get the current primary key constraint name
8- const tableExists = await queryRunner . hasTable ( 'project_qf_rounds_qf_round' ) ;
9- if ( ! tableExists ) {
10- throw new Error ( 'Table project_qf_rounds_qf_round does not exist' ) ;
7+ // Try to drop primary key constraints with common names first
8+ const commonConstraintNames = [
9+ 'pk_project_qf_rounds_qf_round' ,
10+ 'project_qf_rounds_qf_round_pkey' ,
11+ 'PK_project_qf_rounds_qf_round' ,
12+ ] ;
13+
14+ for ( const constraintName of commonConstraintNames ) {
15+ try {
16+ await queryRunner . query ( `
17+ ALTER TABLE "project_qf_rounds_qf_round"
18+ DROP CONSTRAINT IF EXISTS "${ constraintName } "
19+ ` ) ;
20+ } catch ( error ) {
21+ // Continue to next constraint name if this fails
22+ continue ;
23+ }
1124 }
1225
13- // Get the current primary key constraint name
14- const primaryKeyQuery = await queryRunner . query ( `
15- SELECT constraint_name
16- FROM information_schema.table_constraints
17- WHERE table_name = 'project_qf_rounds_qf_round'
18- AND constraint_type = 'PRIMARY KEY'
19- ` ) ;
20-
21- const primaryKeyName = primaryKeyQuery [ 0 ] ?. constraint_name || 'PK_project_qf_rounds_qf_round' ;
22-
23- // Drop the existing composite primary key
24- await queryRunner . query ( `
25- ALTER TABLE "project_qf_rounds_qf_round"
26- DROP CONSTRAINT "${ primaryKeyName } "
27- ` ) ;
26+ // Then try to find and drop any remaining primary key constraints
27+ try {
28+ await queryRunner . query ( `
29+ DO $$
30+ DECLARE
31+ constraint_name TEXT;
32+ BEGIN
33+ -- Get all primary key constraint names for this table
34+ SELECT conname INTO constraint_name
35+ FROM pg_constraint c
36+ JOIN pg_class t ON c.conrelid = t.oid
37+ WHERE t.relname = 'project_qf_rounds_qf_round'
38+ AND c.contype = 'p'
39+ LIMIT 1;
40+
41+ IF constraint_name IS NOT NULL THEN
42+ -- Try a direct drop first
43+ BEGIN
44+ EXECUTE 'ALTER TABLE project_qf_rounds_qf_round DROP CONSTRAINT ' || constraint_name;
45+ EXCEPTION
46+ WHEN OTHERS THEN
47+ -- If it fails, just continue - constraint might not exist
48+ NULL;
49+ END;
50+ END IF;
51+ END $$;
52+ ` ) ;
53+ } catch ( error ) {
54+ // If the whole DO block fails, continue - table might not have constraints
55+ // silently continue
56+ }
2857
29- // Add the new id column as auto-incrementing primary key
58+ // Add id column as primary key
3059 await queryRunner . query ( `
31- ALTER TABLE "project_qf_rounds_qf_round"
32- ADD COLUMN "id" SERIAL PRIMARY KEY
60+ ALTER TABLE IF EXISTS "project_qf_rounds_qf_round"
61+ ADD COLUMN IF NOT EXISTS "id" SERIAL PRIMARY KEY
3362 ` ) ;
3463
35- // Add unique constraint on the composite key to maintain uniqueness
64+ // Add unique constraint on projectId and qfRoundId
3665 await queryRunner . query ( `
3766 ALTER TABLE IF EXISTS "project_qf_rounds_qf_round"
38- ADD CONSTRAINT "UQ_project_qf_rounds_composite"
67+ ADD CONSTRAINT IF NOT EXISTS "UQ_project_qf_rounds_composite"
3968 UNIQUE ("projectId", "qfRoundId")
4069 ` ) ;
4170
42- // Add indexes on projectId and qfRoundId for performance
71+ // Add indexes
4372 await queryRunner . query ( `
44- CREATE INDEX "IDX_project_qf_rounds_projectId"
73+ CREATE INDEX IF NOT EXISTS "IDX_project_qf_rounds_projectId"
4574 ON "project_qf_rounds_qf_round" ("projectId")
4675 ` ) ;
4776
4877 await queryRunner . query ( `
49- CREATE INDEX "IDX_project_qf_rounds_qfRoundId"
78+ CREATE INDEX IF NOT EXISTS "IDX_project_qf_rounds_qfRoundId"
5079 ON "project_qf_rounds_qf_round" ("qfRoundId")
5180 ` ) ;
5281 }
5382
5483 public async down ( queryRunner : QueryRunner ) : Promise < void > {
55- // Drop the indexes first
84+ // Drop indexes
5685 await queryRunner . query ( `
5786 DROP INDEX IF EXISTS "IDX_project_qf_rounds_projectId"
5887 ` ) ;
@@ -61,39 +90,21 @@ export class AddIdToProjectQfRound1779182511001 implements MigrationInterface {
6190 DROP INDEX IF EXISTS "IDX_project_qf_rounds_qfRoundId"
6291 ` ) ;
6392
64- // Drop the unique constraint
93+ // Drop unique constraint
6594 await queryRunner . query ( `
66- ALTER TABLE IF EXISTS "project_qf_rounds_qf_round"
95+ ALTER TABLE "project_qf_rounds_qf_round"
6796 DROP CONSTRAINT IF EXISTS "UQ_project_qf_rounds_composite"
6897 ` ) ;
6998
70- // Get the current primary key constraint name for the id column
71- const primaryKeyQuery = await queryRunner . query ( `
72- SELECT constraint_name
73- FROM information_schema.table_constraints
74- WHERE table_name = 'project_qf_rounds_qf_round'
75- AND constraint_type = 'PRIMARY KEY'
76- ` ) ;
77-
78- const primaryKeyName = primaryKeyQuery [ 0 ] ?. constraint_name ;
79-
80- if ( primaryKeyName ) {
81- // Drop the id primary key constraint
82- await queryRunner . query ( `
83- ALTER TABLE IF EXISTS "project_qf_rounds_qf_round"
84- DROP CONSTRAINT "${ primaryKeyName } "
85- ` ) ;
86- }
87-
88- // Drop the id column
99+ // Drop id column (which includes dropping its primary key constraint)
89100 await queryRunner . query ( `
90- ALTER TABLE IF EXISTS "project_qf_rounds_qf_round"
101+ ALTER TABLE "project_qf_rounds_qf_round"
91102 DROP COLUMN IF EXISTS "id"
92103 ` ) ;
93104
94- // Restore the composite primary key
105+ // Restore composite primary key
95106 await queryRunner . query ( `
96- ALTER TABLE IF EXISTS "project_qf_rounds_qf_round"
107+ ALTER TABLE "project_qf_rounds_qf_round"
97108 ADD CONSTRAINT "PK_project_qf_rounds_qf_round"
98109 PRIMARY KEY ("projectId", "qfRoundId")
99110 ` ) ;
0 commit comments