Migration structure
Migrations are located in~/workspace/source/supabase/migrations/ and follow a timestamp-based naming convention:
- Has a unique timestamp prefix (YYYYMMDDHHMMSS)
- Contains a descriptive name separated by underscores
- Includes SQL statements to modify the schema
- Documents changes with comments at the top
Apply migrations
There are multiple ways to apply migrations to your Supabase project.Using Supabase CLI
Link your project
Find your project ref in the Supabase dashboard URL:
https://app.supabase.com/project/[project-ref]Using SQL Editor
Database schema overview
DoctorSoft+ uses a comprehensive schema for medical practice management. Here are the main table groups:Core patient tables
- tcPacientes: Patient demographic and contact information
- tpPacienteHistPatologica: Pathological medical history
- tpPacienteHistNoPatol: Non-pathological history (lifestyle, social)
- tpFcHeredoFamiliar: Family hereditary history
- tpDocPaciente: Patient document attachments
Clinical records
- clinical_histories: Patient clinical history text
- clinical_evolution: Clinical evolution notes over time
- medical_records: Visit records with diagnosis and treatment
- somatometry_records: Height, weight, BMI measurements
- tpSignosVitales: Vital signs measurements (blood pressure, temperature, etc.)
- tcSignosVitales: Vital signs catalog (reference ranges by age/sex)
Appointments
- tcCitas: Appointment scheduling and details
- tcCitasEstados: Appointment status catalog (17 different statuses)
- tcCitasEdoTrans: Allowed status transitions
- tcAgendaSettings: Schedule configuration (hours, days, intervals)
- tcAgendaBloqueada: Blocked time periods
- tcConsultorios: Office/room catalog
Prescriptions and medications
- prescriptions: Prescription header information
- prescription_medications: Individual medications per prescription
- medications: Medication catalog with dosage and contraindications
- tcPatologias: Pathology/diagnosis catalog (CIE-10 codes)
System and configuration
- tcCodigosPostales: Mexican postal code catalog
- tcUsuarios: User accounts and permissions
- tcBu: Business units (multi-tenant support)
- tcActividadReciente: Activity tracking for audit logs
Key migrations explained
Appointment status system
The appointment lifecycle is managed through a sophisticated status system:- 0 - Solicitada: Online/phone request (waiting list)
- 1 - Programada: Scheduled in calendar
- 2 - Confirmada: Patient confirmed
- 3 - En Espera: Patient arrived and waiting
- 4 - En Progreso: Consultation in progress
- 5 - Atendida: Consultation completed
- 6-8: No-show and cancellations
- 9-10: Rescheduling
- 11 - Urgencia: Emergency appointment
- 12-14: Billing states
- 15 - Cerrada: Final archived state
- 16: Patient left without service
20251002182941_create_appointment_statuses_table.sql:59-77.
RLS policies for multi-tenancy
All tables implement Row Level Security (RLS) to ensure business unit isolation:Activity tracking
The system tracks all user actions for audit compliance:Soft deletes
Many tables support soft deletion instead of hard deletion:WHERE deleted_at IS NULL.
Create new migrations
When you need to modify the schema:Generate migration file
Use Supabase CLI to create a new migration:This creates a timestamped file in
supabase/migrations/.Test locally
Apply the migration to your local development database:This resets the database and applies all migrations from scratch.
Best practices
Always use transactions
Wrap migration statements in transactions when possible:Include rollback instructions
Document how to rollback the migration:Add helpful comments
Use SQL comments to document tables and columns:Never modify existing migrations
Once applied to production, never edit a migration file. Instead:- Create a new migration to make changes
- Reference the original migration in comments
- Document why the change is needed
Troubleshooting
Migration fails with “relation already exists”
This means the migration was partially applied. Options:-
Check what exists:
-
Add IF NOT EXISTS:
Policy recursion errors
RLS policies that reference the same table can cause recursion. Use security definer functions:20251003185652_fix_get_user_idbu_simple_function.sql for the complete implementation.
Foreign key constraint violations
Apply migrations in order. If you get constraint errors:- Check the migration order (timestamps)
- Ensure referenced tables exist first
- Verify the foreign key columns have matching types
Next steps
Security policies
Learn about RLS policies and authentication
Supabase setup
Configure your Supabase project