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:
1 2 3 4 5 6 7 8 |
var upgrader = DeployChanges.To .SqlDatabase(connectionString) .WithScriptsFromFileSystem(@"../../../../Scripts/") //.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly()) .WithTransactionPerScript() .LogToConsole() .Build(); |
DbUp will work as before, execute the scripts when they are new and updates the SchemaVersions table:
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.
Can you create a GitHub example? Trying to understand where your scripts folder is in relationship to the solution and project. Thanks
Hi Troy,
You can find an example solution at https://github.com/jgraber/Blog_Snippets/tree/master/DbUp/DBMigrationWithExternalScripts/ that shows a small example.
Regards,
Johnny