Files
HealthLog/prisma/migrations/0404_medication_ingredients/migration.sql
T
Marc-André Bombeck 6e72de1c35 feat(db): add the v1.43 schema and drop the readable location columns
Migrations 0402 to 0410: AI usage levels, biomarker aliases and merges,
medication ingredients (with the supplement label run kind), life
circumstance fact kinds, AI sections and AUDIT-C, Dawarich context, the
pulse day-mean method, intraday hour buckets, the readable location drop
and five relation link tables. Every new column carries its reader or a
pending-consumer marker.

0409 refuses to run while any home, location period or stored day still
holds a readable location, and names the counts. Locations are read from
the sealed copy only; the encryption backfill no longer handles them, and
the demo seed writes sealed locations only.

The new tables are backed up and wiped, the new sealed columns are in the
rotation registry with labels of their own, the AI usage level and the
life-circumstance confirmations travel with the backup, and the wellbeing
score reads the mental-health screeners only.
2026-10-15 21:48:57 +02:00

44 lines
1.9 KiB
SQL

-- Supplement composition.
--
-- What one unit (tablet, capsule) of a medication contains, per nutrient, in
-- the nutrient catalogue's canonical unit. One row per (medication, nutrient).
-- The entered amount and unit are kept when the input unit differed (IU for
-- vitamin D). Taken doses add their share to `nutrient_intake_days` under the
-- text source `SUPPLEMENT`, which needs no schema change. Reading a label
-- photo is a background document AI run of its own kind; the value is added
-- only, nothing here uses it.
-- AlterEnum
ALTER TYPE "document_ai_run_kind" ADD VALUE 'SUPPLEMENT_LABEL_EXTRACT';
-- CreateTable
CREATE TABLE "medication_ingredients" (
"id" TEXT NOT NULL,
"user_id" TEXT NOT NULL,
"medication_id" TEXT NOT NULL,
"nutrient" VARCHAR(40) NOT NULL,
"amount_per_unit" DOUBLE PRECISION NOT NULL,
"unit" VARCHAR(8) NOT NULL,
"entered_amount" DOUBLE PRECISION,
"entered_unit" VARCHAR(8),
"source" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
CONSTRAINT "medication_ingredients_pkey" PRIMARY KEY ("id"),
CONSTRAINT "medication_ingredients_amount_per_unit_check" CHECK ("amount_per_unit" > 0),
CONSTRAINT "medication_ingredients_source_check" CHECK ("source" IN ('MANUAL', 'LABEL_AI'))
);
-- CreateIndex
CREATE INDEX "medication_ingredients_user_id_idx" ON "medication_ingredients"("user_id");
-- CreateIndex
CREATE UNIQUE INDEX "medication_ingredients_medication_id_nutrient_key" ON "medication_ingredients"("medication_id", "nutrient");
-- AddForeignKey
ALTER TABLE "medication_ingredients" ADD CONSTRAINT "medication_ingredients_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "medication_ingredients" ADD CONSTRAINT "medication_ingredients_medication_id_fkey" FOREIGN KEY ("medication_id") REFERENCES "medications"("id") ON DELETE CASCADE ON UPDATE CASCADE;