Pular para o conteúdo principal
Versão: main (dev) 🚧

ADR-022 — Every migration carries its SQL script; DbMigrator ships as a local tool; production applies the scripts

Date: 2026-10-21 Status: Accepted Relates to: ADR-014 (migrations applied on startup only when DataOptions:ApplyMigrations is on), ADR-020 (dbo holds only __EFMigrationsHistory). Restores: the ITLab.WorkGuardian flow, where the DbMigrator creates every migration together with its SQL script.

Context​

ADR-014 made applying the schema a deployment decision: the versioned default is ApplyMigrations = false, so a real environment gets its schema from a DBA or a pipeline. Runbook 03 had them generate a cumulative --idempotent script at release time, from the release commit — an artefact nobody had reviewed before the release, and that nobody had ever applied: the review of this ADR found that it failed on an empty database. Five migrations ran raw SQL (and one a filtered index) against a column created in the same migration. dotnet ef database update, which every test and every developer used, sends one statement per command; a script is a batch that SQL Server compiles whole before running it, and the batch stopped with Msg 207 Invalid column name.

ITLab.WorkGuardian already used the DbMigrator, a thin CLI over dotnet ef that writes <MigrationsPath>/Scripts/<MigrationId>.sql every time it adds a migration. It was a 79 MB self-contained Windows binary committed to the repository, with a hard-coded --output-dir, a "last migration" picked by file write time and an exit code of 0 whatever happened.

Decision​

  1. A migration is three files, committed together: <Id>.cs, <Id>.Designer.cs and Persistence/Migrations/Scripts/<Id>.sql, the incremental script from the previous migration (no --idempotent, the WorkGuardian contract). Scripts/Baseline.sql covers, from an empty database, every migration it records in the history; later migrations have their own script.
  2. The scripts are the production contract (runbook 03, path A): read MAX(MigrationId) from the history, apply the later scripts in id order with sqlcmd -b -I; Baseline.sql only on an empty database; an environment behind the baseline gets a delta from dotnet dbmigrator -s. One owner of the schema per environment: with path A, ApplyMigrations is off on every host.
  3. Scripts are safe to apply. Every script starts with SET XACT_ABORT ON; SET QUOTED_IDENTIFIER ON;, so a failing statement rolls its whole migration back instead of committing the rest of the batch. The history table is [dbo].[__EFMigrationsHistory] explicitly (MigrationsHistoryTable), so the script does not depend on the default schema of the DBA's login. Raw SQL that reads a column created or renamed in the same migration, and a filtered index on a new column, go through EXEC(N'…').
  4. DbMigrator 1.3.0 is a dotnet tool (ITLab.Template.DbMigrator, command dbmigrator, feed ITLab.Template), pinned with dotnet-ef in .config/dotnet-tools.json. No binary in git; the same command on every OS. It derives --output-dir from MigrationsPath, orders migrations by id, exits non-zero on failure and on invalid usage, passes arguments by ArgumentList, runs dotnet by absolute path, writes -s deltas outside Scripts/, and -u shows its target and refuses anything but Development.
  5. Guarded twice. EveryMigrationHasSqlScriptTests (Infrastructure.Tests) fails on a migration with neither a script nor a line in Baseline.sql, a script with no migration, a baseline that records a migration that no longer exists, or a script without the safety header. The CI (scripts/verify-migration-scripts.ps1, Azure Pipelines and GitHub) applies every script to an empty SQL Server and fails unless the schema equals the one dotnet ef database update builds.

Consequences​

  • The schema of a release is reviewed when its migration is reviewed, not reconstructed at deployment time, and a script that does not compile as a batch fails CI on the commit that introduced it.
  • Restoring a generated project needs the ITLab.Template feed for the tool too — already true for the DevPack.
  • A scaffold that redesigns the domain from scratch deletes every shipped migration, the snapshot and Baseline.sql, then runs dotnet dbmigrator -a InitialCreate…; its script starts from the empty database.
  • WorkGuardian and the template pin the same tool; a new DbMigrator version is a manifest bump in each.
  • The EXEC(N'…') rule is the one thing the test cannot see; the CI job is what enforces it.

Alternatives considered​

  • DbMigrator.exe committed to the scaffold (this ADR's first draft). Rejected in review: an unsigned binary in every generated repository whose only integrity check was a hash in the same commit, Windows-only, and every version kept in git forever.
  • The cumulative --idempotent script of the release as the production contract. It works now that the migrations are fixed, but it is generated at release time and never reviewed as part of the migration, and it would leave two procedures for the same task. The committed scripts are both what is reviewed and what is applied.
  • Moving the migrations to Data/EntityFrameworkCore/Migrations to match the old hard-coded path. Rejected: it changes the canon, the namespace of 24 migrations and the docs to fit another tool's literal.
  • Post-editing only Baseline.sql instead of fixing the migrations. Rejected: the baseline would stop being regenerable, and the -s deltas and any idempotent script would stay broken.
  • The DbMigrator source as a project in the template, or in the DevPack. Rejected: every generated project would inherit the tool's maintenance, and the DevPack is a library consumed by code (G-01).