Files
alpenwerk-hr/db/migrations/20260724120000_employee_assignment_history.sql
Raphael Müllner b47dec6387 Übergabestand 2026-10-14
Alpenwerk HR — HR-Stammdatenverwaltung

Mitarbeiter:innen, Organisationsstruktur, Planstellen, Eintritt, Austritt,
Versetzung, Beförderung, Karenz, On- und Offboarding, Berichte, Exporte,
Massenimport und ein lückenloser Nachweis jeder Änderung.

Next.js 16, React 19 und TypeScript; PostgreSQL über Kysely; Anmeldung
ausschliesslich über Microsoft Entra ID. Die Fachlogik liegt in der
Datenbank, die Zugriffsschranke ist Row Level Security.

Betrieb auf Windows Server: siehe deploy/windows/README.md.
Sicherheitskonzept: docs/sicherheitskonzept.md.
2026-10-14 10:12:22 +02:00

120 lines
6.3 KiB
PL/PgSQL

-- Org-assignment history, so the Organigramm can be shown as of any date.
--
-- `employees` carries only the *current* placement (manager_id, team_id,
-- division_id, job_title, is_lead, org_level), and every mutation overwrites
-- it in place. employee_history records that something happened, but only
-- as free text — no old/new values. Forward-looking dates are covered by
-- pending_org_changes (it holds not-yet-due changes structurally); this
-- table holds the past, so a past reporting line can be reconstructed.
--
-- Captured by a trigger rather than by editing the mutating RPCs: there are
-- many `update employees` statements spread over the RPCs (several of which
-- are redefined repeatedly), so per-RPC bookkeeping would easily miss a
-- path, now or in a future RPC. A trigger on the table catches all of them,
-- including ones not written yet.
create table employee_assignments (
id uuid primary key default gen_random_uuid(),
employee_id uuid not null references employees(id) on delete cascade,
manager_id uuid references employees(id) on delete set null,
team_id uuid references teams(id),
division_id uuid not null references divisions(id),
job_title text not null,
is_lead boolean not null default false,
org_level int not null,
valid_from date not null,
-- Exclusive upper bound; null means "still in force". Note this tracks
-- *placement*, not employment: the row of someone who has left stays open,
-- because whether they were employed on a given date is derived separately
-- from entry/exit/karenz dates. Keeping the two apart is what lets a
-- rehire reuse the same open row instead of needing it reopened.
valid_to date,
created_at timestamptz not null default now(),
constraint chk_assignment_range check (valid_to is null or valid_to > valid_from)
);
create index on employee_assignments (employee_id, valid_from desc);
create index on employee_assignments (valid_from);
-- At most one open interval per employee — the invariant the trigger relies
-- on when it looks up "the current row" to close.
create unique index employee_assignments_one_open on employee_assignments (employee_id) where valid_to is null;
alter table employee_assignments enable row level security;
create policy "employee_assignments_hr_read" on employee_assignments for select using (is_hr_user());
-- ── Backfill ───────────────────────────────────────────────────────
-- One open interval per employee, starting at their entry date, holding
-- the placement at the time of the backfill. Earlier changes are not
-- recoverable — employee_history holds no structured old values — so for
-- anyone whose assignment predates this table, a Stichtag before the
-- backfill shows the placement as it stood at backfill time. Everything
-- recorded afterwards is exact.
insert into employee_assignments (employee_id, manager_id, team_id, division_id, job_title, is_lead, org_level, valid_from)
select id, manager_id, team_id, division_id, job_title, is_lead, org_level, entry_date
from employees;
-- ── Trigger ────────────────────────────────────────────────────────
-- The date a change takes effect is normally the day it is written: an RPC
-- with a future effective date does not touch `employees` at all (it queues
-- into pending_org_changes, and apply_due_pending_changes writes it on the
-- due date), and one dated today writes immediately. A *backdated* change is
-- the exception — it writes immediately although it should take effect
-- earlier — so callers may set `app.effective_date` for the transaction to
-- say so explicitly; unset, it falls back to the current date.
create or replace function fn_track_employee_assignment()
returns trigger
language plpgsql
security definer
set search_path = public
as $$
declare
v_date date := coalesce(nullif(current_setting('app.effective_date', true), '')::date, current_date);
v_open_from date;
begin
if tg_op = 'INSERT' then
insert into employee_assignments (employee_id, manager_id, team_id, division_id, job_title, is_lead, org_level, valid_from)
values (new.id, new.manager_id, new.team_id, new.division_id, new.job_title, new.is_lead, new.org_level,
coalesce(new.entry_date, v_date));
return new;
end if;
if new.manager_id is not distinct from old.manager_id
and new.team_id is not distinct from old.team_id
and new.division_id is not distinct from old.division_id
and new.job_title is not distinct from old.job_title
and new.is_lead is not distinct from old.is_lead
and new.org_level is not distinct from old.org_level then
return new;
end if;
select valid_from into v_open_from
from employee_assignments where employee_id = new.id and valid_to is null;
if v_open_from is null then
insert into employee_assignments (employee_id, manager_id, team_id, division_id, job_title, is_lead, org_level, valid_from)
values (new.id, new.manager_id, new.team_id, new.division_id, new.job_title, new.is_lead, new.org_level, v_date);
elsif v_date <= v_open_from then
-- Two changes on the same day (or a backdate landing inside the open
-- interval): overwrite in place, so no zero-length or inverted interval
-- is ever stored.
update employee_assignments
set manager_id = new.manager_id, team_id = new.team_id, division_id = new.division_id,
job_title = new.job_title, is_lead = new.is_lead, org_level = new.org_level
where employee_id = new.id and valid_to is null;
else
update employee_assignments set valid_to = v_date where employee_id = new.id and valid_to is null;
insert into employee_assignments (employee_id, manager_id, team_id, division_id, job_title, is_lead, org_level, valid_from)
values (new.id, new.manager_id, new.team_id, new.division_id, new.job_title, new.is_lead, new.org_level, v_date);
end if;
return new;
end;
$$;
drop trigger if exists trg_track_employee_assignment on employees;
create trigger trg_track_employee_assignment
after insert or update on employees
for each row execute function fn_track_employee_assignment();
grant all on table employee_assignments to anon, authenticated, service_role;