Recently I've been working with a client to improve their continuous integration process, using TeamCity and Octopus. As part of this process, I started looking at how we could copy the Episerver patches between environments automatically. If you've read, How To Create A New Episerver Database then you'll know there are a few useful Nuget commands that you can run in the package console to create a new Episerver database and patch it to the correct version, however, how does this work? In today's guide, I'm going to deep dive into the inner workings of these scripts! In your everyday development life, I admit the information contained in this post probably isn't that useful. As a developer working locally with Episerver, all you really need to know is that if you want to create a clean version of an Episerver database, you can use two functions via the Nuget package console to do this. If you are trying to set up some cool, funky continuous deployment process and you're fiddling with build server steps, then understanding how these scripts work will make your life a lot easier. I've created build scripts that have cleared down the Episerver database, called the update scripts, and then used a page import script to populate the content automatically. This can be really useful when you're initially building and testing a new website. Sound good, read on!

It's All Nuget Princess

Episerver is entirely installed via Nuget, so it makes sense that these scripts to update the database are also all pulled in via NuGet. When you install Episerver you'll need EPiServer.CMS.Core and EPiServer.Framework installed. If you look in your packages directory within the EPiServer.Framework, you'll see a folder called tools and a Powershell script called Upgrade.psm1:

How The Episerver Powershell Functions Work Under Da Hood 1

If you open the script in a text editor, you can see the two install functions, Update-EPiDatabase and Initialize-EPiDatabase:

How The Episerver Powershell Functions Work Under Da Hood 2

And...

How The Episerver Powershell Functions Work Under Da Hood 3

Initialize-EPiDatabase will call the EPiServer.Cms.Core.sq' script within the EPiServer.CMS.Core packages tools folder.

How The Episerver Powershell Functions Work Under Da Hood 4

When you run the Initialize-EPiDatabase in a PowerShell command prompt it should create a fully functioning database patched correctly to the version of Episerver you have installed. This is really useful for building a brand new website, say in your test/QA environment. If you simply want to upgrade your Episerver database (as you've upgraded Episerver), you will need to know about the Update-EPiDatabase function.

Update-EPiDatabase

The Update-EPiDatabase is a little bit more complex, as it iterates through a number of SQL scripts within the epiupdates folder. If you look within that folder you will see a number of update scripts, as seen below:

How The Episerver Powershell Functions Work Under Da Hood 5

When I first stumbled across these scripts, I thought what would happen if I batch these scripts up into a giant superscript and run them directly in my Ci/Cd pipeline against my different environments. The short answer, it fails. If you want to create an automatic Episerver database patching solution within your continuous deployment pipeline, you'll need to have a look at a different function. A function within Upgrade.psm1 called Export-EPiUpdates

If you want to learn more about how to use these SQL scripts outside of your Nuget package console, then I suggest you read, How To Export Episerver Database Update Patches To Use Within Your Build Process.

This concludes how these scripts work. As you can see there's a lot going on when you install Episerver for the first time that you were probably not aware of. Simply knowing these scripts exist, where they live and what they do should help you out. Happy Coding 🤘