InstructionsForAI
database-migration-guidelines.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
## Supabase Migration Standards ### Naming Convention Migrations must follow the format: `YYYYMMDDHHMMSS_description.sql` ```20260114100000_add_user_preferences.sql20260114100001_create_notifications_table.sql``` ### Migration Structure Always include:1. Step comments explaining what each section does2. IF EXISTS / IF NOT EXISTS guards3. Rollback considerations in comments ```sql-- ============================================-- Migration: Add user preferences-- Purpose: Store user notification settings-- ============================================ -- Step 1: Create the tableCREATE TABLE IF NOT EXISTS user_preferences ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, email_notifications BOOLEAN DEFAULT true, created_at TIMESTAMPTZ DEFAULT NOW()); -- Step 2: Add indexesCREATE INDEX IF NOT EXISTS idx_user_preferences_user_id ON user_preferences(user_id); -- Step 3: Enable RLSALTER TABLE user_preferences ENABLE ROW LEVEL SECURITY; -- Step 4: Create policiesCREATE POLICY "Users can manage own preferences" ON user_preferences FOR ALL USING (auth.uid() = user_id);``` ### Testing Migrations Always test migrations locally before pushing:```bashsupabase db resetsupabase db push```
MarkdownMarkdown