feat: Add Model gestion for piece and component

This commit is contained in:
Matthieu
2025-09-23 15:05:33 +02:00
parent bc225bf3e8
commit e64fba3ae7
54 changed files with 1640 additions and 569 deletions

View File

@@ -0,0 +1,21 @@
-- CreateEnum
CREATE TYPE "ModelCategory" AS ENUM ('COMPONENT', 'PIECE');
-- CreateTable
CREATE TABLE "ModelType" (
"id" TEXT NOT NULL,
"name" VARCHAR(120) NOT NULL,
"code" VARCHAR(60) NOT NULL,
"category" "ModelCategory" NOT NULL,
"notes" TEXT,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
CONSTRAINT "ModelType_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "ModelType_code_key" ON "ModelType"("code");
-- CreateIndex
CREATE INDEX "ModelType_category_name_idx" ON "ModelType"("category", "name");

View File

@@ -0,0 +1,35 @@
/*
Warnings:
- You are about to drop the `type_composants` table. If the table is not empty, all the data it contains will be lost.
*/
-- DropForeignKey
ALTER TABLE "composant_models" DROP CONSTRAINT "composant_models_typeComposantId_fkey";
-- DropForeignKey
ALTER TABLE "composants" DROP CONSTRAINT "composants_typeComposantId_fkey";
-- DropForeignKey
ALTER TABLE "custom_fields" DROP CONSTRAINT "custom_fields_typeComposantId_fkey";
-- DropForeignKey
ALTER TABLE "type_machine_component_requirements" DROP CONSTRAINT "type_machine_component_requirements_typeComposantId_fkey";
-- AlterTable
ALTER TABLE "ModelType" ADD COLUMN "description" TEXT;
-- DropTable
DROP TABLE "type_composants";
-- AddForeignKey
ALTER TABLE "composants" ADD CONSTRAINT "composants_typeComposantId_fkey" FOREIGN KEY ("typeComposantId") REFERENCES "ModelType"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "custom_fields" ADD CONSTRAINT "custom_fields_typeComposantId_fkey" FOREIGN KEY ("typeComposantId") REFERENCES "ModelType"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "composant_models" ADD CONSTRAINT "composant_models_typeComposantId_fkey" FOREIGN KEY ("typeComposantId") REFERENCES "ModelType"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "type_machine_component_requirements" ADD CONSTRAINT "type_machine_component_requirements_typeComposantId_fkey" FOREIGN KEY ("typeComposantId") REFERENCES "ModelType"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@@ -0,0 +1,43 @@
/*
Migration: migrate legacy TypePiece references to ModelType entries
*/
-- Insert existing type_pieces into ModelType if not already present
INSERT INTO "ModelType" ("id", "name", "code", "category", "notes", "description", "createdAt", "updatedAt")
SELECT
tp."id",
tp."name",
tp."id" AS "code",
'PIECE'::"ModelCategory",
tp."description",
tp."description",
tp."createdAt",
tp."updatedAt"
FROM "type_pieces" tp
ON CONFLICT ("id") DO NOTHING;
-- Drop existing foreign keys referencing type_pieces
ALTER TABLE "pieces" DROP CONSTRAINT IF EXISTS "pieces_typePieceId_fkey";
ALTER TABLE "custom_fields" DROP CONSTRAINT IF EXISTS "custom_fields_typePieceId_fkey";
ALTER TABLE "piece_models" DROP CONSTRAINT IF EXISTS "piece_models_typePieceId_fkey";
ALTER TABLE "type_machine_piece_requirements" DROP CONSTRAINT IF EXISTS "type_machine_piece_requirements_typePieceId_fkey";
-- Drop legacy table
DROP TABLE IF EXISTS "type_pieces";
-- Recreate foreign keys pointing to ModelType
ALTER TABLE "pieces"
ADD CONSTRAINT "pieces_typePieceId_fkey"
FOREIGN KEY ("typePieceId") REFERENCES "ModelType"("id") ON DELETE SET NULL ON UPDATE CASCADE;
ALTER TABLE "custom_fields"
ADD CONSTRAINT "custom_fields_typePieceId_fkey"
FOREIGN KEY ("typePieceId") REFERENCES "ModelType"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "piece_models"
ADD CONSTRAINT "piece_models_typePieceId_fkey"
FOREIGN KEY ("typePieceId") REFERENCES "ModelType"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "type_machine_piece_requirements"
ADD CONSTRAINT "type_machine_piece_requirements_typePieceId_fkey"
FOREIGN KEY ("typePieceId") REFERENCES "ModelType"("id") ON DELETE RESTRICT ON UPDATE CASCADE;