Files
alpenwerk-hr/supabase/migrations/20260810110000_fix_hire_workdays_default.sql
Maximilian Stubhan 92685f0ac0 Only staff a position while it exists
hire_employee and transfer_employee checked whether the position was free,
never whether it was there. Someone could be hired today onto a position
that starts in October, or onto one that lapsed in spring: the assignment
sat in the database while the position was absent from the org chart, and
the person hung off a structure that did not exist on their entry date.

That stopped being theoretical when the positions view began showing future
positions — they now appear in the same picker the hire wizard uses. This is
the rule that makes showing them safe.

The date of the assignment must fall in [valid_from, valid_to). valid_to is
exclusive throughout the model, as in lib/positions.ts.

Second correction in the same place: occupancy only looked at assignments
with an open end, so one ending later was invisible and the position could
be double-booked — the same gap the vacancy list had.

And a defect the verification exposed rather than the report: the work_days
default in hire_employee never applied. `array(select …)` over a missing key
yields an empty array, not null, so coalesce kept `{}` and the CHECK
constraint refused the row. Invisible through the wizard, which always sends
them and will not proceed without — but a default that defaults to nothing
is worse than none, because it reads as though the case was considered.

Verified against the live database, all rolled back: a hire onto a future
position is refused naming the date it begins, a transfer likewise, a hire
onto a currently valid one succeeds — and now also succeeds without
work_days, arriving with Mo–Fr.

The migrations match on a pattern rather than literal text: the function
bodies carry CRLF, and a literal search would have found nothing while the
migration reported success. Both refuse to proceed if the pattern matches
nothing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-10 16:10:18 +02:00

65 lines
2.6 KiB
SQL

-- Der Vorgabewert für die Arbeitstage in hire_employee greift nie.
--
-- coalesce(array(select jsonb_array_elements_text(payload->'work_days'))::text[],
-- '{Mo,Di,Mi,Do,Fr}')
--
-- Fehlt der Schlüssel, liefert die Unterabfrage keine Zeilen, und `array(…)`
-- macht daraus ein **leeres** Array — nicht NULL. `coalesce` sieht also
-- keinen fehlenden Wert und lässt `{}` stehen. Die Bedingung
-- chk_work_days_valid verlangt aber mindestens einen Tag, und die
-- Einstellung bricht ab.
--
-- Sichtbar ist das bisher nicht, weil der Assistent die Arbeitstage immer
-- mitschickt und ohne sie gar nicht weiterlässt. Es ist eine Falle für jeden
-- anderen Aufrufer — und ein Vorgabewert, der nichts vorgibt, ist schlimmer
-- als keiner: er sieht aus, als wäre der Fall bedacht.
--
-- `nullif(…, '{}')` macht aus dem leeren Array wieder ein fehlendes.
do $$
declare
v_def text;
v_neu text;
begin
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.proname = 'hire_employee'
limit 1;
v_neu := regexp_replace(
v_def,
'coalesce\(\s*array\(\s*select\s+jsonb_array_elements_text\(payload->''work_days''\)\s*\)\s*::text\[\]\s*,',
'coalesce(nullif(array(select jsonb_array_elements_text(payload->''work_days''))::text[], ''{}''),',
'g'
);
if v_neu = v_def then
raise exception 'Das Muster für die Arbeitstage passte nicht — hire_employee blieb unverändert.';
end if;
execute v_neu;
end;
$$;
-- ═══ Gegenprobe ══════════════════════════════════════════════════
-- Der Ausdruck in beiden Formen, damit die Regel festgehalten ist und nicht
-- beim nächsten Mal neu entdeckt werden muss.
do $$
begin
if array(select jsonb_array_elements_text('{}'::jsonb->'work_days')) is null then
raise exception 'array() über einen fehlenden Schlüssel liefert null — die Annahme dieser Migration stimmt nicht mehr.';
end if;
if coalesce(nullif(array(select jsonb_array_elements_text('{}'::jsonb->'work_days'))::text[], '{}'), '{Mo,Di,Mi,Do,Fr}')
<> '{Mo,Di,Mi,Do,Fr}'::text[] then
raise exception 'Der korrigierte Ausdruck liefert nicht die Vorgabe.';
end if;
if coalesce(nullif(array(select jsonb_array_elements_text('{"work_days":["Mo","Di"]}'::jsonb->'work_days'))::text[], '{}'), '{Mo,Di,Mi,Do,Fr}')
<> '{Mo,Di}'::text[] then
raise exception 'Der korrigierte Ausdruck überschreibt einen mitgegebenen Wert.';
end if;
end;
$$;