Expand machine hydration unit coverage
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
-- Drop old foreign keys linking components and pieces directly to machines
|
||||
ALTER TABLE "composants" DROP CONSTRAINT IF EXISTS "composants_machineId_fkey";
|
||||
ALTER TABLE "composants" DROP CONSTRAINT IF EXISTS "composants_parentComposantId_fkey";
|
||||
ALTER TABLE "composants" DROP CONSTRAINT IF EXISTS "composants_typeMachineComponentRequirementId_fkey";
|
||||
ALTER TABLE "pieces" DROP CONSTRAINT IF EXISTS "pieces_machineId_fkey";
|
||||
ALTER TABLE "pieces" DROP CONSTRAINT IF EXISTS "pieces_composantId_fkey";
|
||||
ALTER TABLE "pieces" DROP CONSTRAINT IF EXISTS "pieces_typeMachinePieceRequirementId_fkey";
|
||||
|
||||
-- Create new link tables to associate machines with components and pieces
|
||||
CREATE TABLE "machine_component_links" (
|
||||
"id" TEXT NOT NULL,
|
||||
"machineId" TEXT NOT NULL,
|
||||
"composantId" TEXT NOT NULL,
|
||||
"parentLinkId" TEXT,
|
||||
"typeMachineComponentRequirementId" TEXT,
|
||||
"nameOverride" TEXT,
|
||||
"referenceOverride" TEXT,
|
||||
"prixOverride" DECIMAL(10,2),
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
CONSTRAINT "machine_component_links_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE TABLE "machine_piece_links" (
|
||||
"id" TEXT NOT NULL,
|
||||
"machineId" TEXT NOT NULL,
|
||||
"pieceId" TEXT NOT NULL,
|
||||
"parentLinkId" TEXT,
|
||||
"typeMachinePieceRequirementId" TEXT,
|
||||
"nameOverride" TEXT,
|
||||
"referenceOverride" TEXT,
|
||||
"prixOverride" DECIMAL(10,2),
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
CONSTRAINT "machine_piece_links_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- Seed the new link tables using the existing component and piece assignments
|
||||
INSERT INTO "machine_component_links" (
|
||||
"id",
|
||||
"machineId",
|
||||
"composantId",
|
||||
"typeMachineComponentRequirementId",
|
||||
"createdAt",
|
||||
"updatedAt"
|
||||
)
|
||||
SELECT
|
||||
"machineId" || '_' || "id" AS "id",
|
||||
"machineId",
|
||||
"id" AS "composantId",
|
||||
"typeMachineComponentRequirementId",
|
||||
"createdAt",
|
||||
"updatedAt"
|
||||
FROM "composants"
|
||||
WHERE "machineId" IS NOT NULL;
|
||||
|
||||
UPDATE "machine_component_links" AS link
|
||||
SET "parentLinkId" = link."machineId" || '_' || c."parentComposantId"
|
||||
FROM "composants" AS c
|
||||
WHERE link."composantId" = c."id"
|
||||
AND c."parentComposantId" IS NOT NULL;
|
||||
|
||||
INSERT INTO "machine_piece_links" (
|
||||
"id",
|
||||
"machineId",
|
||||
"pieceId",
|
||||
"parentLinkId",
|
||||
"typeMachinePieceRequirementId",
|
||||
"createdAt",
|
||||
"updatedAt"
|
||||
)
|
||||
SELECT
|
||||
"machineId" || '_' || "id" AS "id",
|
||||
"machineId",
|
||||
"id" AS "pieceId",
|
||||
CASE WHEN "composantId" IS NOT NULL THEN "machineId" || '_' || "composantId" ELSE NULL END,
|
||||
"typeMachinePieceRequirementId",
|
||||
"createdAt",
|
||||
"updatedAt"
|
||||
FROM "pieces"
|
||||
WHERE "machineId" IS NOT NULL;
|
||||
|
||||
-- Remove the obsolete columns now that the data has been migrated
|
||||
ALTER TABLE "composants" DROP COLUMN IF EXISTS "machineId";
|
||||
ALTER TABLE "composants" DROP COLUMN IF EXISTS "parentComposantId";
|
||||
ALTER TABLE "composants" DROP COLUMN IF EXISTS "typeMachineComponentRequirementId";
|
||||
|
||||
ALTER TABLE "pieces" DROP COLUMN IF EXISTS "machineId";
|
||||
ALTER TABLE "pieces" DROP COLUMN IF EXISTS "composantId";
|
||||
ALTER TABLE "pieces" DROP COLUMN IF EXISTS "typeMachinePieceRequirementId";
|
||||
|
||||
-- Add the new foreign key constraints for the link tables
|
||||
ALTER TABLE "machine_component_links"
|
||||
ADD CONSTRAINT "machine_component_links_machineId_fkey"
|
||||
FOREIGN KEY ("machineId") REFERENCES "machines"("id") ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
ADD CONSTRAINT "machine_component_links_composantId_fkey"
|
||||
FOREIGN KEY ("composantId") REFERENCES "composants"("id") ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
ADD CONSTRAINT "machine_component_links_parentLinkId_fkey"
|
||||
FOREIGN KEY ("parentLinkId") REFERENCES "machine_component_links"("id") ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
ADD CONSTRAINT "machine_component_links_typeMachineComponentRequirementId_fkey"
|
||||
FOREIGN KEY ("typeMachineComponentRequirementId") REFERENCES "type_machine_component_requirements"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "machine_piece_links"
|
||||
ADD CONSTRAINT "machine_piece_links_machineId_fkey"
|
||||
FOREIGN KEY ("machineId") REFERENCES "machines"("id") ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
ADD CONSTRAINT "machine_piece_links_pieceId_fkey"
|
||||
FOREIGN KEY ("pieceId") REFERENCES "pieces"("id") ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
ADD CONSTRAINT "machine_piece_links_parentLinkId_fkey"
|
||||
FOREIGN KEY ("parentLinkId") REFERENCES "machine_component_links"("id") ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
ADD CONSTRAINT "machine_piece_links_typeMachinePieceRequirementId_fkey"
|
||||
FOREIGN KEY ("typeMachinePieceRequirementId") REFERENCES "type_machine_piece_requirements"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
@@ -52,12 +52,12 @@ model TypeMachine {
|
||||
}
|
||||
|
||||
model Machine {
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
reference String?
|
||||
prix Decimal? @db.Decimal(10, 2)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
reference String?
|
||||
prix Decimal? @db.Decimal(10, 2)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
// Relations
|
||||
siteId String
|
||||
@@ -69,94 +69,114 @@ model Machine {
|
||||
constructeurId String?
|
||||
constructeur Constructeur? @relation(fields: [constructeurId], references: [id], onDelete: SetNull)
|
||||
|
||||
composants Composant[]
|
||||
pieces Piece[]
|
||||
documents Document[] @relation("MachineDocuments")
|
||||
customFieldValues CustomFieldValue[] @relation("MachineCustomFieldValues")
|
||||
componentLinks MachineComponentLink[]
|
||||
pieceLinks MachinePieceLink[]
|
||||
documents Document[] @relation("MachineDocuments")
|
||||
customFieldValues CustomFieldValue[] @relation("MachineCustomFieldValues")
|
||||
|
||||
@@map("machines")
|
||||
}
|
||||
|
||||
model Composant {
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
reference String?
|
||||
prix Decimal? @db.Decimal(10, 2)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
// Relations hiérarchiques
|
||||
machineId String?
|
||||
machine Machine? @relation(fields: [machineId], references: [id], onDelete: Cascade)
|
||||
|
||||
parentComposantId String?
|
||||
parentComposant Composant? @relation("ComposantHierarchy", fields: [parentComposantId], references: [id], onDelete: Cascade)
|
||||
sousComposants Composant[] @relation("ComposantHierarchy")
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
reference String?
|
||||
prix Decimal? @db.Decimal(10, 2)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
typeComposantId String?
|
||||
typeComposant ModelType? @relation("ModelTypeComponentAssignments", fields: [typeComposantId], references: [id])
|
||||
|
||||
typeMachineComponentRequirementId String?
|
||||
typeMachineComponentRequirement TypeMachineComponentRequirement? @relation(fields: [typeMachineComponentRequirementId], references: [id], onDelete: SetNull)
|
||||
|
||||
constructeurId String?
|
||||
constructeur Constructeur? @relation(fields: [constructeurId], references: [id], onDelete: SetNull)
|
||||
|
||||
pieces Piece[]
|
||||
documents Document[] @relation("ComposantDocuments")
|
||||
customFieldValues CustomFieldValue[] @relation("ComposantCustomFieldValues")
|
||||
documents Document[] @relation("ComposantDocuments")
|
||||
customFieldValues CustomFieldValue[] @relation("ComposantCustomFieldValues")
|
||||
machineLinks MachineComponentLink[]
|
||||
|
||||
@@map("composants")
|
||||
}
|
||||
|
||||
model Piece {
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
reference String?
|
||||
prix Decimal? @db.Decimal(10, 2)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
// Relations
|
||||
machineId String?
|
||||
machine Machine? @relation(fields: [machineId], references: [id], onDelete: Cascade)
|
||||
|
||||
composantId String?
|
||||
composant Composant? @relation(fields: [composantId], references: [id], onDelete: Cascade)
|
||||
id String @id @default(cuid())
|
||||
name String
|
||||
reference String?
|
||||
prix Decimal? @db.Decimal(10, 2)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
typePieceId String?
|
||||
typePiece ModelType? @relation("ModelTypePieceAssignments", fields: [typePieceId], references: [id])
|
||||
|
||||
typeMachinePieceRequirementId String?
|
||||
typeMachinePieceRequirement TypeMachinePieceRequirement? @relation(fields: [typeMachinePieceRequirementId], references: [id], onDelete: SetNull)
|
||||
|
||||
constructeurId String?
|
||||
constructeur Constructeur? @relation(fields: [constructeurId], references: [id], onDelete: SetNull)
|
||||
|
||||
documents Document[] @relation("PieceDocuments")
|
||||
customFieldValues CustomFieldValue[] @relation("PieceCustomFieldValues")
|
||||
machineLinks MachinePieceLink[]
|
||||
|
||||
@@map("pieces")
|
||||
}
|
||||
|
||||
model MachineComponentLink {
|
||||
id String @id @default(cuid())
|
||||
machineId String
|
||||
composantId String
|
||||
parentLinkId String?
|
||||
typeMachineComponentRequirementId String?
|
||||
nameOverride String?
|
||||
referenceOverride String?
|
||||
prixOverride Decimal? @db.Decimal(10, 2)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
machine Machine @relation(fields: [machineId], references: [id], onDelete: Cascade)
|
||||
composant Composant @relation(fields: [composantId], references: [id], onDelete: Cascade)
|
||||
parentLink MachineComponentLink? @relation("MachineComponentLinkHierarchy", fields: [parentLinkId], references: [id], onDelete: Cascade)
|
||||
childLinks MachineComponentLink[] @relation("MachineComponentLinkHierarchy")
|
||||
typeMachineComponentRequirement TypeMachineComponentRequirement? @relation("ComponentRequirementLinks", fields: [typeMachineComponentRequirementId], references: [id], onDelete: SetNull)
|
||||
pieceLinks MachinePieceLink[] @relation("ComponentLinkPieceLinks")
|
||||
|
||||
@@map("machine_component_links")
|
||||
}
|
||||
|
||||
model MachinePieceLink {
|
||||
id String @id @default(cuid())
|
||||
machineId String
|
||||
pieceId String
|
||||
parentLinkId String?
|
||||
typeMachinePieceRequirementId String?
|
||||
nameOverride String?
|
||||
referenceOverride String?
|
||||
prixOverride Decimal? @db.Decimal(10, 2)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
machine Machine @relation(fields: [machineId], references: [id], onDelete: Cascade)
|
||||
piece Piece @relation(fields: [pieceId], references: [id], onDelete: Cascade)
|
||||
parentLink MachineComponentLink? @relation("ComponentLinkPieceLinks", fields: [parentLinkId], references: [id], onDelete: Cascade)
|
||||
typeMachinePieceRequirement TypeMachinePieceRequirement? @relation("PieceRequirementLinks", fields: [typeMachinePieceRequirementId], references: [id], onDelete: SetNull)
|
||||
|
||||
@@map("machine_piece_links")
|
||||
}
|
||||
|
||||
enum ModelCategory {
|
||||
COMPONENT
|
||||
PIECE
|
||||
}
|
||||
|
||||
model ModelType {
|
||||
id String @id @default(cuid())
|
||||
name String @db.VarChar(120)
|
||||
code String @unique @db.VarChar(60)
|
||||
category ModelCategory
|
||||
notes String? @db.Text
|
||||
description String? @db.Text
|
||||
id String @id @default(cuid())
|
||||
name String @db.VarChar(120)
|
||||
code String @unique @db.VarChar(60)
|
||||
category ModelCategory
|
||||
notes String? @db.Text
|
||||
description String? @db.Text
|
||||
componentSkeleton Json?
|
||||
pieceSkeleton Json?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index([category, name])
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
composants Composant[] @relation("ModelTypeComponentAssignments")
|
||||
componentRequirements TypeMachineComponentRequirement[] @relation("ModelTypeComponentRequirements")
|
||||
@@ -164,6 +184,8 @@ model ModelType {
|
||||
pieceRequirements TypeMachinePieceRequirement[] @relation("ModelTypePieceRequirements")
|
||||
pieces Piece[] @relation("ModelTypePieceAssignments")
|
||||
pieceCustomFields CustomField[] @relation("ModelTypePieceCustomFields")
|
||||
|
||||
@@index([category, name])
|
||||
}
|
||||
|
||||
model Constructeur {
|
||||
@@ -266,7 +288,6 @@ model CustomFieldValue {
|
||||
@@map("custom_field_values")
|
||||
}
|
||||
|
||||
|
||||
model TypeMachineComponentRequirement {
|
||||
id String @id @default(cuid())
|
||||
label String?
|
||||
@@ -283,7 +304,7 @@ model TypeMachineComponentRequirement {
|
||||
typeComposantId String
|
||||
typeComposant ModelType @relation("ModelTypeComponentRequirements", fields: [typeComposantId], references: [id])
|
||||
|
||||
composants Composant[]
|
||||
machineComponentLinks MachineComponentLink[] @relation("ComponentRequirementLinks")
|
||||
|
||||
@@map("type_machine_component_requirements")
|
||||
}
|
||||
@@ -304,7 +325,7 @@ model TypeMachinePieceRequirement {
|
||||
typePieceId String
|
||||
typePiece ModelType @relation("ModelTypePieceRequirements", fields: [typePieceId], references: [id])
|
||||
|
||||
pieces Piece[]
|
||||
machinePieceLinks MachinePieceLink[] @relation("PieceRequirementLinks")
|
||||
|
||||
@@map("type_machine_piece_requirements")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user