Files
alpenwerk-hr/db/migrations/20260816100000_kostenstellen.sql
Maximilian Stubhan b87c8ad64c
Some checks failed
CI / Lint, Typen, Tests, Build (push) Failing after 5m40s
CI / Migrationen auf leerer Datenbank (push) Has been cancelled
Remove Supabase
The database moved to a container of our own; the platform is gone.
This takes out what was left of it — and, where the leftovers were load
bearing, moves rather than deletes.

Moved, not deleted:

  supabase/migrations/  -> db/migrations/      the schema's source of truth
  supabase/build-org.ts -> scripts/build-org.ts
  lib/supabase/types.ts -> lib/types.ts        52 import sites repointed

The bookkeeping needed care. It lived in `supabase_migrations.schema_migrations`,
and simply renaming the schema would have left the runner facing an empty
table: it would have called all 67 migrations pending and replayed them
against a database that is long since current. So the runner now creates
`migrationen.schema_migrations` and, once, copies the old rows across —
guarded so a second run does nothing and a fresh database skips it entirely.
Only then does migration 20260907100000 drop the old schema.

Deleted: the CLI config, the seed, the historical schema/function dumps
(nothing read them), scripts/umzug-von-supabase.sh (the move is done), and
both Supabase packages plus the CLI. Nothing in the application imported
them — the build now succeeds with no environment variables at all, which
is the proof.

Integration tests: six of them signed in through Supabase Auth and asserted
against the anon key and the service role. That model is gone, so the tests
were not portable — they are deleted. session-context and
employee-status-filter already ran on pg and are untouched; om-reporting is
ported to a direct connection because it guards a real risk (the reporting
line rule exists twice, once in SQL and once in TypeScript).

CI: the integration job started a Supabase stack. It now runs a postgres
service, applies deploy/db-init and every migration to an empty database —
that was the valuable part, and it still holds — then checks that a second
run is a no-op, which is what proves the bookkeeping works.

Docs: security-review.md audited a service-role key, a cookie adapter and
auth.users, none of which exist. Restating findings about removed components
would suggest today's system had been reviewed; it has not. It now records
what was removed and says a fresh review is due. data-model.md was already
marked obsolete and described the pre-OM schema; azure-migration.md was a
plan for a route not taken. Both deleted.

Verified: npm ci, typecheck, lint, 445 tests, build — all clean without the
packages. Integration tests skip cleanly with no database. Migration SQL and
the runner are reviewed but NOT executed: no Docker here, and the old
instance no longer resolves.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-07 10:43:22 +02:00

172 lines
7.8 KiB
PL/PgSQL

