Database Deployments with Automatic Rollbacks, part II: Packaging the Database Updates

This post is part of a series on deploying database updates with automatic rollbacks using EF Core Migrations and Octopus Deploy. In this part of the series, we’ll bundle the database updates in a NuGet package that requires no extra tooling other than what already exists on the database server.

All posts in the series:

Part I: Where We Are, and Where We Want To Be
Part II: Packaging the Database Updates (this post)
Part III: Deploying the Database Updates (not yet published)
Part IV: Adding Automatic Rollback of Application Services (not yet published)
Part V: Summary of the Series (not yet published)

All the scripts in this series are available in this gist.

Requirements on Our Package

We want our deployment to fulfill the following requirements:

  • Each migration is applied in its own transaction, so the database never ends up in a state where only part of a migration was applied.
  • Deploying the migrations requires no extra tooling on the server. (We’ll assume that the Octopus tentacle and SQLCMD.exe are available.)
  • The deployment is smart enough to skip the step entirely if all the database is up to date.

Wrapping Migrations in a Transaction

The dotnet ef  utility can’t generate scripts that include transaction boundaries, so we’ll create a small wrapper script that uses SQLCMD directives to wrap another script in a transaction:

Next, we’ll generate one script file for each migration. dotnet ef  sadly can’t do this out of the box either, but it’s quite easy to loop over all migrations and do it manually:

Now, we can issue the following command once for each migration to apply it within the context of a transaction:

Finally, we’ll package all the scripts in a NuGet package using the following nuspec file:

and add a command to actually create the package to the bottom of Package.ps1:

You’ll note that we haven’t yet created the script Deploy.ps1 referenced in the nuspec file. That’s the topic of the next post in the series.

Leave a Reply