DbUp Oracle (edit)
Releases · DbUp/DbUp (github.com)
Using Database Project and DbUp for database management - Kamil Grzybek
Database Migrations with DbUp – Eric L. Anderson (elanderson.net) (HAY HAY HAY)
Simplifying database development with docker and DbUp | Blog (mcode.it) (HAY HAY HAY)
Snippet
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="dbup-core" version="4.5.0" targetFramework="net472" /> <package id="dbup-oracle" version="4.5.0" targetFramework="net472" /> <package id="Oracle.ManagedDataAccess" version="18.3.0" targetFramework="net472" /> </packages>
Snippet
using DbUp; using DbUp.Oracle; using System; using System.Linq; using System.Reflection; namespace Oracle.DbUp { class Program { [Obsolete] static void Main(string[] args) { var connectionString = args.FirstOrDefault() ?? "DATA SOURCE=localhost/ORCL;USER ID=sa;PASSWORD=123456;Min Pool Size=5;Decr Pool Size=10;PERSIST SECURITY INFO=True"; var upgrader = DeployChanges.To .OracleDatabase(connectionString) .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly()) .LogToConsole() .Build(); var result = upgrader.PerformUpgrade(); if (!result.Successful) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(result.Error); Console.ResetColor(); Environment.Exit(-1); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(value: "Success!"); Console.ResetColor(); Environment.Exit(0); } } }
Database Migrations with DbUp
Managing database schemas can be a challenging problem. It is also an area where there is no one size fits all solution (not sure that is ever really true for anything). Solutions range from manual script management, database vendor-specific options (such as DACPAC for Microsoft SQL Server), to migrations. This post will be looking at one of the options for a migration based approach using DbUp.
Sample Database
I needed a sample database to start off with so I dug up one of my posts for Getting a Sample SQL Server Database to guide me through getting Microsoft’s WideWorldImporters sample database downloaded and restored.
DbUp Console Application
There are quite a few ways DbUp can be used, for this example, we are going to be using it from a .NET Core Console application. From a terminal use the following command to create a new console application in the directory you want the application in.
Now we can use the following command to add the DbUp NuGet package to the sample project. In this case, we are using the SQL Server package, but there are packages for quite a few database providers so install the one that is appropriate for you.
Next, open the project in Visual Studio (or any editor but part of how this example is setup is easier in Visual Studio). In the Program class replace all the code with the following. We will look at a couple big of this code that below.
The bit below is trying to pull the connection string for SQL Server out of the first argument passed from the terminal to the application and if no arguments were passed in then it falls back to a hardcoded value. This works great for our sample, but I would advise against the fallback value for production use. It is always a bad day when you think your migrations have run successfully but it was on the wrong database because of a fall back value.
This next section is where all the setup happens for which database to deploy to, where to find the scripts to run, and where to log. There are a lot of options provided by DbUp in this area and I recommend checking out the docs under the More Info section for the details.
Finally, the following is where the scripts are actually executed against the database.
The rest of the function is dealing with displaying to the console the results of the scripts running.
Adding Scripts
Keep in mind that we are using the Embedded Scripts option so DbUp is going to find all the embedded files that end with ‘.sql’. I have added a Scripts directory to the project so the scripts don’t clutter the root of the project, but that isn’t a requirement. Do not that how the files are named will control the order in which the scripts get executed so make sure you establish a good naming convention upfront. For our sample, I added a file named 0001-TestScript.sql to the Scripts directory. Since we are using the embedded scripts provider it is critical that after adding the file we go to its properties and set the Build Action to Embedded resource.
The script file itself doesn’t do anything in this case except select a value.
Trying it out
At this point, we can hit Run in Visual Studio and it will execute our new script. The output will look something like the following.
If we were to run the application again it would tell us that no new scripts need to be executed. DbUp like all migration based solutions I have encountered use a table in the database to keep a record of what migrations have been run. The default table for DbUp is SchemaVersion and it consists of an Id, ScriptName, and Applied columns. Given this structure, it is important to keep in mind that if you rename a script that has already been executed on a database DbUp will see that as a different script and execute it again.
Wrapping Up
In the docs for DbUp there is a philosophy section and one of the points is that a database is a result of a set of transitions, not just its new state vs. its old state. This point is key to why I am a fan of a migration based approach.
Check out the DbUp docs and GitHub repo for more information.