Overview
The Transport Logistics platform uses Supabase for database management, which provides built-in support for PostgreSQL migrations. Migrations are SQL files that define incremental changes to your database schema.All migration files are stored in the
supabase/migrations/ directory and are automatically applied in chronological order based on their timestamp prefixes.Migration Files
The project currently includes these migrations:Row Level Security
File:
20250626084408-*.sqlSets up RLS policies for all tables with role-based access controlUnique Constraints
File:
20250710103823-*.sqlAdds unique constraint to vehicle numbersMigration File Structure
Naming Convention
Migration files follow this pattern:20250626084408-40640dda-5b51-48b2-b2ac-0a64dc5022c0.sql
- Timestamp:
YYYYMMDDHHMMSSformat ensures chronological ordering - UUID: Unique identifier prevents naming conflicts
- Extension:
.sqlfor SQL migration files
Directory Structure
Existing Migrations
Migration 1: Row Level Security (RLS)
File:20250626084408-40640dda-5b51-48b2-b2ac-0a64dc5022c0.sql
This migration establishes comprehensive security policies:
- Overview
- Security Function
- Policy Examples
Enables Row Level Security on all public tables:
vehiclesshipmentspackagesmaterialsprofilesuser_settingsroutestransporters
Migration 2: Vehicle Number Uniqueness
File:20250710103823-11aa3e41-d45b-4c07-877d-ef8adc04984c.sql
Enforces unique vehicle numbers across the system:
This constraint prevents duplicate vehicle registrations and ensures data integrity for vehicle tracking.
Creating New Migrations
Identify Database Changes
Determine what schema changes you need:
- New tables or columns
- Modified constraints or indexes
- Updated RLS policies
- New database functions
Generate Migration File
Create a new migration file with timestamp and UUID:Or use Supabase CLI (recommended):This generates:
supabase/migrations/YYYYMMDDHHMMSS_add_driver_table.sqlTest Migration Locally
Apply the migration to your local Supabase instance:Verify the changes in your local database:
Applying Migrations
Local Development
If you’re using Supabase CLI with local development:Remote Supabase Project
For hosted Supabase projects:Link Your Project
https://app.supabase.com/project/[your-project-ref]Manual Application
If not using Supabase CLI, run migrations manually:Migration Best Practices
Always Use Transactions
Wrap migrations in transactions for atomicity:
Make Idempotent
Use
IF NOT EXISTS and IF EXISTS:Add Comments
Document complex logic:
Test Rollbacks
Create corresponding down migrations:
Security Checklist
Before applying migrations:Database Functions
The project includes custom PostgreSQL functions insupabase/functions/:
get_user_email.sql
Retrieves user email addresses (likely for admin features)delete_user.sql & delete_user_rpc.sql
Provides safe user deletion with cascade handlingThese are database functions (PL/pgSQL), not Edge Functions (TypeScript). They run in PostgreSQL and can be called from your application.
Troubleshooting
Migration fails with constraint error
Migration fails with constraint error
Problem: Existing data violates new constraintSolution:
- Add data cleanup before the constraint:
- Or make the migration less strict initially
RLS policies are too restrictive
RLS policies are too restrictive
Problem: Users can’t access data they should be able toSolution:
- Test policies in SQL editor:
- Temporarily disable RLS for debugging (development only):
- Review policy logic and adjust as needed
Migration applied in wrong order
Migration applied in wrong order
Problem: Timestamps don’t reflect dependency orderSolution:
- Rename migration files to correct the order
- Or create a new migration that fixes the issue
- Never modify already-applied migrations in production
Can't connect to run migrations
Can't connect to run migrations
Problem: Supabase CLI can’t reach databaseSolution:
- Check your internet connection
- Verify project ref:
supabase projects list - Re-link project:
supabase link --project-ref your-ref - Check database pooler settings in Supabase dashboard
Schema Management Tips
Version Control
- ✅ DO commit all migration files
- ✅ DO include descriptive migration names
- ❌ DON’T modify migrations after they’re applied
- ❌ DON’T delete old migrations
Production Safety
- Use staging environment to test migrations first
- Schedule migrations during low-traffic periods
- Have rollback plan ready
- Monitor application errors after deployment
- Keep database backups before major changes
Performance Considerations
- Add indexes for frequently queried columns
- Use partial indexes for filtered queries
- Consider concurrent index creation for large tables:
- Analyze query plans before and after schema changes
Next Steps
Local Setup
Complete development environment setup
Environment Variables
Configure Supabase connection
Supabase Docs
Official Supabase migration guide
PostgreSQL Docs
PostgreSQL documentation