# Migration Status Explanation & Fix

## Current Situation

### What We Found:
1. **Missing Migration Files**: The migrations/versions folder only contains:
   - `nuclear_reset_001_nuclear_reset_complete_schema_from_models.py`
   
2. **Previously Existing Migrations** (shown in VS Code tabs but missing from folder):
   - `c5e9c4fdd34a_align_models_with_existing_database_.py`
   - `dc3c13ef2107_add_rating_fields_to_contracts_table.py`

3. **Local Database**: Not running (PostgreSQL on localhost:5432 is down)

## Why Migrations Aren't Working

The "nuclear reset" process that was run previously:
1. Deleted all existing migration files
2. Created a single "nuclear reset" migration
3. This migration was likely never applied to production

## The Production Database Issue

When you deploy to production (Fly.io), the database still expects the old migrations but:
- The old migration files don't exist locally anymore
- The production database's `alembic_version` table likely points to the old migrations
- There's a mismatch between what the database expects and what files exist

## Solutions

### Option 1: Restore Missing Migrations (Recommended)
Since the migration files are still open in VS Code tabs, you can:
1. Save those files back to the migrations/versions folder
2. Ensure they're committed to git
3. Deploy with all migrations intact

### Option 2: Force Production Database Alignment
If the production database schema is already correct:
1. Connect to production database
2. Update the alembic_version table to match current migration
3. Apply any missing columns/tables manually

### Option 3: Complete Fresh Start
1. Back up production data
2. Drop and recreate all tables
3. Apply the nuclear reset migration
4. Restore data

## Immediate Action Required

To check production database status:
```bash
fly postgres connect -a rateright-db
\dt  # List all tables
SELECT * FROM alembic_version;  # Check current migration version
```

To apply missing columns manually (if schema is mostly correct):
```sql
-- Add any missing columns
ALTER TABLE users ADD COLUMN IF NOT EXISTS role VARCHAR(50) DEFAULT 'worker';
ALTER TABLE users ADD COLUMN IF NOT EXISTS is_verified BOOLEAN DEFAULT FALSE;
-- etc.
```

## Root Cause
The nuclear reset process was too aggressive and removed migration history, causing a disconnect between:
- What migrations exist locally
- What the production database expects
- What the actual schema needs to be

## Prevention
Always preserve migration history unless doing a complete fresh deployment with new database.
