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:
@@ -2,28 +2,48 @@
|
|||||||
-- Issue: Table was created with A=product, B=constructeur
|
-- Issue: Table was created with A=product, B=constructeur
|
||||||
-- But Prisma expects alphabetical order: A=constructeur, B=product
|
-- But Prisma expects alphabetical order: A=constructeur, B=product
|
||||||
|
|
||||||
-- Step 1: Save existing data to temp table
|
-- This migration swaps the A and B columns to fix the orientation
|
||||||
CREATE TEMP TABLE _ProductConstructeurs_backup AS
|
|
||||||
SELECT "A" as old_A, "B" as old_B FROM "_ProductConstructeurs";
|
|
||||||
|
|
||||||
-- Step 2: Drop foreign key constraints
|
DO $$
|
||||||
ALTER TABLE "_ProductConstructeurs" DROP CONSTRAINT IF EXISTS "_ProductConstructeurs_A_fkey";
|
DECLARE
|
||||||
ALTER TABLE "_ProductConstructeurs" DROP CONSTRAINT IF EXISTS "_ProductConstructeurs_B_fkey";
|
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
|
GET DIAGNOSTICS backup_count = ROW_COUNT;
|
||||||
TRUNCATE TABLE "_ProductConstructeurs";
|
RAISE NOTICE 'Backed up % rows', backup_count;
|
||||||
|
|
||||||
-- Step 4: Reinsert data with swapped columns (A and B inverted)
|
-- Step 2: Drop foreign key constraints
|
||||||
INSERT INTO "_ProductConstructeurs" ("A", "B")
|
ALTER TABLE "_ProductConstructeurs" DROP CONSTRAINT IF EXISTS "_ProductConstructeurs_A_fkey";
|
||||||
SELECT old_B, old_A FROM _ProductConstructeurs_backup;
|
ALTER TABLE "_ProductConstructeurs" DROP CONSTRAINT IF EXISTS "_ProductConstructeurs_B_fkey";
|
||||||
|
|
||||||
-- Step 5: Recreate foreign key constraints with correct orientation
|
RAISE NOTICE 'Dropped foreign key constraints';
|
||||||
ALTER TABLE "_ProductConstructeurs"
|
|
||||||
ADD CONSTRAINT "_ProductConstructeurs_A_fkey"
|
|
||||||
FOREIGN KEY ("A") REFERENCES "constructeurs"("id")
|
|
||||||
ON DELETE CASCADE ON UPDATE CASCADE;
|
|
||||||
|
|
||||||
ALTER TABLE "_ProductConstructeurs"
|
-- Step 3: Clear the table
|
||||||
ADD CONSTRAINT "_ProductConstructeurs_B_fkey"
|
DELETE FROM "_ProductConstructeurs";
|
||||||
FOREIGN KEY ("B") REFERENCES "products"("id")
|
|
||||||
ON DELETE CASCADE ON UPDATE CASCADE;
|
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 $$;
|
||||||
|
|||||||
Reference in New Issue
Block a user