Skip to main content

Database Migrations Guide

Migrations are version-controlled changes to your database schema. This guide walks through creating, testing, and deploying migrations in the FindU ecosystem.

Migration Workflow Overview

Creating Your First Migration

1

Create a feature branch

2

Create migration file

3

Write your migration

Edit the generated file with your SQL:
4

Test your migration

  1. Go to Supabase dashboard
  2. Navigate to SQL Editor
  3. Paste your migration SQL
  4. Run in a transaction:
5

Commit and push

6

Create pull request

Types of Migrations

Schema Migrations

Data Migrations

Data migrations should be idempotent - running them multiple times should have the same result.

Creating Functions

Testing Migrations

Local Testing Checklist

Writing Rollback Migrations

For risky changes, prepare a rollback migration:

Deployment Process

To Development

  1. Automatic: Merging PR to dev branch triggers deployment
  2. Manual: If needed, run supabase db push after switching to dev

To Production

  1. Create PR: From dev to main branch
  2. Review Required: Another developer must approve
  3. Automatic Deploy: Merging triggers production deployment
  4. Monitor: Check Supabase dashboard for any issues

Best Practices

Always Include RLS

Every table should have Row Level Security enabled and appropriate policies

Make Migrations Idempotent

Use IF NOT EXISTS, IF EXISTS, and proper checks

One Concern Per Migration

Keep migrations focused on a single feature or change

Test Thoroughly

Always run supabase db reset before pushing

Common Patterns

Adding Audit Fields

Setting Up RLS

Performance Indexes

Troubleshooting

Your migration isn’t idempotent. Add IF NOT EXISTS checks:
Test policies in Supabase dashboard:
  1. Go to Authentication > Policies
  2. Use the policy editor to test with different users
  3. Check policy conditions carefully
Common causes:
  • Missing IF EXISTS checks
  • Dependency on data that doesn’t exist
  • Different Postgres versions
  • Timezone issues with timestamps
Never modify a migration that’s been deployed. Instead:
  1. Create a new migration to fix the issue
  2. Include proper rollback handling
  3. Test the upgrade path thoroughly

Next, learn about schema management or data operations.