From cb6c541ae17b91d8fe30f7dccc56ba9606c45cd4 Mon Sep 17 00:00:00 2001 From: Zack Pollard Date: Fri, 9 May 2025 14:45:44 +0100 Subject: [PATCH] fix: constraint migration to handle any existing pkey name (#18178) --- .../migrations/1746636476623-DropExtraIndexes.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/server/src/schema/migrations/1746636476623-DropExtraIndexes.ts b/server/src/schema/migrations/1746636476623-DropExtraIndexes.ts index aae52829a56..1518593428a 100644 --- a/server/src/schema/migrations/1746636476623-DropExtraIndexes.ts +++ b/server/src/schema/migrations/1746636476623-DropExtraIndexes.ts @@ -4,8 +4,16 @@ export async function up(db: Kysely): Promise { const { rows } = await sql<{ db: string }>`SELECT current_database() as db`.execute(db); const databaseName = rows[0].db; await sql.raw(`ALTER DATABASE "${databaseName}" SET search_path TO "$user", public, vectors`).execute(db); - await sql`ALTER TABLE "naturalearth_countries" DROP CONSTRAINT IF EXISTS "PK_21a6d86d1ab5d841648212e5353";`.execute(db); - await sql`ALTER TABLE "naturalearth_countries" DROP CONSTRAINT IF EXISTS "naturalearth_countries_pkey";`.execute(db); + const naturalearth_pkey = await sql<{ constraint_name: string }>`SELECT constraint_name + FROM information_schema.table_constraints + WHERE table_schema = 'public' + AND table_name = 'naturalearth_countries' + AND constraint_type = 'PRIMARY KEY';`.execute(db); + const naturalearth_pkey_name = naturalearth_pkey.rows[0]?.constraint_name; + if(naturalearth_pkey_name) { + await sql`ALTER TABLE "naturalearth_countries" + DROP CONSTRAINT ${sql.ref(naturalearth_pkey_name)};`.execute(db); + } await sql`ALTER TABLE "naturalearth_countries" ADD CONSTRAINT "naturalearth_countries_pkey" PRIMARY KEY ("id") WITH (FILLFACTOR = 100);`.execute(db); await sql`DROP INDEX IF EXISTS "IDX_02a43fd0b3c50fb6d7f0cb7282";`.execute(db); await sql`DROP INDEX IF EXISTS "IDX_95ad7106dd7b484275443f580f";`.execute(db);