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.
1. Identity & Patients
users
System authentication for Admin and Lab Staff. Fully hashed passwords.
usersAdmin and Lab Staff Identity
| Column | Type | Constraints | Notes |
|---|---|---|---|
| id | uuid | PK | |
| name | varchar(255) | NOT NULL | |
| varchar(255) | UNIQUE NOT NULL | ||
| password | varchar(255) | NOT NULL | Bcrypt/Argon2 |
| is_active | boolean | DEFAULT true |
patients
Core PHI table. Uses Laravel 13 `AsEncryptedArray` for AES-256 encryption at rest.
patientsEncrypted Demographics
| Column | Type | Constraints | Notes |
|---|---|---|---|
| id | uuid | PK | |
| first_name | text | NOT NULL | Encrypted |
| last_name | text | NOT NULL | Encrypted |
| dob | text | NOT NULL | Encrypted Date of Birth |
| varchar(255) | NOT NULL | ||
| phone | varchar(255) | NOT NULL |
2. Location & Catalog (Administrative)
locations
locationsClinic Branch Details
| Column | Type | Constraints | Notes |
|---|---|---|---|
| id | serial | PK | |
| name | varchar(255) | NOT NULL | e.g. 'St. George Clinic' |
| address | jsonb | NOT NULL | {street, city, state, zip} |
| is_active | boolean | DEFAULT true | Frontend visibility |
operating_hours
Replaces the legacy `time_slot`, `day_off`, and `special_day` tables.
operating_hoursClinic Availability Rules
| Column | Type | Constraints | Notes |
|---|---|---|---|
| id | serial | PK | |
| location_id | integer | FK locations(id) | |
| day_of_week | integer | NULL | 0=Sun, 1=Mon. NULL if specific_date used. |
| specific_date | date | NULL | Overrides standard weekly schedule |
| open_time | time | NULL | |
| close_time | time | NULL | |
| is_closed | boolean | DEFAULT false | Replaces legacy 'day_off' |
services
servicesMaster Test Catalog
| Column | Type | Constraints | Notes |
|---|---|---|---|
| id | serial | PK | |
| name | varchar(255) | NOT NULL | |
| test_type | varchar(100) | NOT NULL | COVID, STD, DRUGS, General Health |
| price | numeric(10,2) | NOT NULL | |
| duration_mins | integer | DEFAULT 15 | Used for calendar slot blocking |
| is_active | boolean | DEFAULT true |
medical_references
Replaces `icd_codes` and `specimen_types` tables.
medical_referencesICD Codes & Sample Types
| Column | Type | Constraints | Notes |
|---|---|---|---|
| id | serial | PK | |
| reference_type | varchar(50) | NOT NULL | 'ICD-10' or 'Specimen' |
| code | varchar(50) | NOT NULL | e.g. 'Z11.59' or 'Blood Draw' |
| description | text |
3. Clinical & Booking
appointments
Core logistics hub. Replaces the massive legacy `appoints` table.
appointmentsPer-visit scheduling
| Column | Type | Constraints | Notes |
|---|---|---|---|
| id | uuid | PK | |
| patient_id | uuid | FK patients(id) | |
| location_id | integer | FK locations(id) | Clinic visited |
| scheduled_at | timestamptz | NOT NULL | |
| status | varchar(50) | DEFAULT 'booked' | booked/arrived/completed |
| legacy_order_id | varchar(50) | Preserved for historical data |
order_items
order_itemsTests purchased per appointment
| Column | Type | Constraints | Notes |
|---|---|---|---|
| id | uuid | PK | |
| appointment_id | uuid | FK appointments(id) | |
| service_id | integer | FK services(id) | The specific catalog item |
| price | numeric(10,2) | NOT NULL | Snapshot price |
clinical_intakes
clinical_intakesDynamic screening questions
| Column | Type | Constraints | Notes |
|---|---|---|---|
| id | uuid | PK | |
| appointment_id | uuid | FK appointments(id) | |
| intake_data | jsonb | NOT NULL | Consolidates legacy STD/COVID questions |
test_results
test_resultsLab queue output
| Column | Type | Constraints | Notes |
|---|---|---|---|
| id | uuid | PK | |
| order_item_id | uuid | FK order_items(id) | |
| analyte_name | varchar(255) | NOT NULL | |
| result_value | varchar(255) | NOT NULL | e.g. 'Negative' |
| is_abnormal | boolean | DEFAULT false | Triggers flags in UI |
4. Billing & Security
payments
paymentsSquare transaction logs
| Column | Type | Constraints | Notes |
|---|---|---|---|
| id | uuid | PK | |
| appointment_id | uuid | FK appointments(id) | |
| square_receipt_id | varchar(255) | NOT NULL | Webhook correlation |
| amount | numeric(10,2) | NOT NULL |
patient_insurances
patient_insurancesInsurance Billing Details
| Column | Type | Constraints | Notes |
|---|---|---|---|
| id | uuid | PK | |
| appointment_id | uuid | FK appointments(id) | |
| provider_name | varchar(255) | NOT NULL | |
| policy_number | text | NOT NULL | Encrypted |
| group_number | text | Encrypted | |
| card_front_url | text | Secure S3 Object Key |
coupons
Condenses 4 legacy tables into 1 elegant JSONB rule-set.
couponsDiscount Engine
| Column | Type | Constraints | Notes |
|---|---|---|---|
| id | serial | PK | |
| code | varchar(50) | UNIQUE NOT NULL | |
| discount_type | varchar(20) | NOT NULL | 'percentage' or 'fixed' |
| discount_value | numeric(10,2) | NOT NULL | |
| usage_rules | jsonb | {"allowed_locations": [1]} | |
| is_active | boolean | DEFAULT true |
audit_logs
audit_logsHIPAA Action Tracking
| Column | Type | Constraints | Notes |
|---|---|---|---|
| id | bigserial | PK | |
| user_id | uuid | FK users(id) | |
| action | varchar(255) | NOT NULL | e.g. 'Released Result' |
| ip_address | inet | ||
| created_at | timestamptz | DEFAULT now() |
system_settings
system_settingsGlobal Config overrides
| Column | Type | Constraints | Notes |
|---|---|---|---|
| id | serial | PK | |
| setting_key | varchar(100) | UNIQUE NOT NULL | e.g. 'company_details' |
| setting_value | jsonb | NOT 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 );