From 89807a00e3261fdcdfa515d0d75ece33daf5460a Mon Sep 17 00:00:00 2001 From: Andrei Laas Date: Tue, 13 Oct 2026 14:07:20 +0200 Subject: [PATCH] Eine Versetzung hinterlaesst keine zweite Besetzung Gefunden an E2E-CLAUDE Anna-01: sie sass gleichzeitig auf 60000006 (bis 31.12.) und 60000009 (offen). Die Schliessklausel suchte nach "valid_to is null" und traf nichts mehr, weil ein datierter Austritt die alte Besetzung bereits befristet hatte. Geschlossen wurde nichts, eingefuegt trotzdem. Die zweite Haelfte steckte im values-Teil: die neue Besetzung wurde ohne valid_to eingefuegt. Eine ausgetretene Person belegte ihre Planstelle damit unbefristet weiter. Vier Bloecke in drei Funktionen, jeder als Ganzes ersetzt, Anker aus dem lebenden Katalog, Gegenprobe nach der Ersetzung. Das delete davor wird mitgezogen: sonst entstuende aus der erweiterten Schliessklausel ein Intervall von null Tagen. --- ...ng_hinterlaesst_keine_zweite_besetzung.sql | 286 ++++++++++++++++++ 1 file changed, 286 insertions(+) create mode 100644 db/migrations/20261013140000_eine_versetzung_hinterlaesst_keine_zweite_besetzung.sql diff --git a/db/migrations/20261013140000_eine_versetzung_hinterlaesst_keine_zweite_besetzung.sql b/db/migrations/20261013140000_eine_versetzung_hinterlaesst_keine_zweite_besetzung.sql new file mode 100644 index 0000000..49a4dfd --- /dev/null +++ b/db/migrations/20261013140000_eine_versetzung_hinterlaesst_keine_zweite_besetzung.sql @@ -0,0 +1,286 @@ +-- Eine Versetzung schliesst die alte Besetzung auch dann, wenn ein datierter +-- Austritt sie bereits befristet hat — und die neue endet mit der Person. +-- +-- ── Der Befund ──────────────────────────────────────────────────────────── +-- +-- Gefunden am 13.10.2026 an E2E-CLAUDE Anna-01 (99901). Sie sass gleichzeitig +-- auf zwei Planstellen: +-- +-- 60000006 2026-09-15 bis 2026-12-31 +-- 60000009 2026-10-01 bis offen +-- +-- Der Hergang: zuerst wurde ein Austritt erfasst, der die laufende Besetzung +-- auf den Austrittstag befristete (valid_to = 2026-12-31). Später übernahm der +-- nächtliche Lauf eine auf den 01.10. vorgemerkte Versetzung. Deren +-- Schliessklausel lautete +-- +-- where employee_id = … and valid_to is null +-- +-- und traf damit nichts mehr: die alte Besetzung war ja nicht offen, sondern +-- befristet. Geschlossen wurde nichts, eingefügt wurde trotzdem — zwei +-- gleichzeitige Besetzungen. +-- +-- Auffallen konnte 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 ─────────────────────── +-- +-- Die neue Besetzung wird ohne valid_to eingefügt. Bei einer Person mit +-- eingetragenem Austritt heisst das: sie verlässt am 31.10. 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). Es sucht +-- ebenfalls nach `valid_to is null`. Würde nur die Schliessklausel erweitert, +-- 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. Das ist die +-- Konstellation, an der dieses Projekt schon zweimal eine halbe Korrektur +-- hinterlassen hat; deshalb 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 hat hier am 29.09. bereits +-- danebengetroffen. +-- +-- 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 Zeichenkette, die wir + -- gerade selbst gebaut haben: 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 in diesem +-- Projekt bei create or replace schon einmal verlorengegangen ist. Die Liste +-- stammt aus den tatsächlichen Vorfällen, nicht aus Vorsicht. +do $pruefung$ +declare + v_name text; + v_rumpf text; + v_def text; + v_secdef 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 not v_secdef then + raise exception '%: security definer ist verlorengegangen.', 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; + + 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). Genau sie ist hier schon einmal still verschwunden. + 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; + + -- Die von Hand aufgerufenen Funktionen prüfen weiterhin die Rechte. + -- apply_due_pending_changes steht bewusst nicht in dieser Liste: der + -- nächtliche Lauf hat keine angemeldete Person und darf sie nicht fordern. + foreach v_name in array array['transfer_employee', 'promote_employee'] + loop + select pg_get_functiondef(p.oid) into v_rumpf + 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('require_hr_admin()' in v_rumpf) = 0 then + raise exception '%: require_hr_admin ist verlorengegangen.', v_name; + end if; + end loop; +end +$pruefung$;