55 lines
2.7 KiB
SQL
55 lines
2.7 KiB
SQL
-- ====================================================================
|
|
-- 0039_compliance_employee_bindings — Person-zentrierte Compliance-Bindings
|
|
-- ====================================================================
|
|
-- Bisher konnten Compliance-Rollen nur an Stellen (positions) gebunden
|
|
-- werden. Manche Pflichtrollen sind aber persönliche Bestellungen, die
|
|
-- nicht an einer eigenen Stelle hängen (z.B. VEFK, Stv. VEFK,
|
|
-- Brandschutzhelfer:in, einzelne Schaltberechtigte).
|
|
--
|
|
-- Diese Migration erweitert compliance_role_bindings um eine optionale
|
|
-- employee_id. Pro Binding wird entweder eine Position oder ein
|
|
-- Mitarbeiter referenziert — nie beides, nie keines.
|
|
-- --------------------------------------------------------------------
|
|
|
|
-- position_id wird optional
|
|
alter table public.compliance_role_bindings
|
|
alter column position_id drop not null;
|
|
|
|
-- Neue Spalte für Personen-Bindings
|
|
alter table public.compliance_role_bindings
|
|
add column if not exists employee_id uuid
|
|
references public.employees(id) on delete cascade;
|
|
|
|
-- XOR-Constraint: genau einer von position_id / employee_id muss gesetzt sein
|
|
alter table public.compliance_role_bindings
|
|
drop constraint if exists compliance_role_bindings_subject_check;
|
|
alter table public.compliance_role_bindings
|
|
add constraint compliance_role_bindings_subject_check
|
|
check (
|
|
(position_id is not null and employee_id is null)
|
|
or (position_id is null and employee_id is not null)
|
|
);
|
|
|
|
-- Unique-Constraint erweitern: jetzt auch für employee-Bindings eindeutig
|
|
-- (Drop+Recreate, weil die alte Definition nur position_id berücksichtigt
|
|
-- hat — null-Werte würden mehrfach erlaubt sein.)
|
|
alter table public.compliance_role_bindings
|
|
drop constraint if exists compliance_role_bindings_unique;
|
|
|
|
create unique index if not exists compliance_role_bindings_unique_position
|
|
on public.compliance_role_bindings (framework_id, role_id, position_id)
|
|
where position_id is not null;
|
|
|
|
create unique index if not exists compliance_role_bindings_unique_employee
|
|
on public.compliance_role_bindings (framework_id, role_id, employee_id)
|
|
where employee_id is not null;
|
|
|
|
-- Lookup-Index auf employee_id (analog zu position_id)
|
|
create index if not exists compliance_role_bindings_employee_idx
|
|
on public.compliance_role_bindings (employee_id);
|
|
|
|
comment on column public.compliance_role_bindings.position_id is
|
|
'Wenn gesetzt: Rolle hängt an einer Stelle — Inhaber der Stelle ist Compliance-Träger. Sich gegenseitig ausschließend mit employee_id.';
|
|
comment on column public.compliance_role_bindings.employee_id is
|
|
'Wenn gesetzt: Rolle ist eine persönliche Bestellung an einen Mitarbeiter (z.B. VEFK, Brandschutzhelfer:in). Sich gegenseitig ausschließend mit position_id.';
|