-- Kostenstellen
--
-- Die Kostenstelle hing bisher nirgends. Damit war keine einzige Auswertung
-- nach Personalkosten möglich, und eine offene Planstelle konnte nicht sagen,
-- wessen Budget sie belastet.
--
-- Sie hängt an der **Planstelle**, nicht an der Person: der Sitz kostet Geld,
-- auch wenn niemand darauf sitzt — genau das ist die Frage bei einer Vakanz.
-- Und sie hängt nicht an der Organisationseinheit, obwohl sie sich meistens
-- daraus ergibt: eine einzelne Stelle kann auf eine fremde Kostenstelle
-- kontiert sein (Projekt, Umlage, geteilte Funktion), ohne dass die Einheit
-- wechselt.
--
-- Als eigene Zuordnungstabelle mit Zeitraum, nicht als Spalte. Eine Umkontier-
-- ung ist ein Ereignis mit Stichtag: die Kosten des Vorjahres müssen dort
-- bleiben, wo sie angefallen sind. Als Spalte würde jede Änderung rückwirkend
-- jede alte Auswertung verändern — und das merkt niemand.
--
-- Halboffene Zeiträume [valid_from, valid_to), wie bei position_assignments
-- und om_positions. In SAP OM ist das die Verknüpfung A011 (Kontierung).
create table if not exists cost_centers (
id uuid primary key default gen_random_uuid(),
code text not null unique,
name text not null,
-- Die verantwortliche Einheit. Nur ein Hinweis, keine Ableitung: die
-- Zuordnung einer Planstelle bleibt die der Planstelle.
org_unit_id uuid references org_units(id) on delete set null,
valid_from date not null default current_date,
valid_to date,
created_at timestamptz not null default now(),
constraint chk_cost_center_range check (valid_to is null or valid_to > valid_from)
);
create table if not exists position_cost_centers (
id uuid primary key default gen_random_uuid(),
position_id uuid not null references om_positions(id) on delete cascade,
cost_center_id uuid not null references cost_centers(id),
valid_from date not null,
valid_to date,
created_at timestamptz not null default now(),
constraint chk_pcc_range check (valid_to is null or valid_to > valid_from)
);
create index if not exists position_cost_centers_position_id_idx on position_cost_centers (position_id);
create index if not exists position_cost_centers_cost_center_id_idx on position_cost_centers (cost_center_id);
-- Eine Planstelle wird zu einem Zeitpunkt auf genau eine Kostenstelle
-- kontiert. Eine Kostenteilung wäre etwas anderes und bräuchte einen Anteil.
create unique index if not exists position_cost_centers_one_current
on position_cost_centers (position_id) where valid_to is null;
-- RLS schaltet der Ereignis-Trigger ensure_rls automatisch ein; ohne Policy
-- käme aus beiden Tabellen nichts zurück.
drop policy if exists cost_centers_hr_all on cost_centers;
create policy cost_centers_hr_all on cost_centers for all using (is_hr_user()) with check (is_hr_user());
drop policy if exists position_cost_centers_hr_all on position_cost_centers;
create policy position_cost_centers_hr_all on position_cost_centers
for all using (is_hr_user()) with check (is_hr_user());
-- ── Bestand: Kostenstellen anlegen und alle Planstellen kontieren ──────
--
-- Eine Kostenstelle je Gesellschaft, Bereich und Abteilung — Teams tragen
-- keine eigene, sie kontieren auf ihre Abteilung. Das ist die übliche Tiefe:
-- ein Team ist eine Führungsspanne, kein Budgettopf.
--
-- Die Nummer wird aus der Organisationsnummer abgeleitet (die ersten fünf
-- Stellen), damit sie nachvollziehbar ist und nicht geraten wirkt.
insert into cost_centers (code, name, org_unit_id, valid_from)
select left(o.org_number, 5), o.name, o.id, o.valid_from
from org_units o
where o.unit_type in ('Gesellschaft', 'Bereich', 'Abteilung')
and not exists (select 1 from cost_centers k where k.org_unit_id = o.id)
on conflict (code) do nothing;
-- Jede Planstelle auf die Kostenstelle der nächsten Einheit über ihr, die
-- eine hat — sie selbst zuerst.
with recursive kette as (
select p.id as position_id, p.org_unit_id as unit_id, p.valid_from, 0 as tiefe
from om_positions p
where not exists (select 1 from position_cost_centers z where z.position_id = p.id)
union all
select k.position_id, o.parent_id, k.valid_from, k.tiefe + 1
from kette k
join org_units o on o.id = k.unit_id
where o.parent_id is not null
and not exists (select 1 from cost_centers c where c.org_unit_id = k.unit_id)
),
treffer as (
select distinct on (k.position_id) k.position_id, c.id as cost_center_id, k.valid_from
from kette k
join cost_centers c on c.org_unit_id = k.unit_id
order by k.position_id, k.tiefe
)
insert into position_cost_centers (position_id, cost_center_id, valid_from)
select position_id, cost_center_id, valid_from from treffer;
-- ── Umkontieren ────────────────────────────────────────────────────────
--
-- Zum Stichtag: die laufende Zuordnung wird beendet, die neue beginnt. Kein
-- UPDATE der alten Zeile — sonst wäre die Vergangenheit nachträglich anders.
create or replace function set_position_cost_center(payload jsonb)
returns void
language plpgsql
security definer
set search_path to 'public', 'pg_temp'
as $function$
declare
v_position_id uuid := (payload->>'position_id')::uuid;
v_cost_center_id uuid := (payload->>'cost_center_id')::uuid;
v_ab date := coalesce(nullif(payload->>'valid_from', '')::date, current_date);
v_position om_positions%rowtype;
v_laufend position_cost_centers%rowtype;
v_neu cost_centers%rowtype;
v_alt text;
v_label text;
begin
perform require_hr_admin();
select * into v_position from om_positions where id = v_position_id;
if not found then
raise exception 'Die Planstelle existiert nicht.';
end if;
select * into v_neu from cost_centers where id = v_cost_center_id;
if not found then
raise exception 'Die Kostenstelle existiert nicht.';
end if;
if v_neu.valid_to is not null and v_neu.valid_to <= v_ab then
raise exception 'Die Kostenstelle % ist zum % nicht mehr gültig.', v_neu.code, v_ab;
end if;
-- Vor dem Bestehen der Planstelle gibt es nichts zu kontieren.
if v_ab < v_position.valid_from then
raise exception 'Die Planstelle besteht erst ab %.', v_position.valid_from;
end if;
select * into v_laufend from position_cost_centers
where position_id = v_position_id and valid_to is null for update;
if found then
if v_laufend.cost_center_id = v_cost_center_id then
raise exception 'Diese Planstelle ist bereits auf % kontiert.', v_neu.code;
end if;
-- Ein gleichtägiger Wechsel würde einen Zeitraum der Länge null erzeugen;
-- chk_pcc_range verbietet das, und zu Recht: er hätte nie gegolten.
if v_ab <= v_laufend.valid_from then
raise exception 'Die laufende Kontierung gilt erst ab %. Der Wechsel muss danach liegen.', v_laufend.valid_from;
end if;
select code into v_alt from cost_centers where id = v_laufend.cost_center_id;
update position_cost_centers set valid_to = v_ab where id = v_laufend.id;
end if;
insert into position_cost_centers (position_id, cost_center_id, valid_from)
values (v_position_id, v_cost_center_id, v_ab);
select j.title || ' (' || o.name || ')' into v_label
from om_positions p join jobs j on j.id = p.job_id join org_units o on o.id = p.org_unit_id
where p.id = v_position_id;
insert into audit_log (actor_user_id, actor_name, action, target_label, details, changes)
values (app_current_user_id(), current_actor_name(), 'Kostenstelle geändert', v_label,
coalesce('Von ' || v_alt || ' ', '') || 'auf ' || v_neu.code || ' — ' || v_neu.name || ', ab ' || v_ab,
jsonb_build_array(jsonb_build_object('feld', 'Kostenstelle', 'vorher', v_alt, 'nachher', v_neu.code)));
end;
$function$;
revoke all on function set_position_cost_center(jsonb) from public;
grant execute on function set_position_cost_center(jsonb) to public;