Dr. Upgrade or: How I Learned to Stop Worrying and Love psake and git

In my post describing a summary of sort of my experiences with upgrading to EPiServer CMS 6R2 I briefly mention that I was very glad that I had some powershell scripts and using git. Note that I haven’t created the script so all the credit here goes to Patrik Akselsson.

Database setup

Each developer runs a local database and then we have one for integration tests, one for UI-tests and one for external testing by the customer. All these databases are built from a master database. To automize the restore / backup process (amongst other things) we’re using psake which is is a build automation tool written in powershell as well as osql.exe which is a Sql Server tool to execute t-sql.

So, by simply executing the command .\build.ps1 .\local-database.ps1 what happens is that a backup of the master database is created and copied to my local machine and then restored on my local sql server instance.

function transfer-database([string]$name){
    backup-database $sourceDatabase "$name" "e:\sql\backup\${name}_$BuildType.bak"
    ensure-directory "$dbTarget"
    copy "\\$sourceDatabase\e$\sql\backup\${name}_$BuildType.bak" "$dbTarget"
    $db = (get-item $dbTarget).FullName
    restore-database $TargetDatabaseServer "$name" $BuildType "$db\${name}_$BuildType.bak" "$db" 


Due to the way git works creating a branch is an extremely cheap since no files are copied and the operation is completely local. I know that this perhaps says little if you’re not a Git user but I can’t really do it justice in this sort of post. Seriously, if you haven’t already, start looking it git.

An example of how this helped me

I did the initial upgrade on my machine (using my local database) and created a special upgrade branch.
I didn’t discover the bug in the sql scripts (more information can be found here) until I had the site in somewhat in a running state. So, upgrade wise my database was now upgraded and I had completed some specific code changes to get the R2 solution to build. So in essence I wanted to rerun the upgrade (now with the corrected sql-script) but I didn’t want to rewrite the code I had updated. And performing the upgrade again with those code changes in place would simply owerwrite the changes so I wanted to perform the upgrade on the code I had before the upgrade. Luckily this was as simple as executing the following commands

git checkout dev (my starting point that pointed to a commit just before the upgrade)
.\build.ps1 .\local-database.ps1 (restore my local database from a backup of the master database)
Re-reun the upgrade through deployment center
git checkout epi6R2 (the branch I had commited the R2 specific changes to)

And I was good to go again. And as for the rest of the team, once the master database was upgraded to R2 upgrading their local environment was a simple as

git pull origin master (pull from github)
.\build.ps1 .\local-database.ps1 (restore their local database from a backup of the master database)