Skip to content

Gate and review migration scripts - #5469

Open
aasaa444 wants to merge 1 commit into
dotnet:mainfrom
aasaa444:review-gates-docs
Open

Gate and review migration scripts#5469
aasaa444 wants to merge 1 commit into
dotnet:mainfrom
aasaa444:review-gates-docs

Conversation

@aasaa444

@aasaa444 aasaa444 commented Sep 8, 2026

Copy link
Copy Markdown

Resolves #3515

Adds a page under the migrations section covering how to gate EF Core migration scripts in a deployment pipeline:

  • generating idempotent scripts for review (dotnet ef migrations script --idempotent);
  • reviewing the SQL via Azure DevOps approvals and gates (with a three-stage pipeline example);
  • generating rollback scripts with migrations script <from> <to> and testing them against a copy of the production schema;
  • when to use plain scripts vs migration bundles.

The workflow described is the one I run in production (EF Core 10 on SQL Server, gated SQL review, rollback scripts generated per migration).

@aasaa444

aasaa444 commented Sep 8, 2026

Copy link
Copy Markdown
Author

@dotnet-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The TOC is malformed, and the pipeline references an invalid download target and a bundle it never creates.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds guidance for reviewing and gating EF Core migration scripts in deployment pipelines.

Changes:

  • Adds Azure DevOps pipeline and approval examples.
  • Documents forward, rollback, and idempotent scripts.
  • Compares scripts with migration bundles.
File summaries
File Description
entity-framework/toc.yml Adds the new migration guidance to navigation.
entity-framework/core/managing-schemas/migrations/review-gates.md Introduces migration review, rollback, and deployment guidance.
Review details

Suppressed comments (2)

entity-framework/core/managing-schemas/migrations/review-gates.md:79

  • This repeats the unsupported Latest sentinel, so the documented rollback command fails for normal migration names. Use the same concrete newer and older migration identifiers as the paired example above.
dotnet ef migrations script Latest PreviousMigration -o artifacts/rollback.sql

entity-framework/core/managing-schemas/migrations/review-gates.md:76

  • This repeats the forward-only inclusion rule for a rollback range. When from is newer than to, the generated script runs Down for the newer from migration and leaves the to migration applied, so describe the arguments as starting and target states instead.
A forward script without a tested rollback script is only half the artifact. For every `apply.sql`, generate the matching rollback with `migrations script <from> <to>` (from is excluded, to is included):
  • Files reviewed: 2/2 changed files
  • Comments generated: 8
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +30 to +31
dotnet ef migrations script PreviousMigration Latest -o artifacts/apply.sql
dotnet ef migrations script Latest PreviousMigration -o artifacts/rollback.sql
jobs:
- job: build
steps:
- script: dotnet ef migrations script --idempotent -o $(Build.ArtifactStagingDirectory)/migrate.sql
Comment on lines +68 to +69
- download: sql
- script: dotnet $(System.ArtifactsDirectory)/sql/MigrationsBundle.dll --connection "$(ProdConnectionString)"
Comment thread entity-framework/toc.yml Outdated

## Generate scripts for review

An idempotent script can be applied to a database at any migration level and skips migrations that have already been applied. Generate it as a build artifact so reviewers always see the full pending SQL:
dotnet ef migrations script --idempotent -o artifacts/migrate.sql
```

For a single deployment, a versioned pair of scripts is often easier to review. The `from` migration is excluded, the `to` migration is included. These commands generate the forward script for the latest migration and the script that undoes it:
dotnet ef migrations script Latest PreviousMigration -o artifacts/rollback.sql
```

Test the rollback the same way the forward script is tested: apply it to a copy of the production schema, and check that the schema and data end up in the expected state. Store both scripts together so reviewers see them side by side.

## Scripts or bundles

[Migration bundles](applying.md#bundles) are the right artifact when deployments are automated end to end. Plain scripts are the right artifact when the SQL must be reviewed, signed off, or handed to a DBA. A gated deployment usually needs both: the script for the review gate, and a bundle built from the same migration for the deployment itself.
ms.date: 09/04/2026
uid: core/managing-schemas/migrations/review-gates
---
# Gate and review migration scripts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to core/managing-schemas/migrations/teams.md

Per AndriySvyryd's review: the gated review and rollback workflow
belongs in teams.md rather than a standalone page. Documents the
idempotent script review gate with provider caveats, an Azure DevOps
approvals example that deploys the reviewed script, and rollback script
generation with the data-loss warning.

Closes dotnet#3515
@aasaa444

aasaa444 commented Sep 13, 2026

Copy link
Copy Markdown
Author

Thanks for the review, @AndriySvyryd. Moved the gating and rollback guidance into teams.md as you suggested, and removed the standalone page with its TOC entry.

Also fixed what Copilot flagged on the old version:

  • the pipeline now publishes both migrate.sql and rollback.sql, and each later stage downloads the current artifact;
  • the deploy stage runs the approved migrate.sql via sqlcmd, so no bundle is generated;
  • the rollback command uses concrete migration names, and the page describes from and to as the initial and target database states;
  • added the provider caveat for --idempotent scripts (SQLite) and kept the data-loss warning for rollback scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to setup a reviewing migration (and rollback) gate for scripts in Azure DevOps?

3 participants