# /db — Prisma Database Management

Prisma ORM database management for the mawidi-site project. Uses Prisma 6.x via project-local npx.

## Usage

```
/db push                   # Push schema to DB (development)
/db generate               # Generate Prisma client
/db migrate                # Create and run migration (production)
/db migrate status         # Check migration status
/db studio                 # Open Prisma Studio GUI
/db status                 # Check database connection and migration status
/db seed                   # Run seed script
/db reset                  # WARNING: Reset database (drops all data)
/db diff                   # Show pending schema changes
```

**Examples:**
- `/db push`
- `/db status`
- `/db migrate create add-invoices-table`

## Workflow

### Push Schema (Development)
```bash
cd /Users/Asim/Desktop/mawidi_codex/mawidi-site && npx prisma db push
```
- Use in development to sync schema without creating migration files
- Shows which changes will be applied

### Generate Client
```bash
cd /Users/Asim/Desktop/mawidi_codex/mawidi-site && npx prisma generate
```
- Regenerates the Prisma client after schema changes
- Required after modifying `prisma/schema.prisma`

### Create Migration
```bash
cd /Users/Asim/Desktop/mawidi_codex/mawidi-site && npx prisma migrate dev --name <name>
```
- Creates a migration file and applies it
- Name should be descriptive: `add-invoices-table`, `add-org-stripe-keys`

### Check Migration Status
```bash
cd /Users/Asim/Desktop/mawidi_codex/mawidi-site && npx prisma migrate status
```

### Open Prisma Studio
```bash
cd /Users/Asim/Desktop/mawidi_codex/mawidi-site && npx prisma studio
```
- Opens web GUI at http://localhost:5555

### Database Status
```bash
cd /Users/Asim/Desktop/mawidi_codex/mawidi-site
npx prisma migrate status
npx prisma db pull --print | head -20
```
Also check the health endpoint:
```bash
curl -s http://localhost:9000/api/health/database | jq .
```

### Reset Database (DANGEROUS)
**ALWAYS warn the user:**
```
WARNING: This will DROP all tables and recreate the database!
All data will be lost. Are you sure? (y/N)
```
Only if confirmed:
```bash
cd /Users/Asim/Desktop/mawidi_codex/mawidi-site && npx prisma migrate reset
```

### Schema Diff
```bash
cd /Users/Asim/Desktop/mawidi_codex/mawidi-site && npx prisma migrate diff --from-schema-datamodel prisma/schema.prisma --to-schema-datasource prisma/schema.prisma
```

## Key Rules
- ALWAYS use `npx prisma` — never global prisma (Prisma 6.x)
- ALWAYS warn before destructive operations (reset, push with data loss)
- Run `npx prisma generate` after every schema change
- Schema file is at `prisma/schema.prisma`
- Generated client is at `lib/generated/prisma/`
- After schema changes: generate -> push/migrate -> restart dev server
