diff --git a/apps/api/prisma/migrations/20260818113250_ingredient_taxonomy_rework/migration.sql b/apps/api/prisma/migrations/20260818113250_ingredient_taxonomy_rework/migration.sql index 025b473..b2cc977 100644 --- a/apps/api/prisma/migrations/20260818113250_ingredient_taxonomy_rework/migration.sql +++ b/apps/api/prisma/migrations/20260818113250_ingredient_taxonomy_rework/migration.sql @@ -1,18 +1,38 @@ +-- Rewritten after this migration failed on a database that already had +-- seeded ingredient rows: the original version (auto-generated by `prisma +-- migrate diff`) cast every existing `category` value directly from the +-- old 18-value enum to the new 7-value one, which fails for every row +-- since none of the old values exist in the new enum. This version instead +-- adds the new columns with a safe default (never casting existing data), +-- then swaps them in — the same "add with a default, correct for real on +-- the next seed run" pattern already used for `IngredientCategory`'s and +-- `IngredientSubcategory`'s own `@default(...)` (see their doc comments in +-- schema.prisma). `seedReferenceData()` runs right after `migrate deploy` +-- on every container start (see apps/api/Dockerfile) and corrects every +-- row's real category/subcategory immediately. +-- +-- `DROP TYPE IF EXISTS "IngredientSubcategory"` guards against a previous +-- failed attempt at this exact migration: that CREATE TYPE statement runs +-- outside the AlterEnum transaction below and so persists even though the +-- rest of that failed attempt rolled back — retrying without this guard +-- would hit "type already exists". + -- CreateEnum +DROP TYPE IF EXISTS "IngredientSubcategory"; CREATE TYPE "IngredientSubcategory" AS ENUM ('LEGUMES', 'FRUITS', 'HERBES_FRAICHES', 'VIANDES', 'VOLAILLES', 'POISSONS', 'CRUSTACES_FRUITS_DE_MER', 'FECULENTS', 'LEGUMINEUSES', 'GRAINES_FRUITS_SECS', 'AUTRES', 'PAINS', 'PATES_A_CUIRE', 'PRODUITS_LAITIERS', 'OEUFS', 'ALTERNATIVES', 'EPICES', 'SAUCES', 'ASSAISONNEMENTS', 'BASES', 'EPAISSISSANTS', 'SUCRES'); --- AlterEnum -BEGIN; +-- CreateEnum +DROP TYPE IF EXISTS "IngredientCategory_new"; CREATE TYPE "IngredientCategory_new" AS ENUM ('PRODUITS_FRAIS', 'BOUCHERIE_POISSONNERIE', 'EPICERIE_SECHE', 'BOULANGERIE', 'CREMERIE_FROMAGE', 'CONDIMENTS_EPICES', 'AIDES_CULINAIRES'); -ALTER TABLE "ingredients" ALTER COLUMN "category" DROP DEFAULT; -ALTER TABLE "ingredients" ALTER COLUMN "category" TYPE "IngredientCategory_new" USING ("category"::text::"IngredientCategory_new"); -ALTER TYPE "IngredientCategory" RENAME TO "IngredientCategory_old"; + +-- AlterTable: add the new columns at their defaults — no cast of existing +-- `category` values, so this succeeds regardless of what the table +-- currently holds. +ALTER TABLE "ingredients" ADD COLUMN "category_new" "IngredientCategory_new" NOT NULL DEFAULT 'EPICERIE_SECHE'; +ALTER TABLE "ingredients" ADD COLUMN "subcategory" "IngredientSubcategory" NOT NULL DEFAULT 'AUTRES'; + +-- Swap the old `category` column (old 18-value enum) out for the new one. +ALTER TABLE "ingredients" DROP COLUMN "category"; +ALTER TABLE "ingredients" RENAME COLUMN "category_new" TO "category"; +DROP TYPE "IngredientCategory"; ALTER TYPE "IngredientCategory_new" RENAME TO "IngredientCategory"; -DROP TYPE "IngredientCategory_old"; -ALTER TABLE "ingredients" ALTER COLUMN "category" SET DEFAULT 'EPICERIE_SECHE'; -COMMIT; - --- AlterTable -ALTER TABLE "ingredients" ADD COLUMN "subcategory" "IngredientSubcategory" NOT NULL DEFAULT 'AUTRES', -ALTER COLUMN "category" SET DEFAULT 'EPICERIE_SECHE'; -