|
| 1 | +# π Database Migration Guide - Supabase CLI |
| 2 | + |
| 3 | +## Why Migrations? |
| 4 | + |
| 5 | +β
**Version Control** - Track database changes over time |
| 6 | +β
**Reproducible** - Same schema across dev/staging/prod |
| 7 | +β
**Rollback** - Easy to undo changes |
| 8 | +β
**Team Collaboration** - Share schema changes via Git |
| 9 | +β
**CI/CD Ready** - Automated deployments |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## π¦ Setup (One-Time) |
| 14 | + |
| 15 | +### Install Supabase CLI |
| 16 | + |
| 17 | +Already installed via: |
| 18 | + |
| 19 | +```bash |
| 20 | +pnpm add -D supabase |
| 21 | +``` |
| 22 | + |
| 23 | +### Project Structure |
| 24 | + |
| 25 | +``` |
| 26 | +supabase/ |
| 27 | +βββ config.toml # Supabase config |
| 28 | +βββ migrations/ |
| 29 | +β βββ 20251013000001_initial_schema.sql # Initial tables |
| 30 | +β βββ 20251013000002_add_admins.sql # Admin table |
| 31 | +βββ .gitignore |
| 32 | +``` |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +## π Running Migrations |
| 37 | + |
| 38 | +### Option 1: Via Supabase Dashboard (Recommended for Now) |
| 39 | + |
| 40 | +**For Remote Database (cycacvbiknngaoxrgzci):** |
| 41 | + |
| 42 | +1. Go to SQL Editor: |
| 43 | + |
| 44 | + ``` |
| 45 | + https://supabase.com/dashboard/project/cycacvbiknngaoxrgzci/sql/new |
| 46 | + ``` |
| 47 | + |
| 48 | +2. Run migrations in order: |
| 49 | + |
| 50 | + **Migration 1 - Initial Schema:** |
| 51 | + |
| 52 | + ```bash |
| 53 | + # Copy content from: |
| 54 | + supabase/migrations/20251013000001_initial_schema.sql |
| 55 | + |
| 56 | + # Paste in SQL Editor β Run βΆοΈ |
| 57 | + ``` |
| 58 | + |
| 59 | + **Migration 2 - Add Admins:** |
| 60 | + |
| 61 | + ```bash |
| 62 | + # Copy content from: |
| 63 | + supabase/migrations/20251013000002_add_admins.sql |
| 64 | + |
| 65 | + # Paste in SQL Editor β Run βΆοΈ |
| 66 | + ``` |
| 67 | + |
| 68 | +3. Verify in Table Editor: |
| 69 | + - β
students |
| 70 | + - β
chat_messages |
| 71 | + - β
student_insights |
| 72 | + - β
achievements |
| 73 | + - β
study_sessions |
| 74 | + - β
admins (with 1 row) |
| 75 | + |
| 76 | +### Option 2: Via Supabase CLI (Advanced) |
| 77 | + |
| 78 | +**Note**: Requires Supabase CLI installed system-wide and authentication |
| 79 | + |
| 80 | +```bash |
| 81 | +# Login to Supabase |
| 82 | +npx supabase login |
| 83 | + |
| 84 | +# Link to remote project |
| 85 | +npx supabase link --project-ref cycacvbiknngaoxrgzci |
| 86 | + |
| 87 | +# Push migrations to remote |
| 88 | +npx supabase db push |
| 89 | + |
| 90 | +# Or run specific migration |
| 91 | +npx supabase db execute --file supabase/migrations/20251013000001_initial_schema.sql |
| 92 | +npx supabase db execute --file supabase/migrations/20251013000002_add_admins.sql |
| 93 | +``` |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## π Creating New Migrations |
| 98 | + |
| 99 | +### Naming Convention: |
| 100 | + |
| 101 | +``` |
| 102 | +YYYYMMDDHHMMSS_description.sql |
| 103 | +
|
| 104 | +Examples: |
| 105 | +20251013000001_initial_schema.sql |
| 106 | +20251013000002_add_admins.sql |
| 107 | +20251014120000_add_parent_table.sql |
| 108 | +``` |
| 109 | + |
| 110 | +### Template: |
| 111 | + |
| 112 | +```sql |
| 113 | +-- Migration: [Feature Name] |
| 114 | +-- Created: YYYY-MM-DD |
| 115 | +-- Description: What this migration does |
| 116 | + |
| 117 | +-- Your SQL here |
| 118 | +CREATE TABLE IF NOT EXISTS new_table ( |
| 119 | + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 120 | + ... |
| 121 | +); |
| 122 | + |
| 123 | +-- Indexes |
| 124 | +CREATE INDEX IF NOT EXISTS idx_name ON new_table(column); |
| 125 | + |
| 126 | +-- RLS |
| 127 | +ALTER TABLE new_table ENABLE ROW LEVEL SECURITY; |
| 128 | +CREATE POLICY "policy_name" ON new_table FOR SELECT USING (true); |
| 129 | +``` |
| 130 | + |
| 131 | +### Example - Add Parent Table: |
| 132 | + |
| 133 | +Create: `supabase/migrations/20251014120000_add_parents.sql` |
| 134 | + |
| 135 | +```sql |
| 136 | +-- Migration: Add Parents Table |
| 137 | +-- Created: 2025-10-14 |
| 138 | +-- Description: Parent accounts for monitoring student progress |
| 139 | + |
| 140 | +CREATE TABLE IF NOT EXISTS parents ( |
| 141 | + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), |
| 142 | + email VARCHAR(255) UNIQUE NOT NULL, |
| 143 | + name VARCHAR(255) NOT NULL, |
| 144 | + phone VARCHAR(20), |
| 145 | + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), |
| 146 | + updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() |
| 147 | +); |
| 148 | + |
| 149 | +CREATE TABLE IF NOT EXISTS student_parents ( |
| 150 | + student_id UUID REFERENCES students(id) ON DELETE CASCADE, |
| 151 | + parent_id UUID REFERENCES parents(id) ON DELETE CASCADE, |
| 152 | + relationship VARCHAR(20) CHECK (relationship IN ('father', 'mother', 'guardian')), |
| 153 | + PRIMARY KEY (student_id, parent_id) |
| 154 | +); |
| 155 | + |
| 156 | +CREATE INDEX idx_student_parents_student ON student_parents(student_id); |
| 157 | +CREATE INDEX idx_student_parents_parent ON student_parents(parent_id); |
| 158 | + |
| 159 | +ALTER TABLE parents ENABLE ROW LEVEL SECURITY; |
| 160 | +ALTER TABLE student_parents ENABLE ROW LEVEL SECURITY; |
| 161 | + |
| 162 | +CREATE POLICY "Public read parents" ON parents FOR SELECT USING (true); |
| 163 | +CREATE POLICY "Public read student_parents" ON student_parents FOR SELECT USING (true); |
| 164 | + |
| 165 | +CREATE TRIGGER update_parents_updated_at BEFORE UPDATE ON parents |
| 166 | + FOR EACH ROW EXECUTE FUNCTION update_updated_at_column(); |
| 167 | +``` |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## π Migration Workflow |
| 172 | + |
| 173 | +### Development Process: |
| 174 | + |
| 175 | +1. **Create Migration File**: |
| 176 | + |
| 177 | + ```bash |
| 178 | + # Create new file with timestamp |
| 179 | + supabase/migrations/20251015100000_your_feature.sql |
| 180 | + ``` |
| 181 | + |
| 182 | +2. **Write SQL**: |
| 183 | + - Add tables, columns, indexes |
| 184 | + - Update RLS policies |
| 185 | + - Insert seed data (if needed) |
| 186 | + |
| 187 | +3. **Test Locally** (optional): |
| 188 | + |
| 189 | + ```bash |
| 190 | + npx supabase db reset |
| 191 | + ``` |
| 192 | + |
| 193 | +4. **Run on Remote**: |
| 194 | + - Copy SQL to Supabase Dashboard |
| 195 | + - Or use `npx supabase db push` |
| 196 | + |
| 197 | +5. **Commit to Git**: |
| 198 | + ```bash |
| 199 | + git add supabase/migrations/ |
| 200 | + git commit -m "migration: Add feature X" |
| 201 | + git push |
| 202 | + ``` |
| 203 | + |
| 204 | +--- |
| 205 | + |
| 206 | +## π Current Migrations |
| 207 | + |
| 208 | +### Migration 1: Initial Schema |
| 209 | + |
| 210 | +- **File**: `20251013000001_initial_schema.sql` |
| 211 | +- **Creates**: |
| 212 | + - students |
| 213 | + - chat_messages |
| 214 | + - student_insights |
| 215 | + - achievements |
| 216 | + - study_sessions |
| 217 | +- **Status**: β οΈ Needs to be run |
| 218 | + |
| 219 | +### Migration 2: Add Admins |
| 220 | + |
| 221 | +- **File**: `20251013000002_add_admins.sql` |
| 222 | +- **Creates**: |
| 223 | + - admins table |
| 224 | + - Default admin account |
| 225 | +- **Status**: β οΈ Needs to be run (fixes 404 error) |
| 226 | + |
| 227 | +--- |
| 228 | + |
| 229 | +## π― Quick Migration Commands |
| 230 | + |
| 231 | +### Run All Pending Migrations: |
| 232 | + |
| 233 | +```bash |
| 234 | +# Via Dashboard (current method) |
| 235 | +1. Open SQL Editor |
| 236 | +2. Run migration 1 |
| 237 | +3. Run migration 2 |
| 238 | +4. Verify tables created |
| 239 | + |
| 240 | +# Via CLI (alternative) |
| 241 | +npx supabase db push |
| 242 | +``` |
| 243 | + |
| 244 | +### Check Migration Status: |
| 245 | + |
| 246 | +```bash |
| 247 | +npx supabase migration list |
| 248 | +``` |
| 249 | + |
| 250 | +### Create New Migration: |
| 251 | + |
| 252 | +```bash |
| 253 | +# Generate migration file |
| 254 | +npx supabase migration new feature_name |
| 255 | + |
| 256 | +# Or manually create: |
| 257 | +# supabase/migrations/YYYYMMDDHHMMSS_feature_name.sql |
| 258 | +``` |
| 259 | + |
| 260 | +### Reset Database (Dangerous!): |
| 261 | + |
| 262 | +```bash |
| 263 | +# Local only |
| 264 | +npx supabase db reset |
| 265 | + |
| 266 | +# Never run on production! |
| 267 | +``` |
| 268 | + |
| 269 | +--- |
| 270 | + |
| 271 | +## π Best Practices |
| 272 | + |
| 273 | +### DO: |
| 274 | + |
| 275 | +β
Use IF NOT EXISTS for idempotency |
| 276 | +β
Add indexes for foreign keys |
| 277 | +β
Enable RLS on all tables |
| 278 | +β
Include descriptive comments |
| 279 | +β
Test migrations on local first |
| 280 | +β
Keep migrations small & focused |
| 281 | +β
Use ON CONFLICT for seed data |
| 282 | + |
| 283 | +### DON'T: |
| 284 | + |
| 285 | +β Modify existing migrations |
| 286 | +β Delete data without backup |
| 287 | +β Skip RLS policies |
| 288 | +β Hardcode sensitive data |
| 289 | +β Run untested SQL on production |
| 290 | + |
| 291 | +--- |
| 292 | + |
| 293 | +## π οΈ For This Project (Static Site) |
| 294 | + |
| 295 | +Since we're using **GitHub Pages** (static site), we use **manual migrations**: |
| 296 | + |
| 297 | +### Current Approach: |
| 298 | + |
| 299 | +1. Create migration files (version controlled) |
| 300 | +2. Run SQL manually in Supabase Dashboard |
| 301 | +3. Team shares migrations via Git |
| 302 | +4. Simple, no CLI dependency |
| 303 | + |
| 304 | +### Why Not Full CLI? |
| 305 | + |
| 306 | +- Static site deployment |
| 307 | +- No server-side migrations |
| 308 | +- Manual control preferred |
| 309 | +- Simpler for small team |
| 310 | + |
| 311 | +### When to Use CLI? |
| 312 | + |
| 313 | +- Local development (optional) |
| 314 | +- Multiple environments |
| 315 | +- CI/CD pipelines |
| 316 | +- Larger teams |
| 317 | + |
| 318 | +--- |
| 319 | + |
| 320 | +## π Migration Checklist |
| 321 | + |
| 322 | +### Before Running: |
| 323 | + |
| 324 | +- [ ] Review SQL for errors |
| 325 | +- [ ] Check dependencies (foreign keys) |
| 326 | +- [ ] Verify table names |
| 327 | +- [ ] Test on local (optional) |
| 328 | +- [ ] Backup if modifying data |
| 329 | + |
| 330 | +### After Running: |
| 331 | + |
| 332 | +- [ ] Verify tables created |
| 333 | +- [ ] Check indexes exist |
| 334 | +- [ ] Test RLS policies |
| 335 | +- [ ] Verify sample data |
| 336 | +- [ ] Update documentation |
| 337 | + |
| 338 | +--- |
| 339 | + |
| 340 | +## π― Next Steps |
| 341 | + |
| 342 | +### Immediate: |
| 343 | + |
| 344 | +1. β
Run migration 1 (initial schema) |
| 345 | +2. β
Run migration 2 (add admins) |
| 346 | +3. β
Verify tables in Supabase |
| 347 | +4. β
Test admin login |
| 348 | +5. β
Test student features |
| 349 | + |
| 350 | +### Future Migrations: |
| 351 | + |
| 352 | +- [ ] Add parent accounts |
| 353 | +- [ ] Add teacher portal |
| 354 | +- [ ] Add study groups |
| 355 | +- [ ] Add notifications |
| 356 | +- [ ] Add content library |
| 357 | + |
| 358 | +--- |
| 359 | + |
| 360 | +## π Learn More |
| 361 | + |
| 362 | +- [Supabase Migrations](https://supabase.com/docs/guides/cli/local-development#database-migrations) |
| 363 | +- [Database Migrations Best Practices](https://www.prisma.io/dataguide/types/relational/migration-best-practices) |
| 364 | +- [SQL Migration Patterns](https://www.postgresql.org/docs/current/ddl.html) |
| 365 | + |
| 366 | +--- |
| 367 | + |
| 368 | +**Current Status**: |
| 369 | + |
| 370 | +- Migrations created β
|
| 371 | +- Need to run on remote β οΈ |
| 372 | +- Files committed to Git β
|
| 373 | + |
| 374 | +**To fix 404 error**: Run migration 2 in Supabase Dashboard! π |
0 commit comments