batchCooking/apps/api/prisma/migrations/20260816100611_init/migration.sql
kyuno053 b90817b8e1
Add specs + Prisma schema for the documented data model (#3)
* Add project specs, gitignore the source PDF

specs/batch-cooking-architecture.md and specs/batch-cooking-modele.md
are the clean markdown transcription of "Projet batch cooking.pdf"
(a scanned/image-only PDF, no extractable text). The PDF itself is
gitignored — source working document, not meant to be committed.

* Add Prisma schema for the documented data model

Models every table from specs/batch-cooking-modele.md: users/household
(user_profiles, house, diet, allergy, category), planning (planning,
planning_item), and recipes (recipe, ingredients, step, tech_step,
tech_step_mapping, sources).

Two deliberate deviations from the literal spec doc, per project
discussion:

- recipe_ingredient (recipe <-> ingredients) carries quantity + unit.
  The spec describes a plain many-to-many with no extra fields, but a
  shopping list / batch-cooking calculation needs quantities.
- step is modeled one-to-many from recipe (not many-to-many as labeled
  in the doc): the documented `order` column only makes sense scoped
  to a single recipe, which isn't reconcilable with steps being
  shared across recipes.

Everything else follows the doc as-is, including field nullability
choices made where the doc doesn't specify (e.g. user_profiles.house_id
optional, recipe.source_id optional) and onDelete behavior (Cascade
for owned child records, SetNull for optional references) — first
draft, not meant as final production hardening.

Verified: `prisma validate`, `prisma generate`, and a real
`prisma migrate dev` against a local Postgres (via docker-compose) —
the migration applies cleanly and produces the expected schema.

README: documents the migrate command and a Postgres port-conflict
gotcha hit during validation (a native Postgres service on this
machine was already bound to 5432, intercepting the Docker container's
connections).

* Add COMMENT ON for every table and column in the init migration

Descriptions pulled from specs/batch-cooking-modele.md's per-table
field tables. The two tables not in the original spec (join tables
recipe_ingredient, user_profile_allergy) get a comment explaining
why they exist.

Amends the still-unmerged init migration directly rather than adding
a follow-up migration, since it hasn't been applied anywhere but this
local dev database.

Verified: `prisma migrate reset --force` reapplies cleanly, and a
query against pg_description confirms every column of every project
table has a comment (only Prisma's own internal _prisma_migrations
table is uncommented, out of scope).
2026-08-16 12:23:59 +02:00

271 lines
11 KiB
SQL

