Migrate away from legacy component and piece models
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "ModelType"
|
||||
ADD COLUMN "componentSkeleton" JSONB,
|
||||
ADD COLUMN "pieceSkeleton" JSONB;
|
||||
@@ -0,0 +1,41 @@
|
||||
-- Migrate legacy component and piece models into ModelType skeletons, then drop obsolete tables
|
||||
|
||||
-- Transfer component model structures into ModelType.componentSkeleton when missing
|
||||
UPDATE "ModelType" mt
|
||||
SET "componentSkeleton" = cm."structure"
|
||||
FROM (
|
||||
SELECT DISTINCT ON ("typeComposantId")
|
||||
"typeComposantId",
|
||||
"structure"
|
||||
FROM "composant_models"
|
||||
WHERE "structure" IS NOT NULL
|
||||
ORDER BY "typeComposantId", "updatedAt" DESC, "createdAt" DESC
|
||||
) cm
|
||||
WHERE mt."id" = cm."typeComposantId"
|
||||
AND mt."componentSkeleton" IS NULL;
|
||||
|
||||
-- Transfer piece model structures into ModelType.pieceSkeleton when missing
|
||||
UPDATE "ModelType" mt
|
||||
SET "pieceSkeleton" = pm."structure"
|
||||
FROM (
|
||||
SELECT DISTINCT ON ("typePieceId")
|
||||
"typePieceId",
|
||||
"structure"
|
||||
FROM "piece_models"
|
||||
WHERE "structure" IS NOT NULL
|
||||
ORDER BY "typePieceId", "updatedAt" DESC, "createdAt" DESC
|
||||
) pm
|
||||
WHERE mt."id" = pm."typePieceId"
|
||||
AND mt."pieceSkeleton" IS NULL;
|
||||
|
||||
-- Drop foreign keys before removing the legacy columns
|
||||
ALTER TABLE "composants" DROP CONSTRAINT IF EXISTS "composants_composantModelId_fkey";
|
||||
ALTER TABLE "pieces" DROP CONSTRAINT IF EXISTS "pieces_pieceModelId_fkey";
|
||||
|
||||
-- Remove columns referencing the legacy model tables
|
||||
ALTER TABLE "composants" DROP COLUMN IF EXISTS "composantModelId";
|
||||
ALTER TABLE "pieces" DROP COLUMN IF EXISTS "pieceModelId";
|
||||
|
||||
-- Drop obsolete model tables
|
||||
DROP TABLE IF EXISTS "composant_models";
|
||||
DROP TABLE IF EXISTS "piece_models";
|
||||
Reference in New Issue
Block a user