Standort: locations.parent_id, eine Ebene. Neu "Shop" mit sechs Shops (Shop W17, Shop Wodo, Shop Parndorf, Shop Donaupark, Shop W1, Shop Wien Hauptbahnhof). Personen sitzen nur auf Standorten ohne Unterstandorte; zwei Trigger halten das auch fuer Import und Nachtlauf fest. Einstellung, Externe und Versetzung zeigen bei "Shop" eine zweite Auswahl (StandortAuswahl). Filter in Liste und Berichten und die Importliste bieten "Shop" selbst nicht an; der Import nennt bei "Shop" die Shops. rauchtest.sql und vertragsangaben.sql waehlen einen zuweisbaren Standort. C-Level: ueberall nur der Code (Formular, Akte, Zusammenfassung, Historie, Berichte, Import). Export ohne Spalte "C-Level Bezeichnung". Angehoerige und Notfallkontakte im Import wie im Formular: Auswahl als feste Liste und daneben "Verhaeltnis (Sonstige)" fuer den eigenen Text. Gespeichert wird bei "Sonstige" der Text; ein Text ohne "Sonstige" ist ein Fehler. Der Download teilt einen eigenen Text in beide Spalten. Geprueft: lint, tsc, types:check, npm test, waechter; Migrationen auf leerer Datenbank zweimal (Nichts anzuwenden), rauchtest.sql, vertragsangaben.sql; als alpenwerk_app: Einstellung auf "Shop" abgewiesen, auf "Shop W17" angenommen, Wechsel auf "Shop" abgewiesen, auf "Shop Wodo" angenommen, Unterstandort unter einem Shop abgewiesen.
140 lines
6.3 KiB
PL/PgSQL
140 lines
6.3 KiB
PL/PgSQL
-- Standorte mit Unterstandorten: „Shop" und die einzelnen Shops
|
|
--
|
|
-- Ein Standort kann Unterstandorte haben. Eine Person sitzt immer auf einem
|
|
-- Standort ohne Unterstandorte: „Shop" allein sagt nicht, wo jemand
|
|
-- arbeitet. Die Oberfläche bietet zuerst die Standorte ohne übergeordneten
|
|
-- an und bei einem mit Unterstandorten eine zweite Auswahl.
|
|
--
|
|
-- Der Name eines Unterstandorts nennt den übergeordneten mit („Shop W17"):
|
|
-- Liste, Berichte, Exporte und IT-Sicht lesen nur den Namen und bleiben so
|
|
-- ohne weitere Verknüpfung eindeutig.
|
|
--
|
|
-- Eine Ebene: ein Unterstandort hat keine eigenen. Zwei Trigger halten das
|
|
-- fest, auch für den Import und den Nachtlauf, die an der Oberfläche
|
|
-- vorbeigehen.
|
|
|
|
alter table locations add column if not exists parent_id uuid references locations(id);
|
|
|
|
comment on column locations.parent_id is
|
|
'Übergeordneter Standort (etwa „Shop"); leer = Standort der obersten Ebene. Personen sitzen nur auf Standorten ohne Unterstandorte.';
|
|
|
|
alter table locations drop constraint if exists chk_location_nicht_eigener_parent;
|
|
alter table locations add constraint chk_location_nicht_eigener_parent check (parent_id is null or parent_id <> id);
|
|
|
|
create index if not exists idx_locations_parent on locations (parent_id) where parent_id is not null;
|
|
|
|
-- Sitzt schon jemand auf einem Standort „Shop", fehlt die Angabe, auf welchem.
|
|
-- Raten wäre schlechter als anhalten.
|
|
do $pruefung$
|
|
begin
|
|
if exists (select 1 from employees e join locations l on l.id = e.location_id where l.name = 'Shop') then
|
|
raise exception 'Auf dem Standort „Shop" sitzen Personen. Sie brauchen zuerst einen anderen Standort; danach die Migration erneut ausführen.';
|
|
end if;
|
|
end
|
|
$pruefung$;
|
|
|
|
-- ── Regeln ──────────────────────────────────────────────────────────────
|
|
|
|
create or replace function public.standort_ebene_pruefen()
|
|
returns trigger
|
|
language plpgsql
|
|
set search_path = public, pg_temp
|
|
as $$
|
|
begin
|
|
if new.parent_id is null then
|
|
return new;
|
|
end if;
|
|
if exists (select 1 from locations where id = new.parent_id and parent_id is not null) then
|
|
raise exception 'Ein Unterstandort kann keine eigenen Unterstandorte haben.';
|
|
end if;
|
|
if exists (select 1 from locations where parent_id = new.id) then
|
|
raise exception 'Ein Standort mit Unterstandorten kann nicht selbst unter einem anderen stehen.';
|
|
end if;
|
|
if exists (select 1 from employees where location_id = new.parent_id) then
|
|
raise exception 'Auf dem Standort „%" sitzen Personen; er kann keine Unterstandorte bekommen.',
|
|
(select name from locations where id = new.parent_id);
|
|
end if;
|
|
return new;
|
|
end;
|
|
$$;
|
|
|
|
drop trigger if exists trg_standort_ebene on locations;
|
|
create trigger trg_standort_ebene
|
|
before insert or update of parent_id on locations
|
|
for each row execute function public.standort_ebene_pruefen();
|
|
|
|
create or replace function public.standort_ohne_unterstandorte()
|
|
returns trigger
|
|
language plpgsql
|
|
set search_path = public, pg_temp
|
|
as $$
|
|
begin
|
|
if new.location_id is not null and exists (select 1 from locations where parent_id = new.location_id) then
|
|
raise exception 'Der Standort „%" fasst mehrere zusammen. Bitte einen davon auswählen.',
|
|
(select name from locations where id = new.location_id);
|
|
end if;
|
|
return new;
|
|
end;
|
|
$$;
|
|
|
|
drop trigger if exists trg_standort_ohne_unterstandorte on employees;
|
|
create trigger trg_standort_ohne_unterstandorte
|
|
before insert or update of location_id on employees
|
|
for each row execute function public.standort_ohne_unterstandorte();
|
|
|
|
-- ── Shop und die einzelnen Shops ────────────────────────────────────────
|
|
|
|
insert into locations (name, country) values ('Shop', 'Österreich') on conflict (name) do nothing;
|
|
|
|
insert into locations (name, country, parent_id)
|
|
select v.name, 'Österreich', (select id from locations where name = 'Shop')
|
|
from (values ('Shop W17'), ('Shop Wodo'), ('Shop Parndorf'), ('Shop Donaupark'), ('Shop W1'), ('Shop Wien Hauptbahnhof')) v(name)
|
|
on conflict (name) do nothing;
|
|
|
|
-- Bestanden einzelne Shops schon als eigene Standorte, kommen sie unter „Shop".
|
|
update locations
|
|
set parent_id = (select id from locations where name = 'Shop')
|
|
where name in ('Shop W17', 'Shop Wodo', 'Shop Parndorf', 'Shop Donaupark', 'Shop W1', 'Shop Wien Hauptbahnhof')
|
|
and parent_id is null;
|
|
|
|
-- ── Selbstprüfung ───────────────────────────────────────────────────────
|
|
|
|
do $migration$
|
|
declare
|
|
v_shop uuid;
|
|
begin
|
|
select id into v_shop from locations where name = 'Shop';
|
|
if v_shop is null then
|
|
raise exception 'Der Standort „Shop" fehlt.';
|
|
end if;
|
|
if (select parent_id from locations where id = v_shop) is not null then
|
|
raise exception '„Shop" steht unter einem anderen Standort.';
|
|
end if;
|
|
if (select count(*) from locations where parent_id = v_shop) <> 6 then
|
|
raise exception 'Unter „Shop" stehen nicht genau die sechs Shops.';
|
|
end if;
|
|
if exists (
|
|
select 1 from (values ('Shop W17'), ('Shop Wodo'), ('Shop Parndorf'), ('Shop Donaupark'), ('Shop W1'), ('Shop Wien Hauptbahnhof')) v(name)
|
|
where not exists (select 1 from locations l where l.name = v.name and l.parent_id = v_shop)
|
|
) then
|
|
raise exception 'Ein Shop fehlt oder steht nicht unter „Shop".';
|
|
end if;
|
|
if exists (select 1 from employees e join locations l on l.parent_id = e.location_id) then
|
|
raise exception 'Eine Person sitzt auf einem Standort mit Unterstandorten.';
|
|
end if;
|
|
if (select count(*) from pg_trigger
|
|
where not tgisinternal and tgenabled = 'O'
|
|
and tgname in ('trg_standort_ebene', 'trg_standort_ohne_unterstandorte')) <> 2 then
|
|
raise exception 'Die Trigger zu den Unterstandorten fehlen.';
|
|
end if;
|
|
if exists (
|
|
select 1 from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
|
where n.nspname = 'public'
|
|
and p.proname in ('standort_ebene_pruefen', 'standort_ohne_unterstandorte')
|
|
and (p.prosecdef or not ('search_path=public, pg_temp' = any (coalesce(p.proconfig, '{}'))))
|
|
) then
|
|
raise exception 'Die Triggerfunktionen laufen nicht unter den Rechten des Aufrufers oder ohne search_path.';
|
|
end if;
|
|
end
|
|
$migration$;
|