Overview
Studley AI uses SQL migration files to manage database schema changes. All migrations are stored in the/scripts directory and should be run in sequential order.
Migration Files
Migrations are numbered sequentially:Some migration numbers are duplicated (e.g., 007, 010) due to parallel development. Both files should be run.
Running Migrations
Initial Database Setup
For a fresh database installation, run migrations in order: 1. Enable Required Extensions- Open Supabase Dashboard → SQL Editor
- Copy contents of
001_initialize_database.sql - Click “Run”
Supabase Migration Process
For Supabase projects: Using Supabase CLI:- Navigate to SQL Editor in your Supabase dashboard
- Create a new query
- Paste migration SQL
- Click “Run”
- Verify success in Table Editor
Migration Tracking
Create a migrations tracking table:Key Migrations Explained
001_initialize_database.sql
Purpose: Creates core authentication and user management tables Tables Created:users- User accountsaccounts- OAuth providerssessions- Active sessionsverification_tokens- Email verificationcredit_usage- Credit transactionsgenerations- Generation history
010_create_user_system_tables.sql
Purpose: Creates user profile and library system Tables Created:user_profiles- Extended profile datauser_folders- Content organizationuser_library_items- Saved contentadmin_notifications- Platform announcements
010_create_user_profiles.sql
Purpose: Alternative user profile schema (may be redundant with above) Tables Created:public.profiles- User profile information
This may conflict with
010_create_user_system_tables.sql. Check your database to see which profile table structure you’re using.012_create_admin_user.sql
Purpose: Creates initial admin account Manual Steps Required:- Create user in Supabase Auth dashboard first
- Note the user ID
- Update migration script with correct user ID
- Run migration to grant admin privileges
013_create_ai_workspace_tables.sql
Purpose: AI Workspace feature tables Tables Created:ai_chat_sessions- Chat sessionsai_chat_messages- Individual messagesworkspace_documents- Uploaded documents
019_recreate_results_tables.sql
Purpose: Fixes quiz/flashcard/writing results schema Warning: This drops existing results tables!007_create_feedback_tables.sql
Purpose: User feedback and bug reporting Tables Created:bug_reports- Bug submissionsuser_feedback- General feedback
025_create_shared_materials_table.sql
Purpose: Material sharing functionality Tables Created:shared_materials- Persistent sharing links
- Public access tokens
- Expiration dates
- Resharing controls
Custom Migrations
Creating a New Migration
1. Create the file:Migration Best Practices
Migration Guidelines:
- Always use
IF NOT EXISTSfor CREATE statements - Include rollback scripts for destructive changes
- Test on staging environment first
- Document breaking changes clearly
- Version control all migration files
- Never modify existing migration files after they’ve been deployed
Rollback Strategy
For each migration, create a corresponding rollback:Common Migration Tasks
Adding a Column
Creating an Index
Modifying a Column
Adding Foreign Keys
Troubleshooting
Migration Failed: Table Already Exists
Solution: UseIF NOT EXISTS or check if migration was already applied
Migration Failed: Permission Denied
Solution: Ensure you’re using the correct database role:RLS Policy Conflicts
Solution: Drop existing policies before recreating:Foreign Key Constraint Violation
Solution: Ensure referenced tables exist and have matching data types:Production Deployment
Pre-Deployment Checklist
- Backup database
- Test migration on staging
- Review RLS policies
- Check for breaking changes
- Prepare rollback script
- Schedule maintenance window if needed
- Notify users of downtime (if applicable)
Deployment Steps
- Backup Production Database
- Apply Migration
- Verify Migration
- Monitor Application
Rollback Procedure
If migration fails:Related Documentation
Database Schema
View complete database structure
RLS Policies
Understanding Row Level Security
Admin Dashboard
Return to admin overview
User Management
Manage users and data