Create Supabase Migration
Create a new database migration for the Supabase project.
Steps
- •
Create the migration file:
bashpnpm supabase migration new {{name}} - •
Edit the generated SQL file in
supabase/migrations/with the required schema changes - •
Validate locally by resetting the database:
bashpnpm supabase db reset
- •
Regenerate TypeScript types after the migration is applied:
bashpnpm types:generate
Migration Guidelines
- •Use snake_case for table and column names
- •Always include
created_atandupdated_attimestamps for new tables - •Add appropriate indexes for frequently queried columns
- •Include RLS (Row Level Security) policies for new tables
- •Reference existing patterns in
supabase/migrations/for consistency
Example Migration
sql
-- Create user_preferences table CREATE TABLE user_preferences ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE, theme TEXT DEFAULT 'system', notifications_enabled BOOLEAN DEFAULT true, created_at TIMESTAMPTZ DEFAULT now(), updated_at TIMESTAMPTZ DEFAULT now() ); -- Enable RLS ALTER TABLE user_preferences ENABLE ROW LEVEL SECURITY; -- Users can only access their own preferences CREATE POLICY "Users can manage their own preferences" ON user_preferences FOR ALL USING (auth.uid() = user_id); -- Index for fast user lookups CREATE INDEX idx_user_preferences_user_id ON user_preferences(user_id);