Skip to content

Migrations

The CPR backend uses Laravel migrations to manage the database schema. There are 114 migration files that define the complete database structure.

Running Migrations

bash
# Run all pending migrations
php artisan migrate

# Run with seeding
php artisan migrate --seed

# Rollback last batch
php artisan migrate:rollback

# Fresh migration (drops all tables and re-runs)
php artisan migrate:fresh

# Fresh migration with seeding
php artisan migrate:fresh --seed

# Check migration status
php artisan migrate:status

Schema Overview

Core Tables

TablePurpose
usersSystem users (staff, doctors, admins)
branchesClinic branch locations
branch_usersUser-branch assignments (pivot)
rolesSpatie permission roles
permissionsSpatie permission definitions
model_has_rolesUser-role assignments
model_has_permissionsDirect user permissions
role_has_permissionsRole-permission assignments
activity_logAudit trail (Spatie Activity Log)

Patient Management

TablePurpose
patientsPatient demographics, contact info, referral data
patient_visitsVisit records with status, branch, date, impression
patient_visit_plansTreatment plans per visit
patient_visit_prescribed_medicinesPrescriptions per visit
complaintsPatient complaints per visit
medical_certificatesGenerated medical certificates

Clinical Examinations

TablePurpose
visual_acuitiesVisual acuity measurements
refractionsRefraction exam results
tonometriesIntraocular pressure measurements
slit_lampsSlit lamp examination findings
funduscopiesFundus examination results
gonioscopiesAnterior chamber angle assessments
corneal_topographiesCorneal surface mapping
amsler_gridsMacular function tests
eomsExtraocular muscle assessments
exophthalmometriesEye protrusion measurements
lacrimal_irrigationsTear duct assessments
radiologiesRadiology/imaging results
gross_examinationsGross examination findings
indirect_ophthalmoscopiesIndirect ophthalmoscopy records

Billing & Transactions

TablePurpose
transactionsFinancial transactions
transaction_itemsLine items per transaction
bill_itemsMaster list of billable items
bill_item_categoriesBill item categorization
branch_bill_itemsBranch-specific bill items
patient_billing_transactionsPatient billing via queue tickets
payment_methodsAvailable payment methods
insurancesInsurance plans

Pharmacy & Inventory

TablePurpose
pharmacy_itemsPharmaceutical products
pharmacy_item_categoriesDrug categories
pharmacy_item_formsDosage forms (tablet, liquid, etc.)
stock_availablesCurrent stock levels
stock_available_categoriesStock categories
stock_available_unitsMeasurement units
stock_movementsStock in/out tracking
purchase_ordersOrders to suppliers
purchase_order_detailsPO line items
purchase_order_statusesPO status types
deliveriesDelivery records
delivery_detailsDelivery line items
suppliersSupplier information
medicinesMedicine reference data

Queue System

TablePurpose
servicesClinical services offered
branch_servicesServices available per branch
branch_service_countersCounter numbers per service
branch_service_usersStaff assigned to services
queue_ticketsQueue tickets with status tracking

Other

TablePurpose
doctorsDoctor profiles
doctor_rolesDoctor specialization roles
surgery_schedulesPlanned surgeries
surgery_locationsOperating rooms/locations
regions, provinces, municipalities, barangays, zipcodesPhilippine address lookup

Creating New Migrations

bash
# Create a migration
php artisan make:migration create_example_table

# Create migration for an existing table
php artisan make:migration add_column_to_patients_table

Convention

Migration filenames follow the pattern: YYYY_MM_DD_HHMMSS_description.php

Example migration for a new clinical exam:

php
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('example_exams', function (Blueprint $table) {
            $table->id();
            $table->foreignId('patient_visit_id')->constrained()->cascadeOnDelete();
            // exam-specific columns
            $table->text('findings')->nullable();
            $table->string('eye')->nullable(); // OD, OS, OU
            $table->timestamps();
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('example_exams');
    }
};

Key Patterns Used

  • Foreign keys use constrained()->cascadeOnDelete() for referential integrity
  • Soft deletes are used on branches, doctors, medicines, bill_items, procedures, pharmacy_items
  • JSON columns store flexible data (e.g., branches.settings, ophthalmoscopy shots)
  • Enum casting is done at the model level via PHP enums, not in migrations
  • Branch isolation is enforced via branch_id foreign key on most domain tables

CPR - Clinical Patient Records