Overview
DatabaseMigrationTools is a .NET Framework 4.7.2 console application (NuGet job) that runs Entity Framework Code First database migrations against the four core SQL Server databases used by NuGet.org:
- GalleryDatabase — the primary NuGet package index store
- SupportRequestDatabase — admin support ticket data
- ValidationDatabase — package signing / validation pipeline state
- CatalogValidationDatabase — catalog consistency validation state
MigrationTargetDatabase argument that selects which database to migrate. A separate NuGet package is published for each target (four .nuspec files), allowing deployment pipelines to trigger migrations independently per database.
The tool adds a hard 30-second
Thread.Sleep after the job completes. This is a documented workaround for an Application Insights SDK bug where TelemetryChannel.Flush() is unreliable.Role in System
Deployment Gate
Runs as a post-deploy step (via
Scripts/PostDeploy.ps1 → RunJob.cmd) before traffic is shifted to a new deployment slot, ensuring the schema is always ahead of the application code.Multi-Database Fan-Out
A single compiled binary covers all four databases. The
MigrationTargetDatabase argument selects the context at runtime, so four separate NuGet packages can be published from one build artifact.Entra ID–Aware
All
*DbMigrationContext classes capture the SQL connection’s AccessToken at construction time and restore it before every EF context creation via SetSqlConnectionAccessToken(), supporting MSI / service principal authentication.Migration Safety Check
Before applying any pending migration,
Job.CheckIsValidMigration() compares the ordered list of applied database migrations against the local code migrations to detect history divergence.Key Files and Classes
Classes in the companion library
NuGet.Services.DatabaseMigration:
Dependencies
NuGet Packages
Internal Project References
Deployment Flow
Notable Patterns and Quirks
Static context factory pattern. Each
*DbMigrationContext constructor assigns a lambda to a static *ContextFactory property. This side-effectful static mutation works for a single-run console job but would break under concurrent or parallel migration runs in the same process.Skipped Gallery migration.
Job.cs hard-codes SkipGalleryDatabaseMigrationFile = "201304262245205_CuratedPackagesUniqueIndex". This migration exists in the database history but has no corresponding local file; the validation loop skips it to avoid a spurious divergence error.