# Module: DataAccess/DatabaseMigrator ## Purpose Runs DbUp-based SQL migrations against PostgreSQL on application startup. Ensures the database schema is up to date before the API begins serving requests. ## Public Interface ### DatabaseMigrator - Constructor: `DatabaseMigrator(string connectionString, ILogger? logger)` - `RunMigrations() → bool`: creates the database if missing (`EnsureDatabase.For.PostgresqlDatabase`), then runs all embedded SQL scripts matching `.Migrations.` from the DataAccess assembly. Returns `true` on success. ## Internal Logic - Uses `DbUp.DeployChanges` fluent API targeting PostgreSQL - Scripts are embedded resources filtered by path containing `.Migrations.` - Logs to console via DbUp's built-in `LogToConsole()` - On failure, logs the error and returns `false` ## Dependencies - NuGet: `dbup-postgresql` (6.0.3) - `Microsoft.Extensions.Logging` - Embedded SQL resources from `SatelliteProvider.DataAccess/Migrations/` ## Consumers - `Program.cs` — instantiated directly (not via DI) and called during startup. If migration fails, the application throws and does not start. ## Migrations (11 scripts) 1. `001_CreateTilesTable.sql` 2. `002_CreateRegionsTable.sql` 3. `003_CreateIndexes.sql` 4. `004_AddVersionColumn.sql` 5. `005_CreateRoutesTables.sql` 6. `006_AddStitchTilesToRegions.sql` 7. `007_AddRouteMapFields.sql` 8. `008_AddGeofenceFlagToRouteRegions.sql` 9. `009_AddGeofencePolygonIndex.sql` 10. `010_AddTilesZipToRoutes.sql` 11. `011_AddTileCoordinates.sql` ## Configuration Receives connection string directly as constructor parameter. ## External Integrations PostgreSQL — DDL operations via DbUp. ## Security None directly, but controls schema evolution. ## Tests No dedicated tests.