How to Use DbUp Without Embedded Scripts

I use DbUp for all my database migrations. It is super simple and when something goes wrong, you can fix it without much effort. So far, I never had any problems with the embedded scripts approach as it is explained in the Getting Started section of the official documentation and in my blog post.

However, while trying to get Docker working with DbUp I noticed that I must do a lot of extra work when I need to create a separate DbUp image for every migration project I use. It would be much simpler when I could reuse my DbUp project and only replace the SQL scripts. Unfortunately, the embedded scripts approach requires a rebuild of the project and I am back at the beginning.

Thanks to the flexibility of DbUp there is now another approach. We can put the SQL files into a separate folder outside our project. When we create the upgrader object, we replace the call to WithScriptsEmbeddedInAssembly() with the WithScriptsFromFileSystem() method and point it to your scripts folder:

DbUp will work as before, execute the scripts when they are new and updates the SchemaVersions table:

SchemaVersions table does not have a namespace attached to the filename

As you can see, there is no namespace attached to the file name when you use the scripts folder approach. Therefore, when you switch from one approach to the other, you first should check that you update the names of your scripts in the SchemaVersions table. Otherwise, DbUp will try to execute your scripts again and fail because the tables and views already exist.

 

Conclusion

It is a good idea to check from time to time the new features of the tools you use. Those features may help you to solve your new problems in a simple way without the need to code a solution from scratch.

 

2 thoughts on “How to Use DbUp Without Embedded Scripts”

  1. Can you create a GitHub example? Trying to understand where your scripts folder is in relationship to the solution and project. Thanks

    Reply

Leave a Comment

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