In this post, we are going to walk through how to use DbUp to run specific migrations on every run instead of just once. If you are new to DbUp check out the following post to see how the sample project got to its current state.
Database Migrations with DbUp
Code-based Database Migrations with DbUp
Differentiating for Always Run
One of the keys to getting migrations that should run every time is being able to identify them separately from migrations that should only run once. This can be done at either the filename level or by placing all the always run files in a specific directory. This sample is going with the directory methods, but the file method would work basically the same way. In the sample project under the existing Scripts directory, I added an AlwaysRun directory. I also added an example script in this directory so we could verify that it is getting executed on every run. In the sample, the file is named Everytime.sql.
Based on the above when DbUp is processing it will see Everytime.sql as DbupTest.Scripts.AlwaysRun.Everytime.sql.
Filtering Out Always Run
Now that we have a way to identify the migrations we want to always run we need to filter them out of the existing upgrade process so that they don’t get included in the log of already executed migrations. In DbUp this log is referred to as the journal. The extension methods that setup how migrations will be located, either WithScriptsEmbeddedInAssembly or WithScriptsAndCodeEmbeddedInAssembly in our examples, can be provided with a filter which we will use to exclude migrations that are in the AlwaysRun directory. The following code is the upgrader with the filter in place.
var upgrader = DeployChanges.To .SqlDatabase(connectionString) .WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(), f => !f.Contains(".AlwaysRun.")) .LogToConsole() .Build(); var result = upgrader.PerformUpgrade();
Executing Always Run Migrations
Now that we have adjusted our original upgrader to exclude migrations in the AlwaysRun directory we need something to execute the always run migrations. To accomplish this we add a second upgrader that is filtered just to AlwaysRun scripts. The second really important bit about this second upgrader is that it uses a NullJournal. The NullJournal is what keeps the execution of the scripts from being logged which results in them always getting run. The following code was inserted after the above code. The highlighted bits point out the filter and null journal.
if (result.Successful) { var alwaysRunUpgrader = DeployChanges.To .SqlDatabase(connectionString) .WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(), f => f.Contains(".AlwaysRun.")) .JournalTo(new NullJournal()) .LogToConsole() .Build(); result = alwaysRunUpgrader.PerformUpgrade(); }
The following is the full Main function just in case the above code didn’t give enough context on how the code should fit together.
static int Main(string[] args) { var connectionString = args.FirstOrDefault() ?? "Server=localhost; Database=WideWorldImporters; Trusted_connection=true"; var upgrader = DeployChanges.To .SqlDatabase(connectionString) .WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(), f => !f.Contains(".AlwaysRun.")) .LogToConsole() .Build(); var result = upgrader.PerformUpgrade(); if (result.Successful) { var alwaysRunUpgrader = DeployChanges.To .SqlDatabase(connectionString) .WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(), f => f.Contains(".AlwaysRun.")) .JournalTo(new NullJournal()) .LogToConsole() .Build(); result = alwaysRunUpgrader.PerformUpgrade(); } if (!result.Successful) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(result.Error); Console.ResetColor(); #if DEBUG Console.ReadLine(); #endif return -1; } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Success!"); Console.ResetColor(); return 0; }
Wrapping Up
With the above changes we now have a project that can run normal migrations, code-base migrations, and always run migrations. No matter what style of database change management you decide on having the changes in some sort of source control system will be super helpful.
For more information on DbUp’s journalling check out the official docs.
Also published on Medium.