# Migration Summary - October 5, 2025

## What This Migration Does

### ✅ Preserves Your Critical Data (28 tables total)

**Core Business Data:**
- **Users** - All user accounts
- **Clients** - All client information
- **Cases** - All case data
- **Time Entries** - All billable hours
- **Expenses** - All case expenses
- **Invoices** - All invoicing data
- **Payments** - All payment records
- **Case Instructions** - All case notes

**User Authentication & Permissions (20 tables):**
- **Authentication** - Emails, phones, passwords, tokens, faces
- **MFA** - TOTP, WebAuthn, recovery codes, passkeys, 2FA settings
- **Permissions** - Module, table, column level permissions
- **Organization** - Group memberships, org/branch assignments
- **Settings** - User preferences and board configurations

### 🔄 Recreates These Tables (Fresh Start)
- **Documents** - New folder-based structure (no data to migrate)
- **Document Metadata** - New structure with parent_document_id support
- **Modules** - New Case Management module with Back button

## Quick Migration Steps

```bash
# 1. Backup (CRITICAL!)
pg_dump -U postgres -d taf_db > backup_$(date +%Y%m%d_%H%M%S).sql

# 2. Run migration (it will automatically create missing tables first)
cd /home/kevin_admin/projects/TAF
psql -U postgres -d taf_db -f migrations/20251005_live_db_migration.sql

# 3. Populate modules
psql -U postgres -d taf_db -f sql/modules.sql

# 4. Clear browser cache
# Press Ctrl+Shift+R (or Cmd+Shift+R on Mac)
```

**Note:** The migration script automatically runs `TAFDB.pgsql` first to ensure all tables exist (including user_* tables that may not exist in older versions). This makes it safe to run on any version of the database.

## Estimated Time
- **Migration**: 2-5 minutes
- **Module population**: 1-2 minutes
- **Total downtime**: ~10 minutes

## What You Get

### 📁 New Document Management
- **Folder support** - Create folders within case documents
- **Drag & drop** - Easy file organization
- **Breadcrumb navigation** - Navigate folder hierarchy
- **Preview in modal** - View docs without leaving the page

### 🔙 Back Button
- Located in Case Management header (after Record Payment)
- Clears selection and returns to Cases tab
- Smooth navigation workflow

### 🎨 Fixed Modal Display
- Full-screen backdrop (grey overlay)
- Proper modal sizing for tabs/tables
- No more narrow modal windows

### ⚡ Better Performance
- Caching for documents (no duplicate API calls)
- Optimized indexes
- Faster query performance

## Safety Features

✅ **Backup tables created** - Can restore instantly
✅ **Transaction-based** - Auto-rollback on error
✅ **No data loss** - Only drops empty tables
✅ **Full file backup** - Complete database dump

## Rollback (If Needed)

If something goes wrong:

```bash
# Stop application
sudo systemctl stop apache2

# Restore from file backup
dropdb -U postgres taf_db
createdb -U postgres taf_db
psql -U postgres -d taf_db < backup_*.sql

# Restart application
sudo systemctl start apache2
```

## Testing Checklist

After migration, test these:

- [ ] Login as admin
- [ ] Login as regular user
- [ ] Open Case Management
- [ ] Select a case
- [ ] Switch to Documents tab
- [ ] Upload a document
- [ ] Create a folder
- [ ] Click Back button
- [ ] Add time entry
- [ ] Create invoice

## Support

If you encounter issues:

1. Check the browser console (F12)
2. Check PostgreSQL logs: `tail -f /var/log/postgresql/postgresql-*.log`
3. Review migration log in the database: `SELECT * FROM migration_log;`

## Post-Migration Cleanup

**After 7 days** of confirmed success:

```sql
DROP TABLE _backup_users, _backup_clients, _backup_cases,
           _backup_time_entries, _backup_expenses, _backup_invoices,
           _backup_payments, _backup_case_instructions CASCADE;
```

---

**Ready to migrate?** Follow the detailed steps in `MIGRATION_PROCEDURE.md`
