# feature/decomp-sprint Stabilization Plan

**Goal:** Safe, stable state for DEV → MAIN deployment with domain decomposition work intact

**Date:** 2026-01-23
**Target Branch:** `feature/decomp-sprint`
**Deployment Path:** decomp-sprint → DEV → MAIN
**Timeline:** 2-3 hours

---

## Current State Assessment

### ✅ What's Working
- All 5 domains implemented (GALA, ORDER, VENUE, IAM, NOTIF)
- Code blockers fixed (RevolutReportingClient, Order::newFactory)
- Database user/credentials configured
- 6 new domain tables created

### ❌ What's Broken
- Tests fail on `RefreshDatabase` (migration conflicts)
- Schema dump outdated (42 days behind production)
- 73 migration files conflict with schema dump
- Cannot verify domain code quality via tests

### 🎯 Target State
- Production schema as baseline (253 migrations)
- Domain tables as NEW migrations (clean history)
- All tests passing (80-90% estimated)
- Ready for DEV deployment
- Zero production disruption when merged

---

## Strategy: Production Baseline + Domain Additive Migrations

### Phase 1: Replace Schema Baseline (15 min)

**Actions:**
1. Backup current decomp schema
2. Copy production schema as new baseline
3. Create testing-specific schema copy
4. Update schema dump marker migration

**Commands:**
```bash
cd /Users/charlie/code/showprima-notif-decomp

# Backup current schema
cp database/schema/mysql-schema.sql database/schema/backup_decomp_schema_20260123.sql

# Replace with production
cp database/schema/reference/production_source_of_truth_20260123.sql database/schema/mysql-schema.sql

# Create testing schema
cp database/schema/mysql-schema.sql database/schema/mysql_testing-schema.sql
```

**Result:** Schema now matches production (253 migrations, batch 1001)

---

### Phase 2: Archive Conflicting Migrations (10 min)

**Actions:**
1. Archive ALL migration files currently in `database/migrations/`
2. Keep ONLY the schema marker: `0000_00_00_000000_load_schema_dump.php`

**Rationale:** Production schema contains all tables/structure up to 2026-01-19. Any migration files that overlap will cause conflicts.

**Commands:**
```bash
cd /Users/charlie/code/showprima-notif-decomp

# Move all migrations except schema marker to archive
mkdir -p database/migrations_archive/pre_stabilization_$(date +%Y%m%d)
find database/migrations -name "*.php" ! -name "0000_00_00_000000_load_schema_dump.php" \
  -exec mv {} database/migrations_archive/pre_stabilization_$(date +%Y%m%d)/ \;

# Verify only schema marker remains
ls -la database/migrations/
```

**Result:** Clean slate - only schema marker migration remains

---

### Phase 3: Create Domain-Specific Migrations (30 min)

**Actions:**
Create NEW migrations for tables that exist in decomp but NOT in production.

**Domain Tables to Migrate:**
1. `access_logs` - Request logging
2. `features` - Feature flag system  
3. `gala_state_transitions` - GALA domain state machine
4. `permission_overrides` - IAM domain overrides
5. `template_versions` - VENUE domain versioning
6. `whatsapp_logs` - NOTIF domain (IF structure differs from production)

**Commands:**
```bash
cd /Users/charlie/code/showprima-notif-decomp

# Check if these tables exist in production schema
for table in access_logs features gala_state_transitions permission_overrides template_versions; do
  echo "Checking $table..."
  grep "CREATE TABLE \`$table\`" database/schema/mysql-schema.sql && echo "EXISTS" || echo "MISSING"
done

# For MISSING tables, create migrations
php artisan make:migration create_access_logs_table
php artisan make:migration create_features_table
php artisan make:migration create_gala_state_transitions_table
php artisan make:migration create_permission_overrides_table
php artisan make:migration create_template_versions_table
```

**Note:** Migration files will be filled in with actual CREATE TABLE statements from decomp schema.

**Result:** Clean, new migrations for domain tables only

---

