Overview
AniDojo uses Supabase for its database, and migrations are managed through SQL files. This guide covers how to apply migrations to your production database and manage schema changes.Database migrations must be run before deploying your application to production. The application will not function correctly without the proper database schema.
Understanding Migrations
Migrations are SQL scripts that create and modify your database schema. AniDojo’s migrations are located in:- profiles - User profile data extending Supabase Auth
- anime_entries - User’s anime tracking entries
- reviews - User-written anime reviews
- review_votes - Helpful/unhelpful votes on reviews
- comments - Comments on reviews
- comment_votes - Upvotes/downvotes on comments
- custom_lists - User-created anime lists
- custom_list_entries - Anime entries in custom lists
- Row Level Security (RLS) policies for data protection
- Indexes for query performance
- Triggers for automatic profile creation and timestamp updates
Prerequisites
Before running migrations, ensure you have:- A Supabase project created at supabase.com
- Your project’s connection details (URL and keys)
- Either the Supabase Dashboard access or Supabase CLI installed
Method 1: Using Supabase Dashboard (Recommended for Beginners)
This method is quick and doesn’t require any CLI tools.Access SQL Editor
- Go to your Supabase Dashboard
- Select your project
- Navigate to SQL Editor in the left sidebar
Open Migration File
Open the migration file from your project:This file contains 375 lines of SQL that creates your entire database schema.
Copy SQL Content
Copy the entire contents of
001_initial_schema.sqlThe file includes:- Table definitions
- Indexes for performance
- RLS policies for security
- Triggers for automation
- Helper functions
Run Migration
- Paste the SQL into the SQL Editor
- Click Run (or press Ctrl/Cmd + Enter)
- Wait for execution to complete (typically 2-5 seconds)
- Verify you see “Success. No rows returned” message
Verify Tables Created
Verify the migration succeeded:
- Navigate to Table Editor in the left sidebar
- You should see all 8 tables:
- profiles
- anime_entries
- reviews
- review_votes
- comments
- comment_votes
- custom_lists
- custom_list_entries
If you don’t see all tables, check the SQL Editor for error messages and ensure the entire script was pasted.
Method 2: Using Supabase CLI (Recommended for Production)
The CLI method is preferred for production environments and team workflows.Get Project Reference ID
- Go to your Supabase Dashboard
- Select your project
- Go to Settings → General
- Copy your Reference ID (looks like:
abcdefghijklmnop)
Link Project
Link your local project to your Supabase project:You’ll be prompted to enter your database password. This is the password you set when creating your Supabase project.
Push Migrations
Apply all migrations to your remote database:This command:
- Reads all files in
supabase/migrations/ - Applies them to your remote database in order
- Tracks which migrations have been applied
- Shows success/error messages
Post-Migration Setup
After running migrations, set up additional Supabase features:Storage Buckets
Create storage buckets for file uploads:Navigate to Storage
- Go to your Supabase Dashboard
- Click Storage in the left sidebar
- Click Create a new bucket
Create Avatars Bucket
Create a bucket for user avatars:
- Name:
avatars - Public: ✅ Yes
- File size limit: 5 MB
- Allowed MIME types:
image/jpeg,image/png,image/webp,image/gif
Create Review Images Bucket (Optional)
Create a bucket for review attachments:
- Name:
review-images - Public: ✅ Yes
- File size limit: 10 MB
- Allowed MIME types:
image/jpeg,image/png,image/webp
Storage Policies
Set up Row Level Security policies for storage buckets:Avatar Storage Policies
Avatar Storage Policies
Run these SQL commands in the SQL Editor:
Review Images Storage Policies
Review Images Storage Policies
Anime Images Storage Policies
Anime Images Storage Policies
Verifying Your Setup
After running migrations and setting up storage, verify everything works:Test Authentication
- Deploy your application (or run locally)
- Navigate to the signup page
- Create a test account
- Verify the user appears in Supabase Dashboard → Authentication → Users
- Check that a profile was automatically created in Table Editor → profiles
Test Database Operations
- Sign in to your test account
- Add an anime to your list
- Verify it appears in Table Editor → anime_entries
- Try updating and deleting the entry
Managing Future Migrations
As your application evolves, you’ll need to add more migrations:Creating New Migrations
Create Migration File
Create a new migration file with a sequential number:
Use descriptive names that explain what the migration does. Include a number prefix to maintain order.
Best Practices
Always use IF NOT EXISTS/IF EXISTS
Always use IF NOT EXISTS/IF EXISTS
Make migrations idempotent so they can be run multiple times safely:
Include rollback instructions
Include rollback instructions
Document how to undo migrations:
Test migrations on a staging database first
Test migrations on a staging database first
- Create a separate Supabase project for staging
- Apply migrations there first
- Test thoroughly
- Only then apply to production
Never modify existing migrations
Never modify existing migrations
Once a migration has been applied to production:
- Never edit the migration file
- Create a new migration to make changes
- This maintains a clear history of schema changes
Troubleshooting
”relation already exists” Error
Cause: Migration has already been run or table was created manually Solution:- Add
IF NOT EXISTSto CREATE statements - Or drop existing tables before running (⚠️ data loss!)
- Or skip to the next migration
”permission denied” Error
Cause: Insufficient database permissions Solution:- Ensure you’re using the correct database password
- Verify your Supabase project is active (not paused)
- Check that you have owner/admin access to the project
RLS Policy Errors
Cause: Conflict with existing policies or incorrect syntax Solution:- Check for duplicate policy names
- Drop existing policies first:
- Then recreate them
Storage Bucket Issues
“Bucket already exists” error:- Check Storage dashboard for existing buckets
- Use a different bucket name
- Or delete the existing bucket (⚠️ deletes all files!)
- Verify storage policies are set up correctly
- Check file size limits
- Ensure MIME types are allowed
- Verify user is authenticated
Advanced: Local Development with Supabase
For advanced workflows, you can run Supabase locally:Next Steps
Deploy to Vercel
Deploy your application with the configured database
Environment Variables
Configure connection to your database