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 <noreply@anthropic.com>
This commit is contained in:
Matthieu
2026-01-22 11:51:57 +01:00
parent ff278f5549
commit 208d49aac8

View File

@@ -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 $$;