-
Notifications
You must be signed in to change notification settings - Fork 21
Hotfix admin deletion qf project middle table #2217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staging
Are you sure you want to change the base?
Changes from 4 commits
fd00e0a
efab150
aba6757
6595f6d
bcde46f
498acb4
1189ae0
f1d0096
066fa45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,29 +4,145 @@ | |
| name = 'AddIdToProjectQfRound1779182511001'; | ||
|
|
||
| public async up(queryRunner: QueryRunner): Promise<void> { | ||
| // Add the new id column as auto-incrementing column (not primary key) | ||
| // First, check if the table exists and get the current primary key constraint name | ||
| const tableExists = await queryRunner.hasTable( | ||
| 'project_qf_rounds_qf_round', | ||
| ); | ||
| if (!tableExists) { | ||
| throw new Error('Table project_qf_rounds_qf_round does not exist'); | ||
| } | ||
|
|
||
| // Get the current primary key constraint name | ||
| const primaryKeyQuery = await queryRunner.query(` | ||
| SELECT constraint_name | ||
| FROM information_schema.table_constraints | ||
| WHERE table_name = 'project_qf_rounds_qf_round' | ||
| AND constraint_type = 'PRIMARY KEY' | ||
| `); | ||
|
|
||
| const primaryKeyName = primaryKeyQuery[0]?.constraint_name; | ||
|
|
||
| // Drop the existing composite primary key if it exists | ||
| if (primaryKeyName) { | ||
| try { | ||
| await queryRunner.query(` | ||
| ALTER TABLE "project_qf_rounds_qf_round" | ||
| DROP CONSTRAINT "${primaryKeyName}" | ||
| `); | ||
| } catch (error) { | ||
| console.log( | ||
| `Failed to drop constraint ${primaryKeyName}, trying alternative approach...`, | ||
| ); | ||
| // Try with IF EXISTS as fallback | ||
| await queryRunner.query(` | ||
| ALTER TABLE "project_qf_rounds_qf_round" | ||
| DROP CONSTRAINT IF EXISTS "${primaryKeyName}" | ||
| `); | ||
| } | ||
| } else { | ||
| // Fallback: try to drop with constraint detection | ||
| const constraintQuery = await queryRunner.query(` | ||
| SELECT conname | ||
| FROM pg_constraint | ||
| WHERE conrelid = ( | ||
| SELECT oid | ||
| FROM pg_class | ||
| WHERE relname = 'project_qf_rounds_qf_round' | ||
| ) AND contype = 'p' | ||
| `); | ||
|
|
||
| if (constraintQuery[0]?.conname) { | ||
| await queryRunner.query(` | ||
| ALTER TABLE "project_qf_rounds_qf_round" | ||
| DROP CONSTRAINT "${constraintQuery[0].conname}" | ||
| `); | ||
| } | ||
| } | ||
|
|
||
| // Add the new id column as auto-incrementing primary key | ||
| await queryRunner.query(` | ||
| ALTER TABLE "project_qf_rounds_qf_round" | ||
| ADD COLUMN "id" SERIAL | ||
| ADD COLUMN "id" SERIAL PRIMARY KEY | ||
| `); | ||
|
|
||
| // Add index on the id column for performance | ||
| // Add unique constraint on the composite key to maintain uniqueness | ||
| await queryRunner.query(` | ||
| CREATE INDEX "IDX_project_qf_rounds_id" | ||
| ON "project_qf_rounds_qf_round" ("id") | ||
| ALTER TABLE IF EXISTS "project_qf_rounds_qf_round" | ||
| ADD CONSTRAINT "UQ_project_qf_rounds_composite" | ||
| UNIQUE ("projectId", "qfRoundId") | ||
| `); | ||
|
|
||
| // Add indexes on projectId and qfRoundId for performance | ||
| await queryRunner.query(` | ||
| CREATE INDEX "IDX_project_qf_rounds_projectId" | ||
| ON "project_qf_rounds_qf_round" ("projectId") | ||
| `); | ||
|
|
||
| await queryRunner.query(` | ||
| CREATE INDEX "IDX_project_qf_rounds_qfRoundId" | ||
| ON "project_qf_rounds_qf_round" ("qfRoundId") | ||
| `); | ||
| } | ||
|
|
||
| public async down(queryRunner: QueryRunner): Promise<void> { | ||
| // Drop the index first | ||
| // Drop the indexes first | ||
| await queryRunner.query(` | ||
| DROP INDEX IF EXISTS "IDX_project_qf_rounds_projectId" | ||
| `); | ||
|
|
||
| await queryRunner.query(` | ||
| DROP INDEX IF EXISTS "IDX_project_qf_rounds_id" | ||
| DROP INDEX IF EXISTS "IDX_project_qf_rounds_qfRoundId" | ||
| `); | ||
|
|
||
| // Drop the unique constraint | ||
| await queryRunner.query(` | ||
| ALTER TABLE IF EXISTS "project_qf_rounds_qf_round" | ||
| DROP CONSTRAINT IF EXISTS "UQ_project_qf_rounds_composite" | ||
| `); | ||
|
|
||
| // Get the current primary key constraint name for the id column | ||
| const primaryKeyQuery = await queryRunner.query(` | ||
| SELECT constraint_name | ||
| FROM information_schema.table_constraints | ||
| WHERE table_name = 'project_qf_rounds_qf_round' | ||
| AND constraint_type = 'PRIMARY KEY' | ||
| `); | ||
|
|
||
| const primaryKeyName = primaryKeyQuery[0]?.constraint_name; | ||
|
|
||
| if (primaryKeyName) { | ||
| try { | ||
| // Drop the id primary key constraint | ||
| await queryRunner.query(` | ||
| ALTER TABLE "project_qf_rounds_qf_round" | ||
| DROP CONSTRAINT "${primaryKeyName}" | ||
| `); | ||
| } catch (error) { | ||
| // Fallback with IF EXISTS | ||
| await queryRunner.query(` | ||
| ALTER TABLE "project_qf_rounds_qf_round" | ||
| DROP CONSTRAINT IF EXISTS "${primaryKeyName}" | ||
| `); | ||
| } | ||
| } else { | ||
| // Fallback: drop all possible primary key constraints | ||
| await queryRunner.query(` | ||
| ALTER TABLE "project_qf_rounds_qf_round" | ||
| DROP CONSTRAINT IF EXISTS "UQ_project_qf_rounds_qf_round" | ||
| `); | ||
| } | ||
|
|
||
| // Drop the id column | ||
| await queryRunner.query(` | ||
| ALTER TABLE "project_qf_rounds_qf_round" | ||
| ALTER TABLE IF EXISTS "project_qf_rounds_qf_round" | ||
| DROP COLUMN IF EXISTS "id" | ||
| `); | ||
|
|
||
| // Restore the composite primary key | ||
| await queryRunner.query(` | ||
| ALTER TABLE IF EXISTS "project_qf_rounds_qf_round" | ||
| ADD CONSTRAINT "PK_project_qf_rounds_qf_round" | ||
| PRIMARY KEY ("projectId", "qfRoundId") | ||
| `); | ||
|
Comment on lines
+293
to
+298
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hard-coded constraint name may differ from original. The composite primary key is restored with the hard-coded name This was previously flagged and marked as addressed in commit 6595f6d, but the hard-coded name remains. While functionally equivalent, this creates a naming inconsistency. Consider dynamically determining the original name if perfect reversibility is required. However, since constraint names don't affect behavior, this may be acceptable if you're standardizing on the new name going forward. If standardization is not the goal, you could store the original PK name before dropping it in // In up(), before dropping PKs:
const originalPK = await queryRunner.query(`
SELECT conname
FROM pg_constraint
WHERE conrelid = 'project_qf_rounds_qf_round'::regclass
AND contype = 'p'
LIMIT 1
`);
// Store originalPK[0]?.conname somewhere (e.g., migration metadata table)
// In down():
// Retrieve stored name and use it instead of hard-coded value🤖 Prompt for AI Agents |
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.