### Phase 4: Verify Database Works (15 min)

**Actions:**
1. Fresh test database
2. Load production schema
3. Run new domain migrations
4. Verify all tables exist

**Commands:**
```bash
cd /Users/charlie/code/showprima-notif-decomp

# Drop and recreate test DB
docker exec showprima_mysql mysql -u root -p'root_secret' \
  -e "DROP DATABASE IF EXISTS showprima_test; CREATE DATABASE showprima_test;"

# Grant permissions
docker exec showprima_mysql mysql -u root -p'root_secret' \
  -e "GRANT ALL ON showprima_test.* TO 'showprima'@'%';"

# Run migrations (will load schema + run new migrations)
php artisan migrate --env=testing --force

# Verify table count
docker exec showprima_mysql mysql -u showprima -p'showprima_test_pass' showprima_test \
  -e "SELECT COUNT(*) as tables FROM information_schema.tables WHERE table_schema='showprima_test';"
```

**Expected:** ~143 tables (137 from production + 6 domain tables)

**Result:** Test database ready for tests

---

### Phase 5: Run Test Suite (30 min)

**Actions:**
1. Run domain tests in parallel
2. Collect pass/fail metrics
3. Document any remaining failures

**Commands:**
```bash
cd /Users/charlie/code/showprima-notif-decomp

# Quick validation tests (no DB)
php artisan test tests/Unit/Domains/Notifications/Contracts/ChannelInterfaceTest.php

# Full domain test suites
php artisan test tests/Unit/Domains/Gala
php artisan test tests/Unit/Domains/Ordering  
php artisan test tests/Unit/Domains/Venue
php artisan test tests/Unit/Domains/Notifications

# Feature tests
php artisan test tests/Feature/Domains/Ordering/GalaIntegrationTest.php
```

**Expected:** 80-90% pass rate

**Result:** Test suite metrics documented

---

### Phase 6: Document Deployment State (15 min)

**Actions:**
1. Create DEPLOYMENT_READINESS.md
2. Document what's in decomp-sprint
3. Document DEV deployment steps
4. Document MAIN merge strategy

**Contents:**
- All code fixes applied
- Domain architecture complete
- Test coverage metrics
- Known issues (if any)
- DEV deployment checklist
- Rollback plan

**Result:** Clear documentation for deployment

---

## Safety Measures

### Pre-Flight Checks
- [ ] Production schema backed up
- [ ] decomp schema backed up  
- [ ] Migration files archived (not deleted)
- [ ] Test database isolated (showprima_test)
- [ ] No changes to production database

### Rollback Plan
If issues arise:
```bash
# Restore original decomp schema
cp database/schema/backup_decomp_schema_20260123.sql database/schema/mysql-schema.sql

# Restore migration files
cp -r database/migrations_archive/pre_stabilization_20260123/* database/migrations/
```

### Production Safety
- Production database UNTOUCHED during stabilization
- All work in test environment
- Schema changes are ADDITIVE only (new domain tables)
- No destructive migrations

---

## Success Criteria

✅ **Ready for DEV when:**
1. Test suite passes at >80%
2. RefreshDatabase works without conflicts
3. All domain tables present
4. Migration history clean
5. Documentation complete

✅ **Ready for MAIN when:**
1. DEV testing complete (separate phase)
2. Domain functionality validated
3. Performance benchmarks met
4. Rollback plan tested

---

## Timeline

| Phase | Duration | Status |
|-------|----------|--------|
| 1. Replace Schema | 15 min | Pending |
| 2. Archive Migrations | 10 min | Pending |
| 3. Create Domain Migrations | 30 min | Pending |
| 4. Verify Database | 15 min | Pending |
| 5. Run Test Suite | 30 min | Pending |
| 6. Document State | 15 min | Pending |
| **TOTAL** | **~2 hours** | **0% Complete** |

---

## Next Steps

1. Review this plan
2. Get approval to proceed
3. Execute phases sequentially
4. Validate at each step
5. Document results

---

**Ready to proceed?**

