# Production Database Migration Fix Guide

## Problem
The migration `20250927191124_update_schedule_codes` failed because it tries to add required columns without defaults to tables that already contain data.

## Error Message
```
The `20250927191124_update_schedule_codes` migration started at 2026-03-09 10:43:49.907548 UTC failed
```

## Solution Steps

### Step 1: Check Current Database State

SSH into your production server and check which columns exist:

```bash
cd /home/theinitiative/public_html/lenseekapi.zbeetech.com

# Check if the Setting table has storeCommission column
psql -d your_database_name -c "\d \"Setting\""

# Check if the Session table has refreshToken column
psql -d your_database_name -c "\d \"Session\""

# Check if the Earning table has commissionRate column
psql -d your_database_name -c "\d \"Earning\""
```

### Step 2: Choose the Right Fix

#### Option A: If columns DON'T exist in database (Recommended)

```bash
# Mark the migration as rolled back
npx prisma migrate resolve --rolled-back 20250927191124_update_schedule_codes

# Use db push to apply schema changes safely (handles existing data)
npx prisma db push --accept-data-loss

# Regenerate Prisma client
npx prisma generate

# Restart your API
pm2 restart lenseek-api

# Verify it's working
pm2 logs lenseek-api --lines 50
```

#### Option B: If columns already exist in database

```bash
# Mark the migration as successfully applied
npx prisma migrate resolve --applied 20250927191124_update_schedule_codes

# Deploy any remaining migrations
npx prisma migrate deploy

# Regenerate Prisma client
npx prisma generate

# Restart your API
pm2 restart lenseek-api

# Verify it's working
pm2 logs lenseek-api --lines 50
```

### Step 3: Verify the Fix

After applying the fix, check that:

1. The API starts without errors:
```bash
pm2 logs lenseek-api --lines 20
```

2. The settings endpoint works:
```bash
curl http://localhost:3001/settings
```

3. No more Prisma errors appear

## Why This Happened

The migration failed because it tried to add these columns as required (NOT NULL) without defaults:
- `Earning.commissionRate` - required without default
- `Session.refreshToken` - required without default  
- `Session.updatedAt` - required without default

When tables already have data, Prisma can't add required columns without defaults.

## Prevention for Future

For production databases with existing data, always:
1. Add new columns as optional first (nullable)
2. Backfill data
3. Then make them required in a second migration

Or use `prisma db push` which handles this automatically.

## Quick Recovery Commands

**Copy and paste this entire block into your SSH session:**

```bash
cd /home/theinitiative/public_html/lenseekapi.zbeetech.com && \
npx prisma migrate resolve --rolled-back 20250927191124_update_schedule_codes && \
npx prisma db push --accept-data-loss && \
npx prisma generate && \
pm2 restart lenseek-api && \
echo "✅ Done! Checking logs..." && \
sleep 2 && \
pm2 logs lenseek-api --lines 20
```

This will:
1. Mark the failed migration as rolled back
2. Sync your database schema safely
3. Regenerate the Prisma client
4. Restart your API
5. Show you the logs to verify it worked
