diff --git a/db/migrations/20261029200000_planstelle_mit_bezeichnung_in_historie.sql b/db/migrations/20261029200000_planstelle_mit_bezeichnung_in_historie.sql new file mode 100644 index 0000000..d843d55 --- /dev/null +++ b/db/migrations/20261029200000_planstelle_mit_bezeichnung_in_historie.sql @@ -0,0 +1,106 @@ +-- Planstelle mit Bezeichnung in der Historie +-- +-- Fachlich vorgegeben: bei einer Versetzung steht in der Historie neben der +-- Nummer der Planstelle auch ihre Bezeichnung — „Versetzung auf Planstelle +-- 60000124 (Sachbearbeiter)" statt nur der Nummer, unter der sich niemand +-- etwas vorstellen kann. Eintritt und Wiedereintritt schreiben ihre +-- Planstelle in derselben Form, damit die Historie einer Person einheitlich +-- liest. +-- +-- Die Bezeichnung ist die Tätigkeit der Planstelle zum Zeitpunkt des +-- Eintrags. Wird die Planstelle später umbenannt (Beförderung ohne +-- Stellenwechsel), bleibt in der Historie die damalige Bezeichnung stehen — +-- so, wie es damals war. +-- +-- Bestehende Einträge bekommen die Bezeichnung nachträglich, mit der +-- heutigen Tätigkeit der Planstelle. Eine frühere gibt es nicht mehr; ohne +-- Nachtrag stünden alte und neue Einträge in zwei Formen untereinander. + +create or replace function public.planstelle_text(p_position_id uuid) +returns text +language sql +stable +set search_path = public, pg_temp +as $$ + select p.position_number || ' (' || j.title || ')' + from om_positions p join jobs j on j.id = p.job_id + where p.id = p_position_id; +$$; + +grant execute on function public.planstelle_text(uuid) to alpenwerk_app; + +-- Geändert wird an den laufenden Definitionen, an je einem Anker, der genau +-- einmal vorkommen muss. +do $migration$ +declare + v_ziele text[][] := array[ + array['transfer_employee', + $a$'Versetzung auf Planstelle ' || + (select position_number from om_positions where id = v_target_position)$a$, + $a$'Versetzung auf Planstelle ' || planstelle_text(v_target_position)$a$], + array['hire_employee', + $a$'Eintritt auf Planstelle ' || + (select position_number from om_positions where id = v_position_id)$a$, + $a$'Eintritt auf Planstelle ' || planstelle_text(v_position_id)$a$], + array['rehire_employee', + $a$' auf Planstelle ' || + (select position_number from om_positions where id = v_position_id)$a$, + $a$' auf Planstelle ' || planstelle_text(v_position_id)$a$] + ]; + v_def text; + v_treffer integer; + i integer; +begin + for i in 1 .. array_length(v_ziele, 1) 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 = v_ziele[i][1]; + if v_def is null then + raise exception '% ist nicht vorhanden.', v_ziele[i][1]; + end if; + if position(v_ziele[i][3] in v_def) > 0 then + continue; + end if; + v_treffer := (length(v_def) - length(replace(v_def, v_ziele[i][2], ''))) / length(v_ziele[i][2]); + if v_treffer <> 1 then + raise exception '%: der Anker kommt % mal vor, erwartet genau einmal.', v_ziele[i][1], v_treffer; + end if; + execute replace(v_def, v_ziele[i][2], v_ziele[i][3]); + end loop; +end +$migration$; + +-- ── Bestehende Einträge ───────────────────────────────────────────────── +-- Nur, wo genau die Nummer am Ende steht: ein von Hand berichtigter Text +-- (update_history_entry) bleibt, wie er ist. +update employee_history h + set description = h.description || ' (' || j.title || ')' + from om_positions p join jobs j on j.id = p.job_id + where h.event_type in ('Versetzung', 'Eintritt', 'Wiedereintritt') + and h.description ~ ' auf Planstelle [^ ]+$' + and substring(h.description from ' auf Planstelle ([^ ]+)$') = p.position_number; + +do $pruefung$ +declare + v_fn text; + v_def text; +begin + foreach v_fn in array array['transfer_employee', 'hire_employee', 'rehire_employee'] 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 = v_fn; + if position('planstelle_text(' in v_def) = 0 then + raise exception '% schreibt die Planstelle ohne Bezeichnung.', v_fn; + end if; + if position(E'begin\n perform require_hr_admin();' in v_def) = 0 + or position('insert into audit_log' in v_def) = 0 then + raise exception '% hat Rechteprüfung oder Protokoll verloren.', v_fn; + end if; + end loop; + -- Die Prüfung, ob die Zielplanstelle frei ist, darf beim Umschreiben nicht verloren gehen. + select pg_get_functiondef('public.transfer_employee'::regproc) into v_def; + if position('Die Zielplanstelle ist bereits besetzt' in v_def) = 0 then + raise exception 'transfer_employee prüft die Zielplanstelle nicht mehr.'; + end if; +end +$pruefung$; diff --git a/db/tests/rauchtest.sql b/db/tests/rauchtest.sql index ab9abcd..d1bfc73 100644 --- a/db/tests/rauchtest.sql +++ b/db/tests/rauchtest.sql @@ -98,7 +98,14 @@ begin if v_stelle is distinct from v_p2 then raise exception 'Nach der Versetzung sitzt die Person nicht auf Planstelle 2.'; end if; - raise notice '3/34 Versetzung am selben Tag'; + -- Die Historie nennt Nummer und Bezeichnung der Planstelle (20261029200000). + if not exists (select 1 from employee_history + where employee_id = v_a and event_type = 'Versetzung' + and description = 'Versetzung auf Planstelle ' + || (select position_number from om_positions where id = v_p2) || ' (Rauchtest Zwei)') then + raise exception 'Die Versetzung steht ohne Bezeichnung der Planstelle in der Historie.'; + end if; + raise notice '3/34 Versetzung am selben Tag, Planstelle mit Bezeichnung in der Historie'; -- ── Beförderung mit Planstellenwechsel, ebenfalls heute ───────── perform promote_employee(jsonb_build_object(