# migration-production

## When to Use

Run database migrations for ProductionRemote environment, apply pending EF Core migrations, create backups (MANDATORY), and verify connectivity.

⚠️ **WARNING**: Production environment - exercise extreme caution.

## Prerequisites

- .NET SDK installed (`dotnet --version`)
- PostgreSQL accessible for ProductionRemote
- Connection string in `appsettings.ProductionRemote.json`
- `scripts/safe-migrate.sh` available and executable
- ⚠️ Production access permissions required

## Execution Steps

### Step 1: Verify Script Exists and is Executable

Check: `test -f scripts/safe-migrate.sh`

**If missing:** Error and **STOP**

**If not executable:** `chmod +x scripts/safe-migrate.sh`

### Step 2: Verify Environment Configuration

Check: `test -f src/Managing.Api/appsettings.ProductionRemote.json`

**If missing:** Check `appsettings.Production.json`, else **STOP**

### Step 3: Production Safety Check

⚠️ **CRITICAL**: Verify authorization, reviewed migrations, rollback plan, backup will be created.

**Ask user:** "⚠️ You are about to run migrations on ProductionRemote. Are you sure? (yes/no)"

**If confirmed:** Continue

**If not confirmed:** **STOP**

### Step 4: Run Migration Script

Run: `./scripts/safe-migrate.sh ProductionRemote`

**Script performs:** Build → Check connectivity → Create DB if needed → Prompt backup (always choose 'y') → Check pending changes → Generate script → Show for review → Wait confirmation → Apply → Verify

**On success:** Show success, backup location, log location, remind to verify application functionality

**On failure:** Show error output, diagnose (connectivity, connection string, server, permissions, data conflicts), provide guidance or **STOP** if unresolvable (suggest testing in non-prod first)

## Error Handling

**Script not found:** Check `ls -la scripts/safe-migrate.sh`, **STOP** if missing

**Not executable:** `chmod +x scripts/safe-migrate.sh`, retry

**Database connection fails:** Verify PostgreSQL running, check connection string in `appsettings.ProductionRemote.json`, verify network/firewall/credentials, ⚠️ **WARN** production connectivity issues require immediate attention

**Build fails:** Show errors (C# compilation, missing dependencies, config errors), try auto-fix (compilation errors, imports, config), if fixed re-run else **STOP** with ⚠️ **WARN** never deploy broken code

**Migration conflicts:** Review migration history, script handles idempotent migrations, schema conflicts may need manual intervention, ⚠️ **WARN** may require downtime

**Backup fails:** **CRITICAL** - script warns, strongly recommend fixing before proceeding, **WARN** extreme risks if proceeding without backup

**Migration partially applies:** ⚠️ **CRITICAL** dangerous state - check `__EFMigrationsHistory`, may need rollback, **STOP** until database state verified

## Example Execution

**Success flow:**
1. Verify script → ✅
2. Check executable → ✅
3. Verify config → ✅
4. Safety check → User confirms
5. Run: `./scripts/safe-migrate.sh ProductionRemote`
6. Script: Build → Connect → Backup → Generate → Review → Confirm → Apply → Verify → ✅
7. Show backup/log locations, remind to verify functionality

**Connection fails:** Diagnose connection string/server, ⚠️ warn production issue, **STOP**

**Build fails:** Show errors, try auto-fix, if fixed re-run else **STOP** with ⚠️ warn

**User skips backup:** ⚠️ ⚠️ ⚠️ **CRITICAL WARNING** extremely risky, ask again, if confirmed proceed with caution else **STOP**

## Important Notes

- ⚠️ ⚠️ ⚠️ **PRODUCTION** - Extreme caution required
- ✅ Backup MANDATORY, review script before applying, verify functionality after
- ✅ Idempotent migrations - safe to run multiple times
- ⚠️ Environment: `ProductionRemote`, Config: `appsettings.ProductionRemote.json`
- ⚠️ Backups: `scripts/backups/ProductionRemote/`, Logs: `scripts/logs/`
- 📦 Keeps last 5 backups automatically
- 🚨 Have rollback plan, test in non-prod first, monitor after migration

