Migrate away from legacy component and piece models

This commit is contained in:
MatthieuTD
2025-10-02 15:44:02 +02:00
parent 44fd4bb8c7
commit c23ba3a587
34 changed files with 1821 additions and 1825 deletions

View File

@@ -0,0 +1,4 @@
-- AlterTable
ALTER TABLE "ModelType"
ADD COLUMN "componentSkeleton" JSONB,
ADD COLUMN "pieceSkeleton" JSONB;

View File

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