40 lines
2.2 KiB
SQL
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());
|