Overview
Cajas uses SQL migrations to set up and modify the database schema. All migrations are located in thesupabase/migrations/ directory and are executed sequentially by filename.
Migration Files
The application includes the following migrations:| Migration File | Purpose | Tables Created |
|---|---|---|
20240101000000_init.sql | Initial schema setup | users, cases, items, case_items, user_items, transactions, provably_fair_games |
20240101000001_seed.sql | Seed data for development | Inserts sample items and cases |
0000_create_cases_system.sql | Enhanced case system | Adds admin_logs, RLS policies |
20251205120000_fix_case_items_schema.sql | Schema correction | Rebuilds case_items table |
20251205121000_remove_seed_cases.sql | Cleanup | Removes seed cases |
20251209000000_create_provably_fair.sql | Provably fair system | user_seeds, game_rolls |
Running Migrations
Method 1: Using Supabase Dashboard (Recommended)
The easiest way to run migrations is through the Supabase Dashboard:
- Go to Supabase Dashboard
- Select your project
- Navigate to SQL Editor
- Create a new query
- Copy and paste the contents of each migration file in order
- Click Run to execute
Run migrations in alphabetical order by filename. The timestamp prefix ensures correct ordering.
Migration Details
Initial Setup (20240101000000_init.sql)
Creates the core database schema for the application.
Key Features:
- Enables UUID extension for unique identifiers
- Creates public user profiles linked to
auth.users - Sets up case and item management tables
- Configures Row Level Security (RLS) on all tables
- Implements automatic user profile creation trigger
supabase/migrations/20240101000000_init.sql:130-140):
public.users whenever a new user signs up.
Case Items Schema Fix (20251205120000_fix_case_items_schema.sql)
Rebuilds the case_items table to match application requirements.
Changes (supabase/migrations/20251205120000_fix_case_items_schema.sql:1-14):
Provably Fair System (20251209000000_create_provably_fair.sql)
Implements cryptographic fairness for game outcomes.
Tables Created:
-
user_seeds- Stores per-user seeds for provably fair gaming -
game_rolls- Audit log for all game outcomes
supabase/migrations/20251209000000_create_provably_fair.sql:2-38.
Understanding Row Level Security
All tables use RLS to enforce access control at the database level.Public Read Policies
Anyone can view cases and items:User-Specific Policies
Users can only access their own data:supabase/migrations/20251209000000_create_provably_fair.sql:15-21.
Admin-Only Policies
Only users withrole = 'admin' can manage cases:
supabase/migrations/0000_create_cases_system.sql:42-50.
Verifying Migrations
Check Tables Exist
Run this query in the SQL Editor to verify all tables:You should see:
admin_logscase_itemscasesgame_rollsitemstransactionsuser_itemsuser_seedsusers
Verify RLS is Enabled
Check that RLS is enabled on all tables:All tables should show
rowsecurity = true.Test Policies
View all policies for a table:Verify that expected policies exist (public read, admin write).
Creating New Migrations
When you need to modify the schema:Create Migration File
Use a timestamp prefix for ordering:Example:
20260304120000_add_user_preferences.sqlRollback and Recovery
Rollback a migration
Rollback a migration
Supabase doesn’t have automatic rollback. Create a new migration to undo changes:
Restore from backup
Restore from backup
Supabase automatically creates daily backups on paid plans:
- Go to Database > Backups
- Select a backup point
- Click Restore
Export current schema
Export current schema
Save your current schema for reference:Store this in version control or secure storage.
Troubleshooting
Migration already applied
Migration already applied
Symptoms: Error about existing tables or policiesSolution: Migrations are idempotent when using If missing, manually drop and recreate, or skip the migration.
IF NOT EXISTS:Foreign key constraint violation
Foreign key constraint violation
Symptoms:
violates foreign key constraintSolution: Check migration order. Tables must be created before they’re referenced:- Create parent tables first (
users,cases,items) - Then create child tables (
case_items,user_items) - Ensure referenced IDs exist before inserting data
RLS policy conflict
RLS policy conflict
Symptoms:
policy already exists or access denied errorsSolution: Drop existing policies before recreating:Permission denied
Permission denied
Symptoms:
permission denied when running migrationsSolution: Ensure you’re using the database password or service role key:- Dashboard: Automatically uses correct credentials
- CLI: Run
supabase linkwith correct project ref - Direct: Use
postgresuser with database password
Best Practices
Use Timestamps
Always prefix migration files with timestamps:
Idempotent Migrations
Use
IF NOT EXISTS and IF EXISTS to make migrations safe to run multiple times.Test First
Always test migrations on a development database before production.
Document Changes
Add comments explaining complex migrations:
Small Changes
Create focused migrations that do one thing well. Don’t combine unrelated changes.
Backup First
Export your database before running migrations in production.
Next Steps
Authentication Setup
Configure user authentication and authorization
Database Queries
Learn how to query the database in your application
