|
| 1 | +# EF Migrations Setup for Pathfinder Honor Manager API |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This project has been configured with Entity Framework Core migrations for safe, controlled database schema management. The setup includes a **baseline migration** approach for existing databases, ensuring compatibility with both local development and production deployment. |
| 6 | + |
| 7 | +## What Was Implemented |
| 8 | + |
| 9 | +### 1. EF Infrastructure |
| 10 | +- ✅ Added `Microsoft.EntityFrameworkCore.Design` package |
| 11 | +- ✅ Created baseline migration for existing database schema |
| 12 | +- ✅ Configured local development environment |
| 13 | +- ✅ Integrated with existing production deployment pipeline |
| 14 | + |
| 15 | +### 2. Baseline Migration Approach |
| 16 | +- ✅ **Existing Database Integration** - No database recreation required |
| 17 | +- ✅ **Baseline Migration** - `20250826224824_InitialSchemaWithProperDeleteBehavior` captures current schema with safe delete behavior |
| 18 | +- ✅ **Local Development Ready** - Migrations work with existing local database |
| 19 | +- ✅ **Production Compatible** - Pipeline already configured for EF migrations |
| 20 | + |
| 21 | +### 3. Safety Features |
| 22 | +- ✅ **Zero automatic deletions** - EF only adds, never removes |
| 23 | +- ✅ **Existing data preserved** - All current records remain intact |
| 24 | +- ✅ **Migration history tracking** - `__EFMigrationsHistory` table properly configured |
| 25 | +- ✅ **Pipeline control** - Migrations run automatically during deployment |
| 26 | +- ✅ **Safe delete behavior** - Explicit configuration prevents unwanted cascade deletes |
| 27 | + |
| 28 | +### 4. Delete Behavior Configuration |
| 29 | +- ✅ **RESTRICT by default** - Prevents accidental data loss from parent deletions |
| 30 | +- ✅ **Logical cascades only** - Pathfinder deletion removes their achievements/honors |
| 31 | +- ✅ **Protected reference data** - Cannot delete clubs, honors, achievements if in use |
| 32 | +- ✅ **Explicit configuration** - All relationships explicitly defined in `OnModelCreating` |
| 33 | + |
| 34 | +## How It Works |
| 35 | + |
| 36 | +### Local Development Setup |
| 37 | +```bash |
| 38 | +# 1. Ensure baseline migration is applied (one-time setup) |
| 39 | +psql -h localhost -U dbuser -d pathfinder -c "INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\") VALUES ('20250826224824_InitialSchemaWithProperDeleteBehavior', '9.0.8');" |
| 40 | + |
| 41 | +# 2. Verify migration status |
| 42 | +dotnet ef migrations list --project PathfinderHonorManager |
| 43 | + |
| 44 | +# 3. Make model changes and create new migrations |
| 45 | +dotnet ef migrations add AddNewFeature --project PathfinderHonorManager |
| 46 | + |
| 47 | +# 4. Test locally |
| 48 | +dotnet ef database update --project PathfinderHonorManager |
| 49 | +``` |
| 50 | + |
| 51 | +### Production Deployment |
| 52 | +The existing GitHub Actions pipeline (`.github/workflows/main_pathfinderhonormanager.yml`) already includes: |
| 53 | +- **EF migrations integration** - `dotnet ef database update --connection "${{ secrets.PRODCONNECTIONSTRING }}"` |
| 54 | +- **Automatic migration application** - Runs before app deployment |
| 55 | +- **Zero manual intervention** - Migrations apply automatically |
| 56 | + |
| 57 | +## Baseline Migration Details |
| 58 | + |
| 59 | +### What Was Created |
| 60 | +- **Migration**: `20250826224824_InitialSchemaWithProperDeleteBehavior` |
| 61 | +- **Purpose**: Represents the current database schema state with safe delete behavior |
| 62 | +- **Status**: Manually marked as applied in `__EFMigrationsHistory` |
| 63 | +- **Approach**: Standard EF Core baseline migration pattern with explicit delete behavior configuration |
| 64 | + |
| 65 | +### Why This Approach |
| 66 | +1. **Existing Database** - Your database already has the schema |
| 67 | +2. **No Recreation** - Avoids "relation already exists" errors |
| 68 | +3. **Future Migrations** - Enables normal EF Core migration workflow |
| 69 | +4. **Production Ready** - Pipeline will recognize this as baseline |
| 70 | + |
| 71 | +## Development Workflow |
| 72 | + |
| 73 | +### Creating New Migrations |
| 74 | +```bash |
| 75 | +# 1. Make model changes in your C# code |
| 76 | +# 2. Create new migration |
| 77 | +dotnet ef migrations add DescriptiveMigrationName --project PathfinderHonorManager |
| 78 | + |
| 79 | +# 3. Review generated migration file |
| 80 | +# 4. Test locally (optional) |
| 81 | +dotnet ef database update --project PathfinderHonorManager |
| 82 | + |
| 83 | +# 5. Commit and push |
| 84 | +git add . |
| 85 | +git commit -m "Add new feature with migration" |
| 86 | +git push origin main |
| 87 | +``` |
| 88 | + |
| 89 | +### Local Testing |
| 90 | +```bash |
| 91 | +# Check migration status |
| 92 | +dotnet ef migrations list --project PathfinderHonorManager |
| 93 | + |
| 94 | +# Apply pending migrations |
| 95 | +dotnet ef database update --project PathfinderHonorManager |
| 96 | + |
| 97 | +# Generate SQL script for review |
| 98 | +dotnet ef migrations script --project PathfinderHonorManager --output migration.sql |
| 99 | +``` |
| 100 | + |
| 101 | +## Configuration |
| 102 | + |
| 103 | +### Required Environment Variables |
| 104 | +- `PathfinderCS` - Database connection string |
| 105 | +- `Auth0:Domain` - Auth0 domain |
| 106 | +- `Auth0:Audience` - Auth0 audience |
| 107 | +- `Auth0:ClientId` - Auth0 client ID |
| 108 | + |
| 109 | +### Database Connection |
| 110 | +- **Local**: `Host=localhost;Database=pathfinder;Username=dbuser;Password=yourpassword` |
| 111 | +- **Production**: Configured via GitHub Secrets (`PRODCONNECTIONSTRING`) |
| 112 | + |
| 113 | +## Monitoring & Troubleshooting |
| 114 | + |
| 115 | +### Health Check Endpoints |
| 116 | +- `/health` - Overall health status |
| 117 | +- `/health/ready` - Readiness check |
| 118 | +- `/health/live` - Liveness check |
| 119 | + |
| 120 | +### Migration Status |
| 121 | +```bash |
| 122 | +# Check current migration status |
| 123 | +dotnet ef migrations list --project PathfinderHonorManager |
| 124 | + |
| 125 | +# Check database state |
| 126 | +dotnet ef database update --dry-run --project PathfinderHonorManager |
| 127 | +``` |
| 128 | + |
| 129 | +### Common Issues & Solutions |
| 130 | + |
| 131 | +#### "Relation already exists" Error |
| 132 | +**Cause**: Migration trying to create tables that already exist |
| 133 | +**Solution**: Ensure baseline migration is marked as applied in `__EFMigrationsHistory` |
| 134 | + |
| 135 | +```sql |
| 136 | +INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion") |
| 137 | +VALUES ('20250826224824_InitialSchemaWithProperDeleteBehavior', '9.0.8'); |
| 138 | +``` |
| 139 | + |
| 140 | +#### Migration Not Found |
| 141 | +**Cause**: Local database out of sync with migration files |
| 142 | +**Solution**: Check `__EFMigrationsHistory` table and sync accordingly |
| 143 | + |
| 144 | +## Best Practices |
| 145 | + |
| 146 | +### Development |
| 147 | +- ✅ **Baseline Migration** - One-time setup for existing databases |
| 148 | +- ✅ **Descriptive Names** - Use clear migration names like `AddUserProfileTable` |
| 149 | +- ✅ **Small Changes** - Keep migrations focused and incremental |
| 150 | +- ✅ **Local Testing** - Test migrations locally before committing |
| 151 | + |
| 152 | +### Deployment |
| 153 | +- ✅ **Pipeline Integration** - Migrations run automatically |
| 154 | +- ✅ **No Manual Steps** - Production deployment is fully automated |
| 155 | +- ✅ **Health Monitoring** - Use health checks to verify deployment |
| 156 | +- ✅ **Backup Strategy** - Ensure database backups before major changes |
| 157 | + |
| 158 | +### Maintenance |
| 159 | +- ✅ **Migration History** - Monitor `__EFMigrationsHistory` table |
| 160 | +- ✅ **Clean Up** - Remove old migration files when no longer needed |
| 161 | +- ✅ **Documentation** - Document complex schema changes |
| 162 | +- ✅ **Version Control** - Keep migration files in source control |
| 163 | + |
| 164 | +## Rollback Procedures |
| 165 | + |
| 166 | +### Emergency Rollback |
| 167 | +```bash |
| 168 | +# 1. Stop application |
| 169 | +# 2. Rollback database to previous migration |
| 170 | +dotnet ef database update PreviousMigrationName --project PathfinderHonorManager |
| 171 | + |
| 172 | +# 3. Redeploy previous application version |
| 173 | +# 4. Verify data integrity |
| 174 | +``` |
| 175 | + |
| 176 | +### Planned Rollback |
| 177 | +```bash |
| 178 | +# 1. Create rollback migration |
| 179 | +dotnet ef migrations add RollbackFeature --project PathfinderHonorManager |
| 180 | + |
| 181 | +# 2. Test rollback locally |
| 182 | +# 3. Deploy through pipeline |
| 183 | +# 4. Monitor health checks |
| 184 | +``` |
| 185 | + |
| 186 | +## What's Different from Standard EF Setup |
| 187 | + |
| 188 | +### Standard EF Setup |
| 189 | +- Creates database from scratch |
| 190 | +- First migration creates all tables |
| 191 | +- Works with empty databases |
| 192 | + |
| 193 | +### Our Baseline Approach |
| 194 | +- Works with existing database |
| 195 | +- Baseline migration represents current state |
| 196 | +- Future migrations are incremental |
| 197 | +- No database recreation required |
| 198 | + |
| 199 | +## Support |
| 200 | + |
| 201 | +For issues or questions: |
| 202 | +1. Check migration status: `dotnet ef migrations list` |
| 203 | +2. Verify `__EFMigrationsHistory` table contents |
| 204 | +3. Test locally with development database |
| 205 | +4. Check pipeline execution logs |
| 206 | + |
| 207 | +## Next Steps |
| 208 | + |
| 209 | +1. ✅ **Baseline Migration Complete** - Local development ready |
| 210 | +2. ✅ **Production Pipeline Ready** - Migrations will run automatically |
| 211 | +3. **Create New Features** - Add new migrations as needed |
| 212 | +4. **Monitor Deployments** - Verify migrations apply successfully |
| 213 | +5. **Team Documentation** - Share this workflow with your team |
| 214 | + |
| 215 | +## Git Configuration |
| 216 | + |
| 217 | +### .gitignore Updates |
| 218 | +Added EF Core specific entries: |
| 219 | +``` |
| 220 | +# Entity Framework Core |
| 221 | +*.migration.sql |
| 222 | +migration.sql |
| 223 | +baseline-migration.sql |
| 224 | +*.efmigrations |
| 225 | +``` |
| 226 | + |
| 227 | +### What's Tracked |
| 228 | +- ✅ `Migrations/` folder - All migration files |
| 229 | +- ✅ `*.cs` migration files - Migration classes |
| 230 | +- ✅ `*.Designer.cs` files - Migration designer files |
| 231 | +- ✅ `PathfinderContextModelSnapshot.cs` - Model snapshot |
| 232 | + |
| 233 | +### What's Ignored |
| 234 | +- ❌ `*.migration.sql` - Generated SQL scripts |
| 235 | +- ❌ `baseline-migration.sql` - Temporary baseline script |
| 236 | +- ❌ `*.efmigrations` - EF Core artifacts |
0 commit comments