From 7f2e4f85f85f5e2728ad9fb8b735c7fd2e2d0c18 Mon Sep 17 00:00:00 2001 From: Zack Pollard Date: Fri, 25 Jul 2025 16:51:22 +0100 Subject: [PATCH] fix: lookup the primary key constraint name before dropping it (#20221) --- ...1752161055253-RenameGeodataPKConstraint.ts | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/server/src/schema/migrations/1752161055253-RenameGeodataPKConstraint.ts b/server/src/schema/migrations/1752161055253-RenameGeodataPKConstraint.ts index 57ce86704e..086b7c1273 100644 --- a/server/src/schema/migrations/1752161055253-RenameGeodataPKConstraint.ts +++ b/server/src/schema/migrations/1752161055253-RenameGeodataPKConstraint.ts @@ -1,9 +1,23 @@ import { Kysely, sql } from 'kysely'; export async function up(db: Kysely): Promise { - await sql`ALTER TABLE "geodata_places" DROP CONSTRAINT IF EXISTS "PK_c29918988912ef4036f3d7fbff4";`.execute(db); - await sql`ALTER TABLE "geodata_places" DROP CONSTRAINT IF EXISTS "geodata_places_pkey"`.execute(db); - await sql`ALTER TABLE "geodata_places" DROP CONSTRAINT IF EXISTS "geodata_places_tmp_pkey"`.execute(db); + await sql` + DO $$ + DECLARE + constraint_name text; + BEGIN + SELECT con.conname + INTO constraint_name + FROM pg_catalog.pg_constraint con + JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid + WHERE rel.relname = 'geodata_places' AND con.contype = 'p'; + + IF constraint_name IS NOT NULL THEN + EXECUTE 'ALTER TABLE "geodata_places" DROP CONSTRAINT "' || constraint_name || '"'; + END IF; + END; + $$; + `.execute(db); await sql`ALTER TABLE "geodata_places" ADD CONSTRAINT "geodata_places_pkey" PRIMARY KEY ("id");`.execute(db); }