Files
triqura-ecd/docs/archive/migrations/20251122-supabase-scheme.sql
2025-11-23 10:13:00 +01:00

382 lines
17 KiB
SQL

-- WARNING: This schema is for context only and is not meant to be run.
-- Table order and constraints may not be valid for execution.
CREATE TABLE public.ai_events (
id uuid NOT NULL DEFAULT gen_random_uuid(),
kind text NOT NULL CHECK (kind = ANY (ARRAY['summarize'::text, 'readability'::text, 'extract'::text, 'plan'::text])),
client_id uuid,
note_id uuid,
request jsonb NOT NULL DEFAULT '{}'::jsonb,
response jsonb NOT NULL DEFAULT '{}'::jsonb,
duration_ms integer NOT NULL DEFAULT 0 CHECK (duration_ms >= 0),
created_at timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT ai_events_pkey PRIMARY KEY (id),
CONSTRAINT ai_events_client_id_fkey FOREIGN KEY (client_id) REFERENCES public.clients(id),
CONSTRAINT ai_events_note_id_fkey FOREIGN KEY (note_id) REFERENCES public.intake_notes(id)
);
CREATE TABLE public.anamneses (
id uuid NOT NULL DEFAULT gen_random_uuid(),
intake_id uuid NOT NULL,
anamnese_date date NOT NULL DEFAULT CURRENT_DATE,
anamnese_type text NOT NULL CHECK (anamnese_type = ANY (ARRAY['psychiatrisch'::text, 'sociaal'::text, 'medisch'::text, 'familie'::text, 'ontwikkeling'::text, 'overig'::text])),
content text NOT NULL,
notes text,
created_by uuid,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
CONSTRAINT anamneses_pkey PRIMARY KEY (id),
CONSTRAINT anamneses_intake_id_fkey FOREIGN KEY (intake_id) REFERENCES public.intakes(id),
CONSTRAINT anamneses_created_by_fkey FOREIGN KEY (created_by) REFERENCES public.practitioners(id)
);
CREATE TABLE public.care_plans (
id uuid NOT NULL DEFAULT gen_random_uuid(),
identifier text DEFAULT (gen_random_uuid())::text UNIQUE,
status USER-DEFINED NOT NULL DEFAULT 'draft'::careplan_status,
intent text NOT NULL DEFAULT 'plan'::text,
category_code text DEFAULT 'ggz-behandelplan'::text,
category_display text DEFAULT 'GGZ Behandelplan'::text,
title text NOT NULL,
description text,
patient_id uuid NOT NULL,
encounter_id uuid,
period_start date,
period_end date,
created_date timestamp with time zone DEFAULT now(),
author_id uuid,
contributor_ids ARRAY,
care_team_ids ARRAY,
addresses_condition_ids ARRAY,
goals jsonb DEFAULT '[]'::jsonb,
activities jsonb DEFAULT '[]'::jsonb,
note text,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
based_on_intake_id uuid,
based_on_anamneses ARRAY,
based_on_examinations ARRAY,
based_on_risk_assessments ARRAY,
CONSTRAINT care_plans_pkey PRIMARY KEY (id),
CONSTRAINT care_plans_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id),
CONSTRAINT care_plans_encounter_id_fkey FOREIGN KEY (encounter_id) REFERENCES public.encounters(id),
CONSTRAINT care_plans_author_id_fkey FOREIGN KEY (author_id) REFERENCES public.practitioners(id),
CONSTRAINT care_plans_based_on_intake_id_fkey FOREIGN KEY (based_on_intake_id) REFERENCES public.intakes(id)
);
CREATE TABLE public.clients (
id uuid NOT NULL DEFAULT gen_random_uuid(),
first_name text NOT NULL,
last_name text NOT NULL,
birth_date date NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT now(),
updated_at timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT clients_pkey PRIMARY KEY (id)
);
CREATE TABLE public.conditions (
id uuid NOT NULL DEFAULT gen_random_uuid(),
identifier text DEFAULT (gen_random_uuid())::text UNIQUE,
clinical_status USER-DEFINED NOT NULL DEFAULT 'active'::condition_clinical_status,
verification_status USER-DEFINED NOT NULL DEFAULT 'provisional'::condition_verification_status,
category text NOT NULL DEFAULT 'encounter-diagnosis'::text,
severity_code text,
severity_display text,
code_system text NOT NULL DEFAULT 'http://hl7.org/fhir/sid/icd-10'::text,
code_code text NOT NULL,
code_display text NOT NULL,
body_site_code text,
body_site_display text,
patient_id uuid NOT NULL,
encounter_id uuid,
onset_datetime timestamp with time zone,
onset_age integer,
abatement_datetime timestamp with time zone,
abatement_age integer,
recorded_date timestamp with time zone NOT NULL DEFAULT now(),
recorder_id uuid,
asserter_id uuid,
note text,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
CONSTRAINT conditions_pkey PRIMARY KEY (id),
CONSTRAINT conditions_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id),
CONSTRAINT conditions_encounter_id_fkey FOREIGN KEY (encounter_id) REFERENCES public.encounters(id),
CONSTRAINT conditions_recorder_id_fkey FOREIGN KEY (recorder_id) REFERENCES public.practitioners(id),
CONSTRAINT conditions_asserter_id_fkey FOREIGN KEY (asserter_id) REFERENCES public.practitioners(id)
);
CREATE TABLE public.demo_users (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id uuid UNIQUE,
access_level text NOT NULL DEFAULT 'read_only'::text CHECK (access_level = ANY (ARRAY['read_only'::text, 'interactive'::text, 'presenter'::text])),
expires_at timestamp with time zone DEFAULT (now() + '90 days'::interval),
usage_count integer DEFAULT 0,
last_login_at timestamp with time zone,
created_at timestamp with time zone NOT NULL DEFAULT now(),
updated_at timestamp with time zone NOT NULL DEFAULT now(),
notes text,
CONSTRAINT demo_users_pkey PRIMARY KEY (id),
CONSTRAINT demo_users_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id)
);
CREATE TABLE public.encounters (
id uuid NOT NULL DEFAULT gen_random_uuid(),
identifier text DEFAULT (gen_random_uuid())::text UNIQUE,
status USER-DEFINED NOT NULL DEFAULT 'planned'::encounter_status,
class_code text NOT NULL,
class_display text NOT NULL,
type_code text NOT NULL,
type_display text NOT NULL,
priority_code text,
priority_display text,
patient_id uuid NOT NULL,
practitioner_id uuid,
organization_id uuid,
period_start timestamp with time zone NOT NULL,
period_end timestamp with time zone,
reason_code ARRAY,
reason_display ARRAY,
admission_source text,
discharge_disposition text,
notes text,
intake_note_id uuid,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
intake_id uuid,
CONSTRAINT encounters_pkey PRIMARY KEY (id),
CONSTRAINT encounters_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id),
CONSTRAINT encounters_practitioner_id_fkey FOREIGN KEY (practitioner_id) REFERENCES public.practitioners(id),
CONSTRAINT encounters_organization_id_fkey FOREIGN KEY (organization_id) REFERENCES public.organizations(id),
CONSTRAINT encounters_intake_note_id_fkey FOREIGN KEY (intake_note_id) REFERENCES public.intake_notes(id),
CONSTRAINT encounters_intake_id_fkey FOREIGN KEY (intake_id) REFERENCES public.intakes(id)
);
CREATE TABLE public.examinations (
id uuid NOT NULL DEFAULT gen_random_uuid(),
intake_id uuid NOT NULL,
examination_date date NOT NULL DEFAULT CURRENT_DATE,
examination_type text NOT NULL CHECK (examination_type = ANY (ARRAY['bloedonderzoek'::text, 'neuropsychologisch'::text, 'psychodiagnostiek'::text, 'iq_test'::text, 'persoonlijkheid'::text, 'medisch'::text, 'overig'::text])),
performed_by text,
reason text,
findings text NOT NULL,
document_url text,
notes text,
created_by uuid,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
CONSTRAINT examinations_pkey PRIMARY KEY (id),
CONSTRAINT examinations_intake_id_fkey FOREIGN KEY (intake_id) REFERENCES public.intakes(id),
CONSTRAINT examinations_created_by_fkey FOREIGN KEY (created_by) REFERENCES public.practitioners(id)
);
CREATE TABLE public.intake_notes (
id uuid NOT NULL DEFAULT gen_random_uuid(),
client_id uuid NOT NULL,
title text,
tag text CHECK (tag = ANY (ARRAY['Intake'::text, 'Evaluatie'::text, 'Plan'::text])),
content_json jsonb NOT NULL DEFAULT '{}'::jsonb CHECK (content_json IS NOT NULL),
content_text text,
author uuid,
created_at timestamp with time zone NOT NULL DEFAULT now(),
updated_at timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT intake_notes_pkey PRIMARY KEY (id),
CONSTRAINT intake_notes_client_id_fkey FOREIGN KEY (client_id) REFERENCES public.clients(id)
);
CREATE TABLE public.intakes (
id uuid NOT NULL DEFAULT gen_random_uuid(),
patient_id uuid NOT NULL,
title text NOT NULL,
department text NOT NULL,
psychologist_id uuid,
status text NOT NULL DEFAULT 'bezig'::text CHECK (status = ANY (ARRAY['bezig'::text, 'afgerond'::text])),
start_date date NOT NULL DEFAULT CURRENT_DATE,
end_date date,
notes text,
kindcheck_data jsonb DEFAULT '{}'::jsonb,
treatment_advice jsonb DEFAULT '{}'::jsonb,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
CONSTRAINT intakes_pkey PRIMARY KEY (id),
CONSTRAINT intakes_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id),
CONSTRAINT intakes_psychologist_id_fkey FOREIGN KEY (psychologist_id) REFERENCES public.practitioners(id)
);
CREATE TABLE public.observations (
id uuid NOT NULL DEFAULT gen_random_uuid(),
identifier text DEFAULT (gen_random_uuid())::text UNIQUE,
status USER-DEFINED NOT NULL DEFAULT 'final'::observation_status,
category text NOT NULL,
code_system text NOT NULL,
code_code text NOT NULL,
code_display text NOT NULL,
patient_id uuid NOT NULL,
encounter_id uuid,
effective_datetime timestamp with time zone NOT NULL,
issued timestamp with time zone DEFAULT now(),
performer_id uuid,
value_type text NOT NULL,
value_quantity_value numeric,
value_quantity_unit text,
value_quantity_comparator text,
value_string text,
value_boolean boolean,
value_codeable_concept jsonb,
interpretation_code text,
interpretation_display text,
note text,
body_site text,
method_code text,
method_display text,
reference_range_low numeric,
reference_range_high numeric,
reference_range_text text,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT observations_pkey PRIMARY KEY (id),
CONSTRAINT observations_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id),
CONSTRAINT observations_encounter_id_fkey FOREIGN KEY (encounter_id) REFERENCES public.encounters(id),
CONSTRAINT observations_performer_id_fkey FOREIGN KEY (performer_id) REFERENCES public.practitioners(id)
);
CREATE TABLE public.organizations (
id uuid NOT NULL DEFAULT gen_random_uuid(),
identifier_agb text UNIQUE,
identifier_kvk text,
name text NOT NULL,
alias ARRAY,
type_code text DEFAULT 'prov'::text,
type_display text DEFAULT 'Healthcare Provider'::text,
telecom_phone text,
telecom_email text,
telecom_website text,
address_line ARRAY,
address_city text,
address_postal_code text,
address_country text DEFAULT 'NL'::text,
active boolean DEFAULT true,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
CONSTRAINT organizations_pkey PRIMARY KEY (id)
);
CREATE TABLE public.patients (
id uuid NOT NULL DEFAULT gen_random_uuid(),
identifier_bsn text DEFAULT '999999990'::text,
identifier_client_number text,
name_family text NOT NULL,
name_given ARRAY NOT NULL,
name_prefix text,
name_use text DEFAULT 'official'::text,
birth_date date NOT NULL,
gender USER-DEFINED NOT NULL,
telecom_phone text,
telecom_email text,
address_line ARRAY,
address_city text,
address_postal_code text,
address_country text DEFAULT 'NL'::text,
insurance_company text,
insurance_number text,
emergency_contact_name text,
emergency_contact_relationship text,
emergency_contact_phone text,
active boolean DEFAULT true,
general_practitioner_name text,
general_practitioner_agb text,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
status USER-DEFINED DEFAULT 'planned'::episode_status,
is_john_doe boolean DEFAULT false,
CONSTRAINT patients_pkey PRIMARY KEY (id)
);
CREATE TABLE public.practitioners (
id uuid NOT NULL DEFAULT gen_random_uuid(),
identifier_big text UNIQUE,
identifier_agb text,
name_prefix text,
name_given ARRAY NOT NULL,
name_family text NOT NULL,
name_suffix text,
qualification ARRAY,
telecom_phone text,
telecom_email text,
active boolean DEFAULT true,
user_id uuid,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
CONSTRAINT practitioners_pkey PRIMARY KEY (id),
CONSTRAINT practitioners_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id)
);
CREATE TABLE public.problem_profiles (
id uuid NOT NULL DEFAULT gen_random_uuid(),
client_id uuid NOT NULL,
category text NOT NULL CHECK (category = ANY (ARRAY['stemming_depressie'::text, 'angst'::text, 'gedrag_impuls'::text, 'middelen_gebruik'::text, 'cognitief'::text, 'context_psychosociaal'::text])),
severity text NOT NULL CHECK (severity = ANY (ARRAY['laag'::text, 'middel'::text, 'hoog'::text])),
remarks text,
source_note_id uuid,
created_at timestamp with time zone NOT NULL DEFAULT now(),
updated_at timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT problem_profiles_pkey PRIMARY KEY (id),
CONSTRAINT problem_profiles_client_id_fkey FOREIGN KEY (client_id) REFERENCES public.clients(id),
CONSTRAINT problem_profiles_source_note_id_fkey FOREIGN KEY (source_note_id) REFERENCES public.intake_notes(id)
);
CREATE TABLE public.risk_assessments (
id uuid NOT NULL DEFAULT gen_random_uuid(),
intake_id uuid NOT NULL,
assessment_date date NOT NULL DEFAULT CURRENT_DATE,
risk_type text NOT NULL CHECK (risk_type = ANY (ARRAY['suicidaliteit'::text, 'agressie'::text, 'zelfverwaarlozing'::text, 'middelenmisbruik'::text, 'verward_gedrag'::text, 'overig'::text])),
risk_level text NOT NULL CHECK (risk_level = ANY (ARRAY['laag'::text, 'gemiddeld'::text, 'hoog'::text, 'zeer_hoog'::text])),
rationale text NOT NULL,
measures text,
evaluation_date date,
notes text,
created_by uuid,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
CONSTRAINT risk_assessments_pkey PRIMARY KEY (id),
CONSTRAINT risk_assessments_intake_id_fkey FOREIGN KEY (intake_id) REFERENCES public.intakes(id),
CONSTRAINT risk_assessments_created_by_fkey FOREIGN KEY (created_by) REFERENCES public.practitioners(id)
);
CREATE TABLE public.screening_activities (
id uuid NOT NULL DEFAULT gen_random_uuid(),
screening_id uuid NOT NULL,
activity_text text NOT NULL,
created_by uuid,
created_by_name text,
created_at timestamp with time zone DEFAULT now(),
CONSTRAINT screening_activities_pkey PRIMARY KEY (id),
CONSTRAINT screening_activities_screening_id_fkey FOREIGN KEY (screening_id) REFERENCES public.screenings(id),
CONSTRAINT screening_activities_created_by_fkey FOREIGN KEY (created_by) REFERENCES public.practitioners(id)
);
CREATE TABLE public.screening_documents (
id uuid NOT NULL DEFAULT gen_random_uuid(),
screening_id uuid NOT NULL,
file_name text NOT NULL,
file_type text,
file_size integer,
file_path text,
document_type text CHECK (document_type = ANY (ARRAY['verwijsbrief'::text, 'verhuisbericht'::text, 'indicatie'::text, 'overig'::text])),
uploaded_by uuid,
uploaded_by_name text,
uploaded_at timestamp with time zone DEFAULT now(),
CONSTRAINT screening_documents_pkey PRIMARY KEY (id),
CONSTRAINT screening_documents_screening_id_fkey FOREIGN KEY (screening_id) REFERENCES public.screenings(id),
CONSTRAINT screening_documents_uploaded_by_fkey FOREIGN KEY (uploaded_by) REFERENCES public.practitioners(id)
);
CREATE TABLE public.screenings (
id uuid NOT NULL DEFAULT gen_random_uuid(),
patient_id uuid NOT NULL,
request_for_help text,
decision text CHECK (decision = ANY (ARRAY['geschikt'::text, 'niet_geschikt'::text])),
decision_department text,
decision_notes text,
decision_date timestamp with time zone,
decision_by uuid,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
CONSTRAINT screenings_pkey PRIMARY KEY (id),
CONSTRAINT screenings_patient_id_fkey FOREIGN KEY (patient_id) REFERENCES public.patients(id),
CONSTRAINT screenings_decision_by_fkey FOREIGN KEY (decision_by) REFERENCES public.practitioners(id)
);
CREATE TABLE public.treatment_plans (
id uuid NOT NULL DEFAULT gen_random_uuid(),
client_id uuid NOT NULL,
version integer NOT NULL DEFAULT 1 CHECK (version > 0),
status text NOT NULL DEFAULT 'concept'::text CHECK (status = ANY (ARRAY['concept'::text, 'gepubliceerd'::text])),
plan jsonb NOT NULL DEFAULT '{"doelen": [], "frequentie": "", "interventies": [], "meetmomenten": []}'::jsonb CHECK (plan ? 'doelen'::text AND plan ? 'interventies'::text AND plan ? 'frequentie'::text AND plan ? 'meetmomenten'::text),
created_by uuid,
created_at timestamp with time zone NOT NULL DEFAULT now(),
published_at timestamp with time zone,
updated_at timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT treatment_plans_pkey PRIMARY KEY (id),
CONSTRAINT treatment_plans_client_id_fkey FOREIGN KEY (client_id) REFERENCES public.clients(id)
);