Simple Database Migrations with DbUp

Keeping your database up-to-date with your code is an important task. There are many different approaches and even more tools to support you. However, they often come with a price you may not be willing to pay.

A different way goes DbUp. This little tool helps you to update your database in a simple, understandable manner. It can’t do everything, but what it can works great. Let’s have a deeper look at DbUp.

 

Installation

The simplest and most convenient way to use DbUp is within a command line application. That project can be added to your solution and then only cares about modifying your database. The installation itself is done by NuGet by entering this command into the Package Manager Console:

 

Setup

DbUp needs some code to configure the connection string to your database and the location of the scripts you want to run. You can take the code from the official documentation or use mine with an exactly specified connection string name:

 

Your DB Scripts

The next step is to create a folder called Scripts where you can put your migration scripts. You can change the name, but then you need to update the ScriptProvider as well.

The migration scripts are simple text files with SQL commands directly written to it. By using this simple approach, you can write your scripts in the SQL Server Management Studio and then copy and paste them into a migration file. Another great plus is that your database administrator could create the scripts for you, without any prerequisite knowledge for writing code.

If you start with an empty database, then your first migration may create a new table:

With the current configuration of DbUp your scripts must be embedded resources. You therefore now need to change the Build Action to Embedded Resource:

DbUp: Embedded Resource

The naming of the files is up to you. If you start the name with Script* it will be automatically detected. If you use a number directly after that, then the alphabetical sort order in Visual Studio matches the order of execution in DbUp. While this is not a must have, it makes the handling of those files much simpler.

 

Running the Migrations

To update your database, you need to build your command line application project and execute it. DbUp will connect to the database and run your scripts:

To prevent scripts from running over and over again, DbUp tracks the execution of your scripts in the table SchemaVersions. Every executed script will get an entry with the date and the script name:

DbUp: SchemaVersions

On the next run DbUp will check which scripts need to be executed and only run them.

 

Limitations

DbUp is so simple because it only allows you to go forward with your migrations. There is no rollback to a specific version. While there are some discussions about changing that, so far you only can change the database from one state to the next.

 

Conclusion

DbUp is a great little tool to manage your database. Simple SQL commands added to text files in a Scripts-folder are all you need to keep your database up-to-date with your code. It’s so simple that you definitely should give it a try.

10 thoughts on “Simple Database Migrations with DbUp”

  1. Another usefull feature with DBup is that you can use it against LocalDB, which enables integration tests to run against an updated / migrated schema each time. Flyway’s driver can’t connect to LocalDb, so DbUp becomes a great choice for this.

    Reply
  2. I have table changes ,after that I need those changes effect to qa not for production ,How can we achieve using DbUp

    Reply
    • Hi Srinivas,
      Excuse my late reply. For your use case we use two different DbUp projects. One that should run in all environments and one that only runs in QA / Test. This setup allows us to be very specific in which environment we run the scripts.

      Regards,
      Johnny

      Reply
  3. Hi Team,

    Can i call a Dbup project to a database project and execute script in database project by dbup-project

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.