feat(constructeurs): introduce constructors management

This commit is contained in:
Matthieu
2025-09-17 15:09:54 +02:00
parent 339f46ec24
commit 83251b532c
13 changed files with 335 additions and 14 deletions

View File

@@ -0,0 +1,78 @@
-- Create constructeurs table
CREATE TABLE "constructeurs" (
"id" TEXT PRIMARY KEY,
"name" TEXT NOT NULL,
"email" TEXT,
"phone" TEXT,
"createdAt" TIMESTAMP NOT NULL DEFAULT NOW(),
"updatedAt" TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE UNIQUE INDEX "constructeurs_name_unique_idx" ON "constructeurs"(LOWER("name"));
-- Add foreign key columns to machines, composants, pieces
ALTER TABLE "machines" ADD COLUMN "constructeurId" TEXT;
ALTER TABLE "composants" ADD COLUMN "constructeurId" TEXT;
ALTER TABLE "pieces" ADD COLUMN "constructeurId" TEXT;
-- Populate constructeurs from existing data
WITH existing_names AS (
SELECT DISTINCT TRIM(constructeur) AS name FROM "machines" WHERE constructeur IS NOT NULL AND TRIM(constructeur) <> ''
UNION
SELECT DISTINCT TRIM(constructeur) FROM "composants" WHERE constructeur IS NOT NULL AND TRIM(constructeur) <> ''
UNION
SELECT DISTINCT TRIM(constructeur) FROM "pieces" WHERE constructeur IS NOT NULL AND TRIM(constructeur) <> ''
),
prepared AS (
SELECT DISTINCT ON (LOWER(name)) name
FROM existing_names
WHERE name IS NOT NULL AND name <> ''
ORDER BY LOWER(name), name
)
INSERT INTO "constructeurs" ("id", "name")
SELECT substr(md5(random()::text || clock_timestamp()::text), 1, 25), name
FROM prepared
ON CONFLICT (LOWER("name")) DO NOTHING;
-- Link existing records to constructeurs
UPDATE "machines" m
SET "constructeurId" = c.id
FROM "constructeurs" c
WHERE m.constructeur IS NOT NULL
AND TRIM(m.constructeur) <> ''
AND LOWER(c.name) = LOWER(TRIM(m.constructeur));
UPDATE "composants" co
SET "constructeurId" = c.id
FROM "constructeurs" c
WHERE co.constructeur IS NOT NULL
AND TRIM(co.constructeur) <> ''
AND LOWER(c.name) = LOWER(TRIM(co.constructeur));
UPDATE "pieces" p
SET "constructeurId" = c.id
FROM "constructeurs" c
WHERE p.constructeur IS NOT NULL
AND TRIM(p.constructeur) <> ''
AND LOWER(c.name) = LOWER(TRIM(p.constructeur));
-- Drop legacy constructeur columns
ALTER TABLE "machines" DROP COLUMN "constructeur";
ALTER TABLE "composants" DROP COLUMN "constructeur";
ALTER TABLE "pieces" DROP COLUMN "constructeur";
-- Add foreign key constraints
ALTER TABLE "machines"
ADD CONSTRAINT "machines_constructeurId_fkey"
FOREIGN KEY ("constructeurId") REFERENCES "constructeurs"("id")
ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "composants"
ADD CONSTRAINT "composants_constructeurId_fkey"
FOREIGN KEY ("constructeurId") REFERENCES "constructeurs"("id")
ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "pieces"
ADD CONSTRAINT "pieces_constructeurId_fkey"
FOREIGN KEY ("constructeurId") REFERENCES "constructeurs"("id")
ON DELETE SET NULL ON UPDATE CASCADE;

View File

@@ -81,7 +81,6 @@ model Machine {
id String @id @default(cuid())
name String
reference String?
constructeur String?
prix Decimal? @db.Decimal(10, 2)
emplacement String?
createdAt DateTime @default(now())
@@ -94,6 +93,9 @@ model Machine {
typeMachineId String?
typeMachine TypeMachine? @relation(fields: [typeMachineId], references: [id])
constructeurId String?
constructeur Constructeur? @relation(fields: [constructeurId], references: [id], onDelete: SetNull)
composants Composant[]
pieces Piece[]
documents Document[] @relation("MachineDocuments")
@@ -106,7 +108,6 @@ model Composant {
id String @id @default(cuid())
name String
reference String?
constructeur String?
prix Decimal? @db.Decimal(10, 2)
emplacement String?
createdAt DateTime @default(now())
@@ -123,6 +124,9 @@ model Composant {
typeComposantId String?
typeComposant TypeComposant? @relation(fields: [typeComposantId], references: [id])
constructeurId String?
constructeur Constructeur? @relation(fields: [constructeurId], references: [id], onDelete: SetNull)
pieces Piece[]
documents Document[] @relation("ComposantDocuments")
customFieldValues CustomFieldValue[] @relation("ComposantCustomFieldValues")
@@ -134,7 +138,6 @@ model Piece {
id String @id @default(cuid())
name String
reference String?
constructeur String?
prix Decimal? @db.Decimal(10, 2)
emplacement String?
createdAt DateTime @default(now())
@@ -150,12 +153,30 @@ model Piece {
typePieceId String?
typePiece TypePiece? @relation(fields: [typePieceId], references: [id])
constructeurId String?
constructeur Constructeur? @relation(fields: [constructeurId], references: [id], onDelete: SetNull)
documents Document[] @relation("PieceDocuments")
customFieldValues CustomFieldValue[] @relation("PieceCustomFieldValues")
@@map("pieces")
}
model Constructeur {
id String @id @default(cuid())
name String @unique
email String?
phone String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
machines Machine[]
composants Composant[]
pieces Piece[]
@@map("constructeurs")
}
model Document {
id String @id @default(cuid())
name String