# Migration and Setup Summary

## ✅ Current Status

### Tables Created by Migrations

All **6 new dealer claim tables** are included in the migration system:

1. ✅ `dealer_claim_details` - Main claim information
2. ✅ `dealer_proposal_details` - Step 1: Dealer proposal
3. ✅ `dealer_completion_details` - Step 5: Completion documents
4. ✅ `dealer_proposal_cost_items` - Cost breakdown items
5. ✅ `internal_orders` ⭐ - IO details with dedicated fields
6. ✅ `claim_budget_tracking` ⭐ - Comprehensive budget tracking

## Migration Commands

### 1. **`npm run migrate`** ✅
**Status:** ✅ **Fully configured**

This command runs `src/scripts/migrate.ts` which includes **ALL** migrations including:
- ✅ All dealer claim tables (m25-m28)
- ✅ New tables: `internal_orders` (m27) and `claim_budget_tracking` (m28)

**Usage:**
```bash
npm run migrate
```

**What it does:**
- Checks which migrations have already run (via `migrations` table)
- Runs only pending migrations
- Marks them as executed
- Creates all new tables automatically

---

### 2. **`npm run dev`** ✅
**Status:** ✅ **Now fixed and configured**

This command runs:
```bash
npm run setup && nodemon --exec ts-node ...
```

Which calls `npm run setup` → `src/scripts/auto-setup.ts`

**What `auto-setup.ts` does:**
1. ✅ Checks if database exists, creates if missing
2. ✅ Installs PostgreSQL extensions (uuid-ossp)
3. ✅ **Runs all pending migrations** (including dealer claim tables)
4. ✅ Tests database connection

**Fixed:** ✅ Now includes all dealer claim migrations (m29-m35)

**Usage:**
```bash
npm run dev
```

This will automatically:
- Create database if needed
- Run all migrations (including new tables)
- Start the development server

---

### 3. **`npm run setup`** ✅
**Status:** ✅ **Now fixed and configured**

Same as what `npm run dev` calls - runs `auto-setup.ts`

**Usage:**
```bash
npm run setup
```

---

## Migration Files Included

### In `migrate.ts` (for `npm run migrate`):
- ✅ `20251210-add-workflow-type-support` (m22)
- ✅ `20251210-enhance-workflow-templates` (m23)
- ✅ `20251210-add-template-id-foreign-key` (m24)
- ✅ `20251210-create-dealer-claim-tables` (m25) - Creates 3 tables
- ✅ `20251210-create-proposal-cost-items-table` (m26)
- ✅ `20251211-create-internal-orders-table` (m27) ⭐ NEW
- ✅ `20251211-create-claim-budget-tracking-table` (m28) ⭐ NEW

### In `auto-setup.ts` (for `npm run dev` / `npm run setup`):
- ✅ All migrations from `migrate.ts` are now included (m29-m35)

---

## What Gets Created

When you run either `npm run migrate` or `npm run dev`, these tables will be created:

### Dealer Claim Tables (from `20251210-create-dealer-claim-tables.ts`):
1. `dealer_claim_details`
2. `dealer_proposal_details`
3. `dealer_completion_details`

### Additional Tables:
4. `dealer_proposal_cost_items` (from `20251210-create-proposal-cost-items-table.ts`)
5. `internal_orders` ⭐ (from `20251211-create-internal-orders-table.ts`)
6. `claim_budget_tracking` ⭐ (from `20251211-create-claim-budget-tracking-table.ts`)

---

## Verification

After running migrations, verify tables exist:

```sql
-- Check if new tables exist
SELECT table_name 
FROM information_schema.tables 
WHERE table_schema = 'public' 
AND table_name IN (
  'dealer_claim_details',
  'dealer_proposal_details',
  'dealer_completion_details',
  'dealer_proposal_cost_items',
  'internal_orders',
  'claim_budget_tracking'
)
ORDER BY table_name;
```

Should return 6 rows.

---

## Summary

| Command | Runs Migrations? | Includes New Tables? | Status |
|---------|------------------|---------------------|--------|
| `npm run migrate` | ✅ Yes | ✅ Yes | ✅ Working |
| `npm run dev` | ✅ Yes | ✅ Yes | ✅ Fixed |
| `npm run setup` | ✅ Yes | ✅ Yes | ✅ Fixed |

**All commands now create the new tables automatically!** 🎉

---

## Next Steps

1. **Run migrations:**
   ```bash
   npm run migrate
   ```
   OR
   ```bash
   npm run dev  # This will also run migrations via setup
   ```

2. **Verify tables created:**
   Check the database to confirm all 6 tables exist.

3. **Start using:**
   The tables are ready for dealer claim management!

