From 208d49aac803da770c7c6bb02e44e991c12961ee Mon Sep 17 00:00:00 2001 From: Matthieu Date: Thu, 22 Jan 2026 11:51:57 +0100 Subject: [PATCH] fix: use DELETE instead of TRUNCATE for migration Use DELETE instead of TRUNCATE to avoid requiring table ownership. Wrap in DO block for better error handling and logging. Co-Authored-By: Claude Sonnet 4.5 --- .../migration.sql | 60 ++++++++++++------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/prisma/migrations/20260122_fix_product_constructeurs_orientation/migration.sql b/prisma/migrations/20260122_fix_product_constructeurs_orientation/migration.sql index fe12805..3b6ed86 100644 --- a/prisma/migrations/20260122_fix_product_constructeurs_orientation/migration.sql +++ b/prisma/migrations/20260122_fix_product_constructeurs_orientation/migration.sql @@ -2,28 +2,48 @@ -- Issue: Table was created with A=product, B=constructeur -- But Prisma expects alphabetical order: A=constructeur, B=product --- Step 1: Save existing data to temp table -CREATE TEMP TABLE _ProductConstructeurs_backup AS -SELECT "A" as old_A, "B" as old_B FROM "_ProductConstructeurs"; +-- This migration swaps the A and B columns to fix the orientation --- Step 2: Drop foreign key constraints -ALTER TABLE "_ProductConstructeurs" DROP CONSTRAINT IF EXISTS "_ProductConstructeurs_A_fkey"; -ALTER TABLE "_ProductConstructeurs" DROP CONSTRAINT IF EXISTS "_ProductConstructeurs_B_fkey"; +DO $$ +DECLARE + backup_count INTEGER; +BEGIN + -- Step 1: Create temp table with swapped data + CREATE TEMP TABLE IF NOT EXISTS _ProductConstructeurs_backup AS + SELECT "B" as new_A, "A" as new_B FROM "_ProductConstructeurs"; --- Step 3: Clear the table -TRUNCATE TABLE "_ProductConstructeurs"; + GET DIAGNOSTICS backup_count = ROW_COUNT; + RAISE NOTICE 'Backed up % rows', backup_count; --- Step 4: Reinsert data with swapped columns (A and B inverted) -INSERT INTO "_ProductConstructeurs" ("A", "B") -SELECT old_B, old_A FROM _ProductConstructeurs_backup; + -- Step 2: Drop foreign key constraints + ALTER TABLE "_ProductConstructeurs" DROP CONSTRAINT IF EXISTS "_ProductConstructeurs_A_fkey"; + ALTER TABLE "_ProductConstructeurs" DROP CONSTRAINT IF EXISTS "_ProductConstructeurs_B_fkey"; --- Step 5: Recreate foreign key constraints with correct orientation -ALTER TABLE "_ProductConstructeurs" - ADD CONSTRAINT "_ProductConstructeurs_A_fkey" - FOREIGN KEY ("A") REFERENCES "constructeurs"("id") - ON DELETE CASCADE ON UPDATE CASCADE; + RAISE NOTICE 'Dropped foreign key constraints'; -ALTER TABLE "_ProductConstructeurs" - ADD CONSTRAINT "_ProductConstructeurs_B_fkey" - FOREIGN KEY ("B") REFERENCES "products"("id") - ON DELETE CASCADE ON UPDATE CASCADE; + -- Step 3: Clear the table + DELETE FROM "_ProductConstructeurs"; + + RAISE NOTICE 'Cleared table'; + + -- Step 4: Reinsert data with swapped columns + INSERT INTO "_ProductConstructeurs" ("A", "B") + SELECT new_A, new_B FROM _ProductConstructeurs_backup; + + GET DIAGNOSTICS backup_count = ROW_COUNT; + RAISE NOTICE 'Reinserted % rows with swapped columns', backup_count; + + -- Step 5: Recreate foreign key constraints with correct orientation + ALTER TABLE "_ProductConstructeurs" + ADD CONSTRAINT "_ProductConstructeurs_A_fkey" + FOREIGN KEY ("A") REFERENCES "constructeurs"("id") + ON DELETE CASCADE ON UPDATE CASCADE; + + ALTER TABLE "_ProductConstructeurs" + ADD CONSTRAINT "_ProductConstructeurs_B_fkey" + FOREIGN KEY ("B") REFERENCES "products"("id") + ON DELETE CASCADE ON UPDATE CASCADE; + + RAISE NOTICE 'Recreated foreign key constraints'; + RAISE NOTICE 'Migration completed successfully'; +END $$;