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

View File

@@ -96,9 +96,6 @@ model Composant {
typeComposantId String?
typeComposant ModelType? @relation("ModelTypeComponentAssignments", fields: [typeComposantId], references: [id])
composantModelId String?
composantModel ComposantModel? @relation(fields: [composantModelId], references: [id], onDelete: SetNull)
typeMachineComponentRequirementId String?
typeMachineComponentRequirement TypeMachineComponentRequirement? @relation(fields: [typeMachineComponentRequirementId], references: [id], onDelete: SetNull)
@@ -130,9 +127,6 @@ model Piece {
typePieceId String?
typePiece ModelType? @relation("ModelTypePieceAssignments", fields: [typePieceId], references: [id])
pieceModelId String?
pieceModel PieceModel? @relation(fields: [pieceModelId], references: [id], onDelete: SetNull)
typeMachinePieceRequirementId String?
typeMachinePieceRequirement TypeMachinePieceRequirement? @relation(fields: [typeMachinePieceRequirementId], references: [id], onDelete: SetNull)
@@ -157,16 +151,16 @@ model ModelType {
category ModelCategory
notes String? @db.Text
description String? @db.Text
componentSkeleton Json?
pieceSkeleton Json?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@index([category, name])
composants Composant[] @relation("ModelTypeComponentAssignments")
models ComposantModel[] @relation("ModelTypeComponentModels")
componentRequirements TypeMachineComponentRequirement[] @relation("ModelTypeComponentRequirements")
customFields CustomField[] @relation("ModelTypeCustomFields")
pieceModels PieceModel[] @relation("ModelTypePieceModels")
pieceRequirements TypeMachinePieceRequirement[] @relation("ModelTypePieceRequirements")
pieces Piece[] @relation("ModelTypePieceAssignments")
pieceCustomFields CustomField[] @relation("ModelTypePieceCustomFields")
@@ -272,37 +266,6 @@ model CustomFieldValue {
@@map("custom_field_values")
}
model ComposantModel {
id String @id @default(cuid())
name String
description String?
structure Json? // Définition du composant (sous-composants, pièces, champs personnalisés)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
typeComposantId String
typeComposant ModelType @relation("ModelTypeComponentModels", fields: [typeComposantId], references: [id], onDelete: Cascade)
composants Composant[]
@@map("composant_models")
}
model PieceModel {
id String @id @default(cuid())
name String
description String?
structure Json? // Définition de la pièce (champs personnalisés par défaut, etc.)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
typePieceId String
typePiece ModelType @relation("ModelTypePieceModels", fields: [typePieceId], references: [id], onDelete: Cascade)
pieces Piece[]
@@map("piece_models")
}
model TypeMachineComponentRequirement {
id String @id @default(cuid())