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>
121 lines
6.3 KiB
PL/PgSQL
121 lines
6.3 KiB
PL/PgSQL
-- Org-assignment history, so the Organigramm can be shown as of any date.
|
|
--
|
|
-- Until now `employees` carried only the *current* placement (manager_id,
|
|
-- team_id, division_id, job_title, is_lead, org_level) and every mutation
|
|
-- overwrote it in place. employee_history recorded that something happened,
|
|
-- but only as free text — no old/new values — so a past reporting line was
|
|
-- gone for good. Forward-looking dates already worked (pending_org_changes
|
|
-- holds not-yet-due changes structurally); it was the past that could not be
|
|
-- reconstructed. This migration adds the missing timeline.
|
|
--
|
|
-- Captured by a trigger rather than by editing the mutating RPCs: there are
|
|
-- ~70 `update employees` statements spread over fifteen migrations (several
|
|
-- of which redefine the same function repeatedly), so per-RPC bookkeeping
|
|
-- would miss paths today and again with every 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
|
|
-- today's placement. Changes made *before* this migration are not
|
|
-- recoverable — employee_history never stored structured old values — so a
|
|
-- Stichtag before today's date shows the placement as it stands now for
|
|
-- anyone whose assignment predates this table. Everything from here on 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;
|