69 lines
3.0 KiB
SQL
69 lines
3.0 KiB
SQL
-- 0004_seed_tables.sql — globale Vorlagen für Arbeitspaket 3.
|
|
--
|
|
-- Startergerichte und Gangfolgen der Ketten gehören keinem Haushalt: Sie sind
|
|
-- Vorlagen, die ein Haushalt übernimmt (lib/starter.ts, lib/stores.ts) — dann
|
|
-- als eigene Kopie in dishes bzw. stores. Lesbar für alle, schreibbar nur beim
|
|
-- Einspielen (scripts/seed.mjs setzt app.maintenance). Die Inhalte stehen in
|
|
-- db/seed/*.json und werden bei jedem Start nach den Migrationen eingespielt.
|
|
|
|
alter table ingredients
|
|
add column pack_sizes numeric(12, 3)[] not null default '{}', -- übliche Packungen, Basiseinheit
|
|
add column pantry_default boolean not null default false; -- Vorschlag für den Grundvorrat
|
|
|
|
alter table dishes add column starter_key text; -- aus welchem Startergericht übernommen
|
|
create unique index dishes_starter_once_idx on dishes (household_id, starter_key) where starter_key is not null;
|
|
|
|
create table starter_dishes (
|
|
key text primary key check (key ~ '^[a-z]{2}_[a-z0-9_]+$'),
|
|
locale text not null check (locale in ('de', 'en', 'fr', 'es')),
|
|
position integer not null,
|
|
title text not null,
|
|
base text check (base in ('pasta', 'rice', 'potato', 'bread', 'grain', 'none')),
|
|
tags text[] not null default '{}',
|
|
effort_minutes integer
|
|
);
|
|
|
|
create table starter_dish_ingredients (
|
|
starter_key text not null references starter_dishes (key) on delete cascade,
|
|
position integer not null,
|
|
ingredient_id uuid not null references ingredients (id),
|
|
qty_per_portion numeric(12, 3) not null check (qty_per_portion > 0),
|
|
unit text not null check (unit in ('g', 'ml', 'piece')),
|
|
note text,
|
|
is_staple boolean not null default false,
|
|
primary key (starter_key, position)
|
|
);
|
|
|
|
create table starter_dish_steps (
|
|
starter_key text not null references starter_dishes (key) on delete cascade,
|
|
position integer not null,
|
|
text text not null,
|
|
timer_seconds integer check (timer_seconds > 0),
|
|
primary key (starter_key, position)
|
|
);
|
|
|
|
create table store_chains (
|
|
key text primary key check (key ~ '^[a-z0-9_]+$'),
|
|
name text not null,
|
|
position integer not null
|
|
);
|
|
|
|
create table chain_aisles (
|
|
chain_key text not null references store_chains (key) on delete cascade,
|
|
position integer not null,
|
|
names jsonb not null,
|
|
ingredient_categories text[] not null,
|
|
primary key (chain_key, position)
|
|
);
|
|
|
|
do $$
|
|
declare t text;
|
|
begin
|
|
foreach t in array array['starter_dishes', 'starter_dish_ingredients', 'starter_dish_steps', 'store_chains', 'chain_aisles'] loop
|
|
execute format('alter table %I enable row level security', t);
|
|
execute format('alter table %I force row level security', t);
|
|
execute format('create policy %I on %I for select using (true)', t || '_read', t);
|
|
execute format('create policy %I on %I using (app_maintenance()) with check (app_maintenance())', t || '_maintain', t);
|
|
end loop;
|
|
end $$;
|