# Database Migration Scripts

Quick migration tools for Mawidi Supabase database.

## Quick Commands

```bash
# Start Supabase + Migrate everything
npm run supabase:start && npm run db:seed

# Just migrate blog posts
npm run db:migrate-blog

# Reset everything and reseed
npm run db:reset
```

---

## Scripts

### `migrate-blog.sh` ⭐ RECOMMENDED
Smart wrapper with automatic Supabase startup and verification.

```bash
npm run db:migrate-blog
```

What it does:
1. Checks if Supabase is running
2. Starts Supabase if needed
3. Counts blog posts in `content/blog/`
4. Runs migration
5. Shows success and verification links

### `migrate-blog-posts.ts`
Direct blog post migration from MDX files.

```bash
npm run db:migrate
```

Features:
- Parses MDX frontmatter
- Calculates reading time
- Clears existing posts
- Batch inserts all posts
- Supports Arabic and English

### `seed-database.ts` 🌱 COMPREHENSIVE
Complete database seeding with storage setup.

```bash
npm run db:seed
```

Features:
- Creates storage buckets
- Migrates blog posts
- Extensible for other data types

---

## Typical Workflow

### Initial Setup
```bash
# 1. Start Supabase
npm run supabase:start

# 2. Seed database
npm run db:seed

# 3. Verify
npm run supabase:studio
```

### Daily Development
```bash
# Add new blog post to content/blog/en/new-post.mdx
# Then migrate:
npm run db:migrate
```

### After Table Deletion
```bash
# Quick restore:
npm run db:migrate-blog
```

---

## Data Sources

Blog posts are read from:
```
content/blog/ar/*.mdx  (8 Arabic posts)
content/blog/en/*.mdx  (10 English posts)
```

Each MDX file requires frontmatter:
```yaml
---
title: "Post Title"
excerpt: "Brief description"
date: "2024-01-15"
author: "Author Name"
tags: ["tag1", "tag2"]
published: true
featured: false
coverImage: "/images/blog/cover.jpg"
---
```

---

## Environment Setup

Required in `.env.local`:
```env
NEXT_PUBLIC_SUPABASE_URL=http://127.0.0.1:54321
NEXT_PUBLIC_SUPABASE_ANON_KEY=sb_publishable_xxx
SUPABASE_SERVICE_ROLE_KEY=sb_secret_xxx
```

Get values:
```bash
supabase status
```

---

## Extending the Seeder

To add more data types to `seed-database.ts`:

```typescript
// Add after migrateBlogPosts()
async function seedCareers() {
  console.log('\n💼 Seeding careers data...');
  // Your seeding logic
}

// In seedDatabase():
await seedCareers();
```

---

## Verification Commands

```bash
# Check blog post count
curl -H "apikey: YOUR_KEY" -H "Prefer: count=exact" \
  "http://127.0.0.1:54321/rest/v1/blog_posts?select=id" -I \
  | grep content-range

# List all posts
curl -H "apikey: YOUR_KEY" \
  "http://127.0.0.1:54321/rest/v1/blog_posts?select=slug,lang,title"

# Check specific post
curl -H "apikey: YOUR_KEY" \
  "http://127.0.0.1:54321/rest/v1/blog_posts?slug=eq.YOUR_SLUG&lang=eq.en"
```

---

## Safety Notes

- ⚠️ Migration **clears existing posts** before inserting
- ✅ Safe to run multiple times (idempotent)
- ✅ Uses service role key (not exposed to client)
- ✅ Runs on server-side only
- ⚠️ Requires Supabase to be running

---

**See also**: `../SUPABASE_MIGRATION_GUIDE.md` for detailed usage guide