-- CreateTable
CREATE TABLE "house" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
CONSTRAINT "house_pkey" PRIMARY KEY ("id")
);
COMMENT ON TABLE "house" IS 'Foyer regroupant un ou plusieurs profils utilisateurs et leurs plannings.';
COMMENT ON COLUMN "house"."id" IS 'Identifiant';
COMMENT ON COLUMN "house"."name" IS 'Nom du foyer';
-- CreateTable
CREATE TABLE "diet" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
CONSTRAINT "diet_pkey" PRIMARY KEY ("id")
);
COMMENT ON TABLE "diet" IS 'Régime alimentaire pouvant être suivi par un profil utilisateur.';
COMMENT ON COLUMN "diet"."id" IS 'Identifiant';
COMMENT ON COLUMN "diet"."name" IS 'Nom du régime alimentaire';
-- CreateTable
CREATE TABLE "category" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
CONSTRAINT "category_pkey" PRIMARY KEY ("id")
);
COMMENT ON TABLE "category" IS 'Table d''énumération, destinée à grandir au fil du projet (portera notamment les nuances liées aux allergies).';
COMMENT ON COLUMN "category"."id" IS 'Identifiant';
COMMENT ON COLUMN "category"."name" IS 'Nom de la catégorie';
-- CreateTable
CREATE TABLE "allergy" (
"id" SERIAL NOT NULL,
"cat_id" INTEGER NOT NULL,
CONSTRAINT "allergy_pkey" PRIMARY KEY ("id")
);
COMMENT ON TABLE "allergy" IS 'Allergie, rattachée à une catégorie. Associée à user_profiles en many-to-many (table de jointure simple, sans champ additionnel).';
COMMENT ON COLUMN "allergy"."id" IS 'Identifiant';
COMMENT ON COLUMN "allergy"."cat_id" IS 'FK → category';
-- CreateTable
CREATE TABLE "user_profiles" (
"id" SERIAL NOT NULL,
"first_name" TEXT NOT NULL,
"last_name" TEXT NOT NULL,
"email" TEXT NOT NULL,
"house_id" INTEGER,
"diet_id" INTEGER,
CONSTRAINT "user_profiles_pkey" PRIMARY KEY ("id")
);
COMMENT ON TABLE "user_profiles" IS 'Profil utilisateur, rattaché à un foyer et éventuellement à un régime alimentaire.';
COMMENT ON COLUMN "user_profiles"."id" IS 'Identifiant';
COMMENT ON COLUMN "user_profiles"."first_name" IS 'Prénom';
COMMENT ON COLUMN "user_profiles"."last_name" IS 'Nom';
COMMENT ON COLUMN "user_profiles"."email" IS 'Email';
COMMENT ON COLUMN "user_profiles"."house_id" IS 'FK → house';
COMMENT ON COLUMN "user_profiles"."diet_id" IS 'FK → diet';
-- CreateTable
CREATE TABLE "user_profile_allergy" (
"user_profile_id" INTEGER NOT NULL,
"allergy_id" INTEGER NOT NULL,
CONSTRAINT "user_profile_allergy_pkey" PRIMARY KEY ("user_profile_id","allergy_id")
);
COMMENT ON TABLE "user_profile_allergy" IS 'Table de jointure simple pour l''association many-to-many user_profiles ↔ allergy (sans champ additionnel, non présente telle quelle dans la spec d''origine).';
COMMENT ON COLUMN "user_profile_allergy"."user_profile_id" IS 'FK → user_profiles';
COMMENT ON COLUMN "user_profile_allergy"."allergy_id" IS 'FK → allergy';
-- CreateTable
CREATE TABLE "planning" (
"id" SERIAL NOT NULL,
"start_date" DATE NOT NULL,
"finish_date" DATE NOT NULL,
"house_id" INTEGER NOT NULL,
CONSTRAINT "planning_pkey" PRIMARY KEY ("id")
);
COMMENT ON TABLE "planning" IS 'Planning de repas d''un foyer sur une période donnée.';
COMMENT ON COLUMN "planning"."id" IS 'Identifiant';
COMMENT ON COLUMN "planning"."start_date" IS 'Date de début';
COMMENT ON COLUMN "planning"."finish_date" IS 'Date de fin';
COMMENT ON COLUMN "planning"."house_id" IS 'FK → house';
-- CreateTable
CREATE TABLE "planning_item" (
"id" SERIAL NOT NULL,
"planning_id" INTEGER NOT NULL,
"week_day" TEXT NOT NULL,
"meal" TEXT NOT NULL,
"recipe_id" INTEGER NOT NULL,
CONSTRAINT "planning_item_pkey" PRIMARY KEY ("id")
);
COMMENT ON TABLE "planning_item" IS 'Entrée d''un planning : une recette assignée à un jour et un repas donnés.';
COMMENT ON COLUMN "planning_item"."id" IS 'Identifiant';
COMMENT ON COLUMN "planning_item"."planning_id" IS 'FK → planning';
COMMENT ON COLUMN "planning_item"."week_day" IS 'Jour de la semaine';
COMMENT ON COLUMN "planning_item"."meal" IS 'Repas concerné';
COMMENT ON COLUMN "planning_item"."recipe_id" IS 'FK → recipe';
-- CreateTable
CREATE TABLE "sources" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"url" TEXT,
CONSTRAINT "sources_pkey" PRIMARY KEY ("id")
);
COMMENT ON TABLE "sources" IS 'Source d''origine d''une recette (site, livre, etc.), utilisée par le pipeline d''import.';
COMMENT ON COLUMN "sources"."id" IS 'Identifiant';
COMMENT ON COLUMN "sources"."name" IS 'Nom de la source';
COMMENT ON COLUMN "sources"."url" IS 'URL';
-- CreateTable
CREATE TABLE "recipe" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"source_id" INTEGER,
"description" TEXT,
"picture" TEXT,
CONSTRAINT "recipe_pkey" PRIMARY KEY ("id")
);
COMMENT ON TABLE "recipe" IS 'Recette de cuisine. Associée à ingredients en many-to-many (voir recipe_ingredient).';
COMMENT ON COLUMN "recipe"."id" IS 'Identifiant';
COMMENT ON COLUMN "recipe"."name" IS 'Nom de la recette';
COMMENT ON COLUMN "recipe"."source_id" IS 'FK → sources';
COMMENT ON COLUMN "recipe"."description" IS 'Description';
COMMENT ON COLUMN "recipe"."picture" IS 'Image';
-- CreateTable
CREATE TABLE "ingredients" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"icon" TEXT,
"alternate_recipe" INTEGER,
CONSTRAINT "ingredients_pkey" PRIMARY KEY ("id")
);
COMMENT ON TABLE "ingredients" IS 'Ingrédient pouvant entrer dans la composition d''une ou plusieurs recettes.';
COMMENT ON COLUMN "ingredients"."id" IS 'Identifiant';
COMMENT ON COLUMN "ingredients"."name" IS 'Nom';
COMMENT ON COLUMN "ingredients"."icon" IS 'Icône';
COMMENT ON COLUMN "ingredients"."alternate_recipe" IS 'FK → recipe (recette alternative, ex: faire soi-même plutôt qu''acheter)';
-- CreateTable
CREATE TABLE "recipe_ingredient" (
"recipe_id" INTEGER NOT NULL,
"ingredient_id" INTEGER NOT NULL,
"quantity" DECIMAL(10,2) NOT NULL,
"unit" TEXT NOT NULL,
CONSTRAINT "recipe_ingredient_pkey" PRIMARY KEY ("recipe_id","ingredient_id")
);
COMMENT ON TABLE "recipe_ingredient" IS 'Association recipe ↔ ingredients enrichie d''une quantité et d''une unité (non présent dans la spec d''origine, nécessaire pour les listes de courses / le calcul batch-cooking).';
COMMENT ON COLUMN "recipe_ingredient"."recipe_id" IS 'FK → recipe';
COMMENT ON COLUMN "recipe_ingredient"."ingredient_id" IS 'FK → ingredients';
COMMENT ON COLUMN "recipe_ingredient"."quantity" IS 'Quantité de l''ingrédient nécessaire pour la recette';
COMMENT ON COLUMN "recipe_ingredient"."unit" IS 'Unité de mesure de la quantité (g, ml, pièce...)';
-- CreateTable
CREATE TABLE "tech_step" (
"id" SERIAL NOT NULL,
CONSTRAINT "tech_step_pkey" PRIMARY KEY ("id")
);
COMMENT ON TABLE "tech_step" IS 'Technique culinaire réutilisable (ex: éplucher, mixer, cuire...), référencée par les étapes de recette.';
COMMENT ON COLUMN "tech_step"."id" IS 'Identifiant';
-- CreateTable
CREATE TABLE "tech_step_mapping" (
"id" SERIAL NOT NULL,
"tech_step_id" INTEGER NOT NULL,
"expression" TEXT NOT NULL,
"weight" INTEGER NOT NULL,
CONSTRAINT "tech_step_mapping_pkey" PRIMARY KEY ("id")
);
COMMENT ON TABLE "tech_step_mapping" IS 'Utilisé par le pipeline d''import de recette pour détecter automatiquement la technique correspondant à une instruction brute (expression = motif texte, weight = score de correspondance).';
COMMENT ON COLUMN "tech_step_mapping"."id" IS 'Identifiant';
COMMENT ON COLUMN "tech_step_mapping"."tech_step_id" IS 'FK → tech_step';
COMMENT ON COLUMN "tech_step_mapping"."expression" IS 'Expression';
COMMENT ON COLUMN "tech_step_mapping"."weight" IS 'Poids';
-- CreateTable
CREATE TABLE "step" (
"id" SERIAL NOT NULL,
"recipe_id" INTEGER NOT NULL,
"description" TEXT NOT NULL,
"picture" TEXT,
"order" INTEGER NOT NULL,
"tech_step_id" INTEGER,
CONSTRAINT "step_pkey" PRIMARY KEY ("id")
);
COMMENT ON TABLE "step" IS 'Étape d''une recette. Appartient à exactement une recette (one-to-many depuis recipe) : le champ order n''a de sens que dans le contexte d''une recette donnée — voir specs/batch-cooking-modele.md pour la formulation d''origine (many-to-many).';
COMMENT ON COLUMN "step"."id" IS 'Identifiant';
COMMENT ON COLUMN "step"."recipe_id" IS 'FK → recipe';
COMMENT ON COLUMN "step"."description" IS 'Description de l''étape';
COMMENT ON COLUMN "step"."picture" IS 'Image';
COMMENT ON COLUMN "step"."order" IS 'Ordre dans la recette';
COMMENT ON COLUMN "step"."tech_step_id" IS 'FK → tech_step';
-- CreateIndex
CREATE UNIQUE INDEX "user_profiles_email_key" ON "user_profiles"("email");
-- AddForeignKey
ALTER TABLE "allergy" ADD CONSTRAINT "allergy_cat_id_fkey" FOREIGN KEY ("cat_id") REFERENCES "category"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "user_profiles" ADD CONSTRAINT "user_profiles_house_id_fkey" FOREIGN KEY ("house_id") REFERENCES "house"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "user_profiles" ADD CONSTRAINT "user_profiles_diet_id_fkey" FOREIGN KEY ("diet_id") REFERENCES "diet"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "user_profile_allergy" ADD CONSTRAINT "user_profile_allergy_user_profile_id_fkey" FOREIGN KEY ("user_profile_id") REFERENCES "user_profiles"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "user_profile_allergy" ADD CONSTRAINT "user_profile_allergy_allergy_id_fkey" FOREIGN KEY ("allergy_id") REFERENCES "allergy"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "planning" ADD CONSTRAINT "planning_house_id_fkey" FOREIGN KEY ("house_id") REFERENCES "house"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "planning_item" ADD CONSTRAINT "planning_item_planning_id_fkey" FOREIGN KEY ("planning_id") REFERENCES "planning"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "planning_item" ADD CONSTRAINT "planning_item_recipe_id_fkey" FOREIGN KEY ("recipe_id") REFERENCES "recipe"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "recipe" ADD CONSTRAINT "recipe_source_id_fkey" FOREIGN KEY ("source_id") REFERENCES "sources"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "ingredients" ADD CONSTRAINT "ingredients_alternate_recipe_fkey" FOREIGN KEY ("alternate_recipe") REFERENCES "recipe"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "recipe_ingredient" ADD CONSTRAINT "recipe_ingredient_recipe_id_fkey" FOREIGN KEY ("recipe_id") REFERENCES "recipe"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "recipe_ingredient" ADD CONSTRAINT "recipe_ingredient_ingredient_id_fkey" FOREIGN KEY ("ingredient_id") REFERENCES "ingredients"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "tech_step_mapping" ADD CONSTRAINT "tech_step_mapping_tech_step_id_fkey" FOREIGN KEY ("tech_step_id") REFERENCES "tech_step"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "step" ADD CONSTRAINT "step_recipe_id_fkey" FOREIGN KEY ("recipe_id") REFERENCES "recipe"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "step" ADD CONSTRAINT "step_tech_step_id_fkey" FOREIGN KEY ("tech_step_id") REFERENCES "tech_step"("id") ON DELETE SET NULL ON UPDATE CASCADE;