RTL Complete Database Schema

Fully merged PostgreSQL schema containing the Core Transaction Flow (Appointments, Clinical Forms) and the Administrative Logic (Locations, Catalogs, Coupons, Insurance).

Entity Relationship Overview

Demonstrates how the Administrative catalog tables link to the Core transactional tables.

patients PK id first_name (enc) locations PK id name appointments PK id FK patient_id FK location_id scheduled_at clinical_intakes FK appointment_id services PK id test_type (COVID, etc) order_items FK appointment_id FK service_id test_results FK order_item_id result_value 1:N 1:N 1:1 1:N 1:N 1:N

1. Identity & Patients

users

System authentication for Admin and Lab Staff. Fully hashed passwords.

usersAdmin and Lab Staff Identity
ColumnTypeConstraintsNotes
iduuidPK
namevarchar(255)NOT NULL
emailvarchar(255)UNIQUE NOT NULL
passwordvarchar(255)NOT NULLBcrypt/Argon2
is_activebooleanDEFAULT true

patients

Core PHI table. Uses Laravel 13 `AsEncryptedArray` for AES-256 encryption at rest.

patientsEncrypted Demographics
ColumnTypeConstraintsNotes
iduuidPK
first_nametextNOT NULLEncrypted
last_nametextNOT NULLEncrypted
dobtextNOT NULLEncrypted Date of Birth
emailvarchar(255)NOT NULL
phonevarchar(255)NOT NULL

2. Location & Catalog (Administrative)

locations

locationsClinic Branch Details
ColumnTypeConstraintsNotes
idserialPK
namevarchar(255)NOT NULLe.g. 'St. George Clinic'
addressjsonbNOT NULL{street, city, state, zip}
is_activebooleanDEFAULT trueFrontend visibility

operating_hours

Replaces the legacy `time_slot`, `day_off`, and `special_day` tables.

operating_hoursClinic Availability Rules
ColumnTypeConstraintsNotes
idserialPK
location_idintegerFK locations(id)
day_of_weekintegerNULL0=Sun, 1=Mon. NULL if specific_date used.
specific_datedateNULLOverrides standard weekly schedule
open_timetimeNULL
close_timetimeNULL
is_closedbooleanDEFAULT falseReplaces legacy 'day_off'

services

servicesMaster Test Catalog
ColumnTypeConstraintsNotes
idserialPK
namevarchar(255)NOT NULL
test_typevarchar(100)NOT NULLCOVID, STD, DRUGS, General Health
pricenumeric(10,2)NOT NULL
duration_minsintegerDEFAULT 15Used for calendar slot blocking
is_activebooleanDEFAULT true

medical_references

Replaces `icd_codes` and `specimen_types` tables.

medical_referencesICD Codes & Sample Types
ColumnTypeConstraintsNotes
idserialPK
reference_typevarchar(50)NOT NULL'ICD-10' or 'Specimen'
codevarchar(50)NOT NULLe.g. 'Z11.59' or 'Blood Draw'
descriptiontext

3. Clinical & Booking

appointments

Core logistics hub. Replaces the massive legacy `appoints` table.

appointmentsPer-visit scheduling
ColumnTypeConstraintsNotes
iduuidPK
patient_iduuidFK patients(id)
location_idintegerFK locations(id)Clinic visited
scheduled_attimestamptzNOT NULL
statusvarchar(50)DEFAULT 'booked'booked/arrived/completed
legacy_order_idvarchar(50)Preserved for historical data

order_items

order_itemsTests purchased per appointment
ColumnTypeConstraintsNotes
iduuidPK
appointment_iduuidFK appointments(id)
service_idintegerFK services(id)The specific catalog item
pricenumeric(10,2)NOT NULLSnapshot price

clinical_intakes

clinical_intakesDynamic screening questions
ColumnTypeConstraintsNotes
iduuidPK
appointment_iduuidFK appointments(id)
intake_datajsonbNOT NULLConsolidates legacy STD/COVID questions

test_results

test_resultsLab queue output
ColumnTypeConstraintsNotes
iduuidPK
order_item_iduuidFK order_items(id)
analyte_namevarchar(255)NOT NULL
result_valuevarchar(255)NOT NULLe.g. 'Negative'
is_abnormalbooleanDEFAULT falseTriggers flags in UI

4. Billing & Security

payments

paymentsSquare transaction logs
ColumnTypeConstraintsNotes
iduuidPK
appointment_iduuidFK appointments(id)
square_receipt_idvarchar(255)NOT NULLWebhook correlation
amountnumeric(10,2)NOT NULL

patient_insurances

patient_insurancesInsurance Billing Details
ColumnTypeConstraintsNotes
iduuidPK
appointment_iduuidFK appointments(id)
provider_namevarchar(255)NOT NULL
policy_numbertextNOT NULLEncrypted
group_numbertextEncrypted
card_front_urltextSecure S3 Object Key

coupons

Condenses 4 legacy tables into 1 elegant JSONB rule-set.

couponsDiscount Engine
ColumnTypeConstraintsNotes
idserialPK
codevarchar(50)UNIQUE NOT NULL
discount_typevarchar(20)NOT NULL'percentage' or 'fixed'
discount_valuenumeric(10,2)NOT NULL
usage_rulesjsonb{"allowed_locations": [1]}
is_activebooleanDEFAULT true

audit_logs

audit_logsHIPAA Action Tracking
ColumnTypeConstraintsNotes
idbigserialPK
user_iduuidFK users(id)
actionvarchar(255)NOT NULLe.g. 'Released Result'
ip_addressinet
created_attimestamptzDEFAULT now()

system_settings

system_settingsGlobal Config overrides
ColumnTypeConstraintsNotes
idserialPK
setting_keyvarchar(100)UNIQUE NOT NULLe.g. 'company_details'
setting_valuejsonbNOT NULL

Complete SQL DDL (Ordered for Foreign Keys)

-- 1. Users (Admin & Staff)
CREATE TABLE users (
  id              uuid         PRIMARY KEY DEFAULT gen_random_uuid(),
  name            varchar(255) NOT NULL,
  email           varchar(255) UNIQUE NOT NULL,
  password        varchar(255) NOT NULL,
  is_active       boolean      DEFAULT true
);

-- 2. Patients (Model Encrypted PHI)
CREATE TABLE patients (
  id              uuid         PRIMARY KEY DEFAULT gen_random_uuid(),
  first_name      text         NOT NULL,
  last_name       text         NOT NULL,
  dob             text         NOT NULL,
  email           varchar(255) NOT NULL,
  phone           varchar(255) NOT NULL
);

-- 3. Locations (Clinics)
CREATE TABLE locations (
  id              serial       PRIMARY KEY,
  name            varchar(255) NOT NULL,
  address         jsonb        NOT NULL,
  is_active       boolean      DEFAULT true
);

-- 4. Operating Hours
CREATE TABLE operating_hours (
  id              serial       PRIMARY KEY,
  location_id     integer      REFERENCES locations(id) ON DELETE CASCADE,
  day_of_week     integer,
  specific_date   date,
  open_time       time,
  close_time      time,
  is_closed       boolean      DEFAULT false
);

-- 5. Services (Test Catalog)
CREATE TABLE services (
  id              serial       PRIMARY KEY,
  name            varchar(255) NOT NULL,
  test_type       varchar(100) NOT NULL,
  price           numeric(10,2)NOT NULL,
  duration_mins   integer      DEFAULT 15,
  is_active       boolean      DEFAULT true
);

-- 6. Medical References (ICD/Specimens)
CREATE TABLE medical_references (
  id              serial       PRIMARY KEY,
  reference_type  varchar(50)  NOT NULL,
  code            varchar(50)  NOT NULL,
  description     text
);

-- 7. Appointments (Logistics)
CREATE TABLE appointments (
  id              uuid         PRIMARY KEY DEFAULT gen_random_uuid(),
  patient_id      uuid         REFERENCES patients(id) ON DELETE CASCADE,
  location_id     integer      REFERENCES locations(id),
  scheduled_at    timestamptz  NOT NULL,
  status          varchar(50)  DEFAULT 'booked',
  legacy_order_id varchar(50)
);

-- 8. Clinical Intakes (JSONB Forms)
CREATE TABLE clinical_intakes (
  id              uuid         PRIMARY KEY DEFAULT gen_random_uuid(),
  appointment_id  uuid         REFERENCES appointments(id) ON DELETE CASCADE,
  intake_data     jsonb        NOT NULL
);

-- 9. Order Items (Purchased Tests)
CREATE TABLE order_items (
  id              uuid         PRIMARY KEY DEFAULT gen_random_uuid(),
  appointment_id  uuid         REFERENCES appointments(id) ON DELETE CASCADE,
  service_id      integer      REFERENCES services(id),
  price           numeric(10,2)NOT NULL
);

-- 10. Test Results (Lab Queue)
CREATE TABLE test_results (
  id              uuid         PRIMARY KEY DEFAULT gen_random_uuid(),
  order_item_id   uuid         REFERENCES order_items(id) ON DELETE CASCADE,
  analyte_name    varchar(255) NOT NULL,
  result_value    varchar(255) NOT NULL,
  is_abnormal     boolean      DEFAULT false
);

-- 11. Payments (Square)
CREATE TABLE payments (
  id              uuid         PRIMARY KEY DEFAULT gen_random_uuid(),
  appointment_id  uuid         REFERENCES appointments(id),
  square_receipt_id varchar(255) NOT NULL,
  amount          numeric(10,2)NOT NULL
);

-- 12. Patient Insurances
CREATE TABLE patient_insurances (
  id              uuid         PRIMARY KEY DEFAULT gen_random_uuid(),
  appointment_id  uuid         REFERENCES appointments(id) ON DELETE CASCADE,
  provider_name   varchar(255) NOT NULL,
  policy_number   text         NOT NULL,
  group_number    text,
  card_front_url  text,
  card_back_url   text
);

-- 13. Coupons (Discount Engine)
CREATE TABLE coupons (
  id              serial       PRIMARY KEY,
  code            varchar(50)  UNIQUE NOT NULL,
  discount_type   varchar(20)  NOT NULL,
  discount_value  numeric(10,2)NOT NULL,
  valid_from      timestamptz,
  valid_until     timestamptz,
  usage_rules     jsonb,
  is_active       boolean      DEFAULT true
);

-- 14. Audit Logs (HIPAA)
CREATE TABLE audit_logs (
  id              bigserial    PRIMARY KEY,
  user_id         uuid         REFERENCES users(id),
  action          varchar(255) NOT NULL,
  resource_id     uuid,
  ip_address      inet,
  created_at      timestamptz  DEFAULT now()
);

-- 15. System Settings
CREATE TABLE system_settings (
  id              serial       PRIMARY KEY,
  setting_key     varchar(100) UNIQUE NOT NULL,
  setting_value   jsonb        NOT NULL
);