- Kommentare im Praesens statt Entwicklungsgeschichte, ohne Vorfaelle, ohne flapsige Wendungen; Verweise auf Migrationsnummern aus Komponenten entfernt; in Migrationen nur Kommentarzeilen, alle Kopien gleich (leere Datenbank: 156 Migrationen, Rauchtests gruen) - app/icon.svg ohne eingebettete Herkunfts-Metadaten - Oberflaeche: keine Emojis in Notizen, Text im Audit-Detail, No Show im Offboarding sachlich - Beispieladressen in Tests auf @firma.example - Installationsanleitung: Abschnitt App in Entra ID einrichten
306 lines
16 KiB
SQL
306 lines
16 KiB
SQL
-- Eine Versetzung schliesst die alte Besetzung auch dann, wenn ein datierter
|
|
-- Austritt sie bereits befristet hat — und die neue endet mit der Person.
|
|
--
|
|
-- ── Der Fall ──────────────────────────────────────────────────────────────
|
|
--
|
|
-- Ein Austritt befristet die laufende Besetzung auf den Austrittstag
|
|
-- (valid_to gesetzt). Übernimmt der nächtliche Lauf danach eine vorgemerkte
|
|
-- Versetzung mit der Schliessklausel
|
|
--
|
|
-- where employee_id = … and valid_to is null
|
|
--
|
|
-- trifft diese nichts mehr: die alte Besetzung ist ja nicht offen, sondern
|
|
-- befristet. Geschlossen wird nichts, eingefügt trotzdem — die Person sässe
|
|
-- gleichzeitig auf zwei Planstellen.
|
|
--
|
|
-- Auffallen kann das nicht von selbst. `position_assignments_one_holder`
|
|
-- wacht über die **Planstelle** (eine offene Besetzung je Stelle), nicht über
|
|
-- die Person; zwei Stellen für einen Menschen verletzt keine Bedingung.
|
|
--
|
|
-- ── Die zweite Hälfte, sichtbar erst im values-Teil ───────────────────────
|
|
--
|
|
-- Ohne valid_to eingefügt, hiesse die neue Besetzung bei einer Person mit
|
|
-- eingetragenem Austritt: sie verlässt zum Austrittstag das Unternehmen und
|
|
-- belegt die Planstelle weiter bis in alle Zeit. Die nächste Besetzung dieser
|
|
-- Stelle liefe dann in genau den Unique-Index, der oben nicht angesprungen
|
|
-- ist — Wochen später und ohne Zusammenhang zum Verursacher.
|
|
--
|
|
-- ── Warum auch das delete angefasst wird ──────────────────────────────────
|
|
--
|
|
-- Das delete davor räumt Besetzungen weg, die am Wirksamkeitstag erst
|
|
-- beginnen; sie liessen sich nicht schliessen, weil chk_assignment_range ein
|
|
-- Intervall von null Tagen verbietet (siehe 20260929180000). Fragte es
|
|
-- weiter nur nach `valid_to is null`, während die Schliessklausel erweitert
|
|
-- wird, entstünde ein neuer Fehler: eine heute begonnene, vom Austritt
|
|
-- bereits befristete Besetzung fiele durch das delete und bekäme vom
|
|
-- erweiterten update valid_to = valid_from — genau das verbotene
|
|
-- Nullintervall. Beide Klauseln müssen dieselbe Frage stellen: „läuft diese
|
|
-- Besetzung am Stichtag noch?"
|
|
--
|
|
-- ── Vier Stellen, ein Muster ──────────────────────────────────────────────
|
|
--
|
|
-- transfer_employee, promote_employee und beide Zweige von
|
|
-- apply_due_pending_changes tragen denselben Dreisatz. Damit keine Stelle
|
|
-- eine halbe Korrektur behält, werden alle vier in einem Durchgang ersetzt
|
|
-- und hinterher einzeln nachgezählt.
|
|
--
|
|
-- Die Anker stammen aus dem **lebenden** Katalog (pg_get_functiondef), nicht
|
|
-- aus den Migrationsdateien: diese Funktionen wurden mehrfach dynamisch
|
|
-- gepatcht, und ein Anker aus einer Datei träfe daneben.
|
|
--
|
|
-- Dass die Einrückung der beiden Zweige in apply_due_pending_changes
|
|
-- voneinander abweicht (acht gegen zehn Leerzeichen vor dem where), ist
|
|
-- übernommen und nicht begradigt. Begradigt wären die beiden Blöcke
|
|
-- zeichengleich — und ein künftiger Anker träfe beide auf einmal.
|
|
|
|
do $migration$
|
|
declare
|
|
r record;
|
|
v_def text;
|
|
v_treffer integer;
|
|
begin
|
|
for r in
|
|
select * from (values
|
|
-- ── transfer_employee ─────────────────────────────────────────────
|
|
('transfer_employee', $a1$
|
|
delete from position_assignments
|
|
where employee_id = v_employee_id and valid_to is null and valid_from >= v_effective;
|
|
update position_assignments set valid_to = v_effective
|
|
where employee_id = v_employee_id and valid_to is null;
|
|
insert into position_assignments (position_id, employee_id, valid_from)
|
|
values (v_target_position, v_employee_id, v_effective);
|
|
$a1$, $e1$
|
|
delete from position_assignments
|
|
where employee_id = v_employee_id and valid_from >= v_effective
|
|
and (valid_to is null or valid_to > v_effective);
|
|
update position_assignments set valid_to = v_effective
|
|
where employee_id = v_employee_id and (valid_to is null or valid_to > v_effective);
|
|
insert into position_assignments (position_id, employee_id, valid_from, valid_to)
|
|
values (v_target_position, v_employee_id, v_effective,
|
|
(select e.exit_date from employees e
|
|
where e.id = v_employee_id and e.exit_date > v_effective));
|
|
$e1$),
|
|
|
|
-- ── promote_employee ──────────────────────────────────────────────
|
|
('promote_employee', $a2$
|
|
delete from position_assignments
|
|
where employee_id = v_employee_id and valid_to is null and valid_from >= v_effective_date;
|
|
update position_assignments set valid_to = v_effective_date
|
|
where employee_id = v_employee_id and valid_to is null;
|
|
insert into position_assignments (position_id, employee_id, valid_from)
|
|
values (v_position, v_employee_id, v_effective_date);
|
|
$a2$, $e2$
|
|
delete from position_assignments
|
|
where employee_id = v_employee_id and valid_from >= v_effective_date
|
|
and (valid_to is null or valid_to > v_effective_date);
|
|
update position_assignments set valid_to = v_effective_date
|
|
where employee_id = v_employee_id and (valid_to is null or valid_to > v_effective_date);
|
|
insert into position_assignments (position_id, employee_id, valid_from, valid_to)
|
|
values (v_position, v_employee_id, v_effective_date,
|
|
(select e.exit_date from employees e
|
|
where e.id = v_employee_id and e.exit_date > v_effective_date));
|
|
$e2$),
|
|
|
|
-- ── apply_due_pending_changes, Zweig 'transfer' ───────────────────
|
|
('apply_due_pending_changes', $a3$
|
|
delete from position_assignments
|
|
where employee_id = v_rec.employee_id and valid_to is null and valid_from >= v_rec.effective_date;
|
|
update position_assignments set valid_to = v_rec.effective_date
|
|
where employee_id = v_rec.employee_id and valid_to is null;
|
|
insert into position_assignments (position_id, employee_id, valid_from)
|
|
values ((v_rec.payload->>'target_position_id')::uuid, v_rec.employee_id, v_rec.effective_date);
|
|
$a3$, $e3$
|
|
delete from position_assignments
|
|
where employee_id = v_rec.employee_id and valid_from >= v_rec.effective_date
|
|
and (valid_to is null or valid_to > v_rec.effective_date);
|
|
update position_assignments set valid_to = v_rec.effective_date
|
|
where employee_id = v_rec.employee_id and (valid_to is null or valid_to > v_rec.effective_date);
|
|
insert into position_assignments (position_id, employee_id, valid_from, valid_to)
|
|
values ((v_rec.payload->>'target_position_id')::uuid, v_rec.employee_id, v_rec.effective_date,
|
|
(select e.exit_date from employees e
|
|
where e.id = v_rec.employee_id and e.exit_date > v_rec.effective_date));
|
|
$e3$),
|
|
|
|
-- ── apply_due_pending_changes, Zweig 'promotion' ──────────────────
|
|
('apply_due_pending_changes', $a4$
|
|
delete from position_assignments
|
|
where employee_id = v_rec.employee_id and valid_to is null and valid_from >= v_rec.effective_date;
|
|
update position_assignments set valid_to = v_rec.effective_date
|
|
where employee_id = v_rec.employee_id and valid_to is null;
|
|
insert into position_assignments (position_id, employee_id, valid_from)
|
|
values ((v_rec.payload->>'target_position_id')::uuid, v_rec.employee_id, v_rec.effective_date);
|
|
$a4$, $e4$
|
|
delete from position_assignments
|
|
where employee_id = v_rec.employee_id and valid_from >= v_rec.effective_date
|
|
and (valid_to is null or valid_to > v_rec.effective_date);
|
|
update position_assignments set valid_to = v_rec.effective_date
|
|
where employee_id = v_rec.employee_id and (valid_to is null or valid_to > v_rec.effective_date);
|
|
insert into position_assignments (position_id, employee_id, valid_from, valid_to)
|
|
values ((v_rec.payload->>'target_position_id')::uuid, v_rec.employee_id, v_rec.effective_date,
|
|
(select e.exit_date from employees e
|
|
where e.id = v_rec.employee_id and e.exit_date > v_rec.effective_date));
|
|
$e4$)
|
|
) as t(funktion, anker, ersatz)
|
|
loop
|
|
select pg_get_functiondef(p.oid) into v_def
|
|
from pg_proc p
|
|
join pg_namespace n on n.oid = p.pronamespace
|
|
where n.nspname = 'public' and p.prokind = 'f' and p.proname = r.funktion;
|
|
|
|
if v_def is null then
|
|
raise exception '% ist nicht vorhanden — hier stimmt etwas Grundsätzlicheres nicht.', r.funktion;
|
|
end if;
|
|
|
|
-- Schon umgestellt: überspringen statt abbrechen. Die Buchführung in
|
|
-- migrationen.schema_migrations verhindert den zweiten Lauf ohnehin.
|
|
if position(r.ersatz in v_def) > 0 then
|
|
raise notice '%: der Block ist bereits umgestellt — nichts zu tun.', r.funktion;
|
|
continue;
|
|
end if;
|
|
|
|
-- Zählen statt „kommt vor" prüfen: trifft ein Anker zweimal, ersetzt
|
|
-- replace() beide, und der Rumpf wird still unbrauchbar.
|
|
v_treffer := (length(v_def) - length(replace(v_def, r.anker, ''))) / length(r.anker);
|
|
if v_treffer <> 1 then
|
|
raise exception '%: der Anker kommt % mal vor, erwartet genau einmal.', r.funktion, v_treffer;
|
|
end if;
|
|
|
|
execute replace(v_def, r.anker, r.ersatz);
|
|
|
|
-- Gegenprobe am lebenden Katalog und nicht an der soeben gebauten
|
|
-- Zeichenkette: nur so ist belegt, dass create or replace
|
|
-- auch angekommen ist.
|
|
select pg_get_functiondef(p.oid) into v_def
|
|
from pg_proc p
|
|
join pg_namespace n on n.oid = p.pronamespace
|
|
where n.nspname = 'public' and p.prokind = 'f' and p.proname = r.funktion;
|
|
|
|
if position(r.ersatz in v_def) = 0 then
|
|
raise exception '%: der neue Block steht nach der Ersetzung nicht im Rumpf.', r.funktion;
|
|
end if;
|
|
if position(r.anker in v_def) > 0 then
|
|
raise exception '%: der alte Block steht nach der Ersetzung immer noch im Rumpf.', r.funktion;
|
|
end if;
|
|
end loop;
|
|
end
|
|
$migration$;
|
|
|
|
-- ── Selbstprüfung ─────────────────────────────────────────────────────────
|
|
--
|
|
-- Geprüft wird nicht nur das neue Verhalten, sondern auch das, was bei
|
|
-- create or replace leicht verlorengeht.
|
|
do $pruefung$
|
|
declare
|
|
v_name text;
|
|
v_rumpf text;
|
|
v_def text;
|
|
v_secdef boolean;
|
|
v_rechte boolean;
|
|
v_config text[];
|
|
v_treffer integer;
|
|
begin
|
|
-- ── Alle drei Funktionen: die erweiterte Klausel ist da ────────────────
|
|
--
|
|
-- Gesucht wird „or valid_to > " ohne Tabellenpräfix. transfer_employee
|
|
-- trägt seit 20260929200000 ein „or pa.valid_to > " in der Belegtprüfung;
|
|
-- mit Präfix gesucht, ginge die Prüfung auch bei misslungener Ersetzung
|
|
-- durch.
|
|
foreach v_name in array array['transfer_employee', 'promote_employee', 'apply_due_pending_changes']
|
|
loop
|
|
select pg_get_functiondef(p.oid), p.prosecdef, p.proconfig
|
|
into v_rumpf, v_secdef, v_config
|
|
from pg_proc p
|
|
join pg_namespace n on n.oid = p.pronamespace
|
|
where n.nspname = 'public' and p.prokind = 'f' and p.proname = v_name;
|
|
|
|
if position('or valid_to > ' in v_rumpf) = 0 then
|
|
raise exception '%: die erweiterte Schliessklausel fehlt.', v_name;
|
|
end if;
|
|
|
|
-- Die neue Besetzung muss das Austrittsdatum mitbekommen.
|
|
if position('e.exit_date >' in v_rumpf) = 0 then
|
|
raise exception '%: die neue Besetzung wird weiterhin unbefristet eingefügt.', v_name;
|
|
end if;
|
|
|
|
if v_config is null or array_to_string(v_config, ',') not like '%search_path%' then
|
|
raise exception '%: SET search_path ist verlorengegangen.', v_name;
|
|
end if;
|
|
end loop;
|
|
|
|
-- ── Das Rechtemodell der drei Funktionen ──────────────────────────────
|
|
--
|
|
-- Hier stehen zwei gegenläufige Erwartungen, und sie gehören zusammen:
|
|
--
|
|
-- apply_due_pending_changes läuft ohne angemeldete Person. Sie braucht
|
|
-- deshalb security definer und darf require_hr_admin() gerade **nicht**
|
|
-- rufen — täte sie es, stünde der nächtliche Lauf jede Nacht still.
|
|
--
|
|
-- transfer_employee und promote_employee ruft ein Mensch. Sie laufen
|
|
-- unter RLS (security invoker) und prüfen die Rechte selbst. Bekämen sie
|
|
-- security definer, liefen sie an den Richtlinien vorbei — das wäre
|
|
-- keine verlorene Eigenschaft, sondern eine neu entstandene Lücke.
|
|
--
|
|
-- Beide Richtungen werden geprüft, weil ein create or replace aus einer
|
|
-- falschen Vorlage in beide Richtungen danebengreifen kann.
|
|
select p.prosecdef, position('require_hr_admin()' in pg_get_functiondef(p.oid)) > 0
|
|
into v_secdef, v_rechte
|
|
from pg_proc p
|
|
join pg_namespace n on n.oid = p.pronamespace
|
|
where n.nspname = 'public' and p.prokind = 'f' and p.proname = 'apply_due_pending_changes';
|
|
|
|
if not v_secdef then
|
|
raise exception 'apply_due_pending_changes: security definer ist verlorengegangen — der nächtliche Lauf sieht unter RLS nichts.';
|
|
end if;
|
|
if v_rechte then
|
|
raise exception 'apply_due_pending_changes: require_hr_admin() ist hineingeraten — der nächtliche Lauf hat keine angemeldete Person.';
|
|
end if;
|
|
|
|
foreach v_name in array array['transfer_employee', 'promote_employee']
|
|
loop
|
|
select p.prosecdef, position('require_hr_admin()' in pg_get_functiondef(p.oid)) > 0
|
|
into v_secdef, v_rechte
|
|
from pg_proc p
|
|
join pg_namespace n on n.oid = p.pronamespace
|
|
where n.nspname = 'public' and p.prokind = 'f' and p.proname = v_name;
|
|
|
|
if v_secdef then
|
|
raise exception '%: security definer ist hinzugekommen — die Funktion liefe an RLS vorbei.', v_name;
|
|
end if;
|
|
if not v_rechte then
|
|
raise exception '%: require_hr_admin ist verlorengegangen.', v_name;
|
|
end if;
|
|
end loop;
|
|
|
|
select pg_get_functiondef(p.oid) into v_def
|
|
from pg_proc p
|
|
join pg_namespace n on n.oid = p.pronamespace
|
|
where n.nspname = 'public' and p.prokind = 'f' and p.proname = 'apply_due_pending_changes';
|
|
|
|
-- Der Unterblock je Vormerkung aus 20261013100000. Ohne ihn hielte eine
|
|
-- einzige unausführbare Zeile wieder den ganzen Lauf auf.
|
|
if position('Geplante Änderung fehlgeschlagen' in v_def) = 0 then
|
|
raise exception 'apply_due_pending_changes: die Fehlerbehandlung je Vormerkung ist verlorengegangen.';
|
|
end if;
|
|
|
|
-- Die Gruppe `role` (Betriebsrat, Dienstwagen, KV, Arbeitstage, Teilzeit,
|
|
-- Kündigungsschutz). Sie darf bei einer Neuerzeugung nicht verlorengehen.
|
|
if position($r$v_rec.payload->'role'$r$ in v_def) = 0 then
|
|
raise exception 'apply_due_pending_changes: die Gruppe role ist verlorengegangen.';
|
|
end if;
|
|
|
|
-- Beide Zweige müssen die Zielplanstelle weiterhin kennen — Zählen und
|
|
-- nicht Vorhandensein prüfen, weil der Name in beiden Zweigen steht.
|
|
v_treffer := (length(v_def) - length(replace(v_def, 'target_position_id', ''))) / length('target_position_id');
|
|
if v_treffer < 2 then
|
|
raise exception 'apply_due_pending_changes: target_position_id kommt nur % mal vor, erwartet mindestens zweimal.', v_treffer;
|
|
end if;
|
|
|
|
-- Beide Zweige tragen die neue Form — zweimal, nicht einmal.
|
|
v_treffer := (length(v_def) - length(replace(v_def, 'e.exit_date >', ''))) / length('e.exit_date >');
|
|
if v_treffer <> 2 then
|
|
raise exception 'apply_due_pending_changes: die befristete Einfügung steht % mal im Rumpf, erwartet genau zweimal.', v_treffer;
|
|
end if;
|
|
|
|
end
|
|
$pruefung$;
|