File size: 5,161 Bytes
b6e19c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# Deployment β€” Supabase CLI & Dashboard Instructions

## Purpose
Read this file when deploying any change to Supabase or your
environment. Always try CLI first. Use dashboard only if CLI
is not possible β€” dashboard steps will be clearly marked.

---

## Core Principle
**CLI first. Dashboard only when CLI cannot do it.**
Every deployment step must be logged and reversible.

---

## Prerequisites
```bash
# Install Supabase CLI
npm install -g supabase

# Login
supabase login

# Link to your project (run once per project)
supabase link --project-ref YOUR_PROJECT_REF

# Confirm link
supabase status
```

---

## Environment Variables
```bash
# .env.local (never commit this file)
NEXT_PUBLIC_SUPABASE_URL=your_project_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_anon_key
SUPABASE_SERVICE_ROLE_KEY=your_service_role_key
```

**Where to find these:**
Dashboard β†’ Project Settings β†’ API β†’ Project URL + Keys

---

## Database Migrations

### Create a new migration
```bash
supabase migration new migration_name
# Creates: supabase/migrations/[timestamp]_migration_name.sql
# Write your SQL inside this file, then push
```

### Push migration to remote
```bash
supabase db push
```

### Check migration status
```bash
supabase migration list
```

### Reset local DB (dev only)
```bash
supabase db reset
```

### Pull remote schema to local
```bash
supabase db pull
```

---

## Local Development

### Start local Supabase
```bash
supabase start
# Gives you local URL, anon key, service role key
```

### Stop local Supabase
```bash
supabase stop
```

### View local DB in browser
```bash
supabase studio
# Opens Supabase Studio at localhost:54323
```

---

## Edge Functions

### Create a new edge function
```bash
supabase functions new function-name
```

### Serve locally
```bash
supabase functions serve function-name --env-file .env.local
```

### Deploy edge function
```bash
supabase functions deploy function-name
```

### Set environment secret for edge function
```bash
supabase secrets set KEY=value
```

### List secrets
```bash
supabase secrets list
```

---

## Storage

### CLI β€” not fully supported for bucket creation
**β†’ Use Dashboard for bucket creation**

#### Dashboard Steps β€” Create Storage Bucket

Go to Supabase Dashboard β†’ Storage
Click "New bucket"
Enter bucket name
Toggle public/private
Click "Create bucket"

### Upload file via CLI
```bash
supabase storage cp ./local-file.png ss:///bucket-name/path/file.png
```

---

## Row Level Security (RLS)

### CLI β€” enable RLS on a table
```sql
-- Inside a migration file
ALTER TABLE your_table ENABLE ROW LEVEL SECURITY;
```

### CLI β€” create a policy via migration
```sql
CREATE POLICY "Users can view own data"
ON your_table
FOR SELECT
USING (auth.uid() = user_id);
```

### Dashboard Steps β€” RLS Policy (if migration not possible)

Go to Supabase Dashboard β†’ Authentication β†’ Policies
Select your table
Click "New Policy"
Choose template or write custom
Click "Review" then "Save Policy"

---

## Auth Configuration

### CLI β€” not supported for OAuth provider setup
**β†’ Use Dashboard for OAuth setup**

#### Dashboard Steps β€” Enable OAuth Provider

Go to Dashboard β†’ Authentication β†’ Providers
Select provider (Google, GitHub, etc.)
Enter Client ID and Secret
Copy callback URL β†’ paste into provider's OAuth app
Click "Save"

### Set auth email templates

Dashboard β†’ Authentication β†’ Email Templates
β†’ Edit confirm signup / reset password templates

---

## scripts/deploy.sh
```bash
#!/bin/bash
echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] Starting deployment..." >> logs/errors.log

# Run tests first β€” abort if they fail
bash scripts/test.sh
if [ $? -ne 0 ]; then
  echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] Deployment aborted β€” tests failed" >> logs/errors.log
  exit 1
fi

# Push DB migrations
echo "Pushing DB migrations..."
supabase db push
if [ $? -ne 0 ]; then
  echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] Migration failed β€” check supabase logs" >> logs/errors.log
  exit 1
fi

echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] Deployment complete" >> logs/errors.log
```

---

## Rollback

### Rollback last migration
```bash
# Supabase does not have auto-rollback
# Write a reverse migration manually

supabase migration new rollback_migration_name
# Write reverse SQL (DROP TABLE, ALTER, etc.)
supabase db push
```

### Dashboard Steps β€” Rollback (if CLI fails)

Go to Dashboard β†’ Database β†’ Migrations
Identify the migration to reverse
Go to Dashboard β†’ SQL Editor
Write and run reverse SQL manually

---

## Deployment Checklist
- [ ] All tests pass (`bash scripts/test.sh`)
- [ ] `.env.local` is not committed
- [ ] Migration file created for every schema change
- [ ] RLS enabled on every new table
- [ ] Edge functions tested locally before deploy
- [ ] `supabase db push` confirms no errors
- [ ] One-liner git commit message written

---

## What NOT to Do
- Do not edit schema directly in Dashboard without a migration file
- Do not push migrations without running tests first
- Do not commit `.env.local` or any file with keys
- Do not disable RLS on any table in production
- Do not deploy edge functions without local testing first