Files
lunch/db/migrations/0022_ingredient_groups.sql
kai 2f56f3a479
release / which version (push) Successful in 9s
release / olares chart · sanity (push) Successful in 7s
release / app (push) Successful in 1m5s
release / tag and release (push) Successful in 3s
26.11.38: Produktgruppen, eigene Zutaten, Lieblingsprodukt (#39)
2026-11-07 14:22:02 +00:00

40 lines
2.2 KiB
SQL

-- Produktgruppen und eigene Zutaten (Kai, 7.11.2026, 26.11.38).
--
-- Gruppen: Eine Zutat kann ein Oberbegriff sein (Pasta, Reis, Käse, Hülsenfrüchte)
-- und selbst gewählt werden („Pasta, beliebige Sorte“); die Sorten zeigen mit
-- parent_id auf ihn. Kommen aus db/seed wie alle globalen Zutaten.
--
-- Eigene Zutaten: Fehlt etwas im Katalog („Gnocchi“), legen Erwachsene es für
-- ihren Haushalt an (household_id). Global bleibt household_id leer.
--
-- Lieblingsprodukt je Zutat oder Gruppe für den ganzen Haushalt („unsere Pasta
-- ist Barilla Nr. 5“): household_products; ein Produkt am Gericht geht vor.
alter table ingredients add column is_group boolean not null default false;
alter table ingredients add column parent_id uuid references ingredients (id);
alter table ingredients add column household_id uuid references households (id) on delete cascade;
create index ingredients_parent_idx on ingredients (parent_id) where parent_id is not null;
create index ingredients_household_idx on ingredients (household_id) where household_id is not null;
drop policy ingredients_read on ingredients;
create policy ingredients_read on ingredients for select
using (household_id is null or household_id = app_household_id() or app_maintenance());
create policy ingredients_own on ingredients for all
using (household_id = app_household_id())
with check (household_id = app_household_id() and not is_group);
create table household_products (
household_id uuid not null references households (id) on delete cascade,
ingredient_id uuid not null references ingredients (id) on delete cascade,
product text not null check (length(trim(product)) between 1 and 80),
product_pack numeric(12, 3) check (product_pack is null or product_pack > 0),
cook_minutes integer check (cook_minutes is null or cook_minutes between 1 and 240),
updated_at timestamptz not null default now(),
primary key (household_id, ingredient_id)
);
alter table household_products enable row level security;
alter table household_products force row level security;
create policy household_products_household on household_products
using (household_id = app_household_id())
with check (household_id = app_household_id());