Files
alpenwerk-hr/supabase/migrations/20260810130000_fix_rehire_status_cast.sql
Maximilian Stubhan f28fd2da60 Let a rehire choose the position, and make it work at all
Clicking "Wiedereinstellen" could never succeed. rehire_employee has always
demanded a position and refuses without one, but the panel offered only a
date and sent only a date — so every rehire ended on an error the dialog
gave no way to fix.

The panel now picks from the open positions, the same list and layout the
transfer panel uses, and warns before submitting when the date falls outside
the chosen position's validity. The old position is deliberately not a
silent default: it may since have been filled, ended, or gone.

Behind that sat a second fault, hidden by the first: the status assignment

    status = case when v_date <= current_date then 'Aktiv' else 'Geplant' end

is text, and the column is employment_status. Postgres refuses that outright,
so the function would have failed even with a position. It surfaced only once
the earlier check stopped firing — the same pattern as hire_employee this
morning, where three faults sat in a queue.

rehire_employee also placed people without checking anything. It now applies
the rule from 20260810100000: the date must lie in the position's validity,
and no assignment may still stand. A rehire could otherwise land on an
occupied position and be caught by the partial index, with a message that
explains nothing.

My first verification of the cast was wrong and passed a broken state:
plpgsql converts silently when assigning to a variable, so the probe proved
nothing. Redone as an UPDATE against a column, which is the case that fails.

Verified end to end against the live database, rolled back: Stefan Egger
returns as Aktiv on a free position, with the assignment and the
Wiedereintritt entry. Without a position, on an occupied one, and on one not
yet valid, it is refused — each with its own message.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-11 19:29:08 +02:00

66 lines
2.4 KiB
SQL

-- rehire_employee funktionierte nie.
--
-- status = case when v_date <= current_date then 'Aktiv' else 'Geplant' end
--
-- Der case-Ausdruck ist `text`, die Spalte ist `employment_status`. Postgres
-- weist das ab:
--
-- column "status" is of type employment_status but expression is of type text
--
-- Sichtbar wurde es erst jetzt. Davor brach die Funktion eine Zeile früher ab,
-- weil das Formular nie eine Planstelle mitschickte — ein Fehler verdeckte den
-- anderen, wie schon bei hire_employee.
--
-- Bei den beiden anderen Zuweisungen im selben update (exit_date, exit_reason)
-- stellt sich die Frage nicht: `null` ist typunabhängig.
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 = 'rehire_employee'
limit 1;
v_neu := regexp_replace(
v_def,
'(case\s+when\s+v_date\s*<=\s*current_date\s+then\s+''Aktiv''\s+else\s+''Geplant''\s+end)',
'(\1)::employment_status',
'g'
);
if v_neu = v_def then
raise exception 'Das Muster für den Status passte nicht — rehire_employee blieb unverändert.';
end if;
execute v_neu;
end;
$$;
-- ═══ Gegenprobe ══════════════════════════════════════════════════
-- Nachgestellt wird die **Spaltenzuweisung**, nicht die an eine Variable.
-- Das ist der Unterschied, an dem mein erster Prüfausdruck vorbeiging:
-- plpgsql wandelt bei einer Variablenzuweisung stillschweigend um, ein
-- UPDATE auf eine Spalte nicht. Nur die zweite Form entspricht dem Fehler.
do $$
begin
create temp table probe_status (s employment_status) on commit drop;
insert into probe_status values ('Aktiv');
begin
execute $probe$ update probe_status set s = (case when true then 'Aktiv' else 'Geplant' end)::text $probe$;
raise exception 'Eine text-Zuweisung an eine employment_status-Spalte wirft nicht mehr — die Prüfung ist wertlos geworden.';
exception
when datatype_mismatch then null; -- erwartet
end;
execute $probe$ update probe_status set s = (case when true then 'Aktiv' else 'Geplant' end)::employment_status $probe$;
if (select s from probe_status) <> 'Aktiv'::employment_status then
raise exception 'Die umgewandelte Form liefert nicht Aktiv.';
end if;
end;
$$;