Old Site

This site is archived. I now update and maintain my blog here.

Automate Umbraco database creation to quick launch Belle

Many thanks to Morten aka @sitereactor for the code snippet that makes this work.

At Moriyama, we are a fan of automating the boring stuff so we can spend as much time as possible doing the interesting stuff such as testing Belle (Umbraco 7). We decided to automate Belle installation, so we can set up environments really quickly.

Our weapon of choice for automation is NAnt, a .NET build tool which is ideal for automating tasks like downloading files, extracting zip files etc. We set about building a Belle installer based on our existing NAnt backup and restore script.

We hit a blocker when we tried to automate creating an empty Umbraco database. There isn't an SQL script nowadays. Thankfully Morten provided us with this code snippet.

using System.Reflection;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.SqlSyntax;

namespace ConsoleApplication3
    class Program
        static void Main(string[] args)
            var db = new UmbracoDatabase("server=.;database=belle;user id=belle;password=belle", "System.Data.SqlClient");

            SqlSyntaxContext.SqlSyntaxProvider = new SqlServerSyntaxProvider();

            //Create DB Schema
            //Get the method we want to run
            var methodToRun = typeof(PetaPocoExtensions).GetMethod("CreateDatabaseSchema", BindingFlags.Static | BindingFlags.NonPublic);

            //Invoke the Method - CreateDatabaseSchema
            methodToRun.Invoke(null, new object[] { db, false });

This is really nice because with a couple of references to Umbraco DLLs we can initialise an empty Umbraco database. It also works for version 6 and version 7, just reference the DLL that ships with the version of Umbraco.

One of the nice things about NAnt is that we can load assemblies into our build script and execute against them directly, so we can take the code above and run it directly from our build as follows:

<script language="C#" prefix="UmbracoExtension">
        <include name="System.Data.dll" />
        <include name="${web.root}/bin/Umbraco.Core.dll" />
        <include name="${web.root}/bin/MiniProfiler.dll" />
        <import namespace="System.Data.SqlClient" />
        <import namespace="System.Reflection" />
        <import namespace="Umbraco.Core.Persistence" />
        <import namespace="Umbraco.Core.Persistence.SqlSyntax" />
        public static void InitDb(string siteName) {
          var db = new UmbracoDatabase("server=.;database="+siteName+";user id="+siteName+";password="+siteName+";", "System.Data.SqlClient");             
          SqlSyntaxContext.SqlSyntaxProvider = new SqlServerSyntaxProvider();
          var methodToRun = typeof(PetaPocoExtensions).GetMethod("CreateDatabaseSchema", BindingFlags.Static | BindingFlags.NonPublic);
          methodToRun.Invoke(null, new object[] { db, false });
    <property name="init.db" value="${UmbracoExtension::InitDb(site.name)}"/>

The result? A local Umbraco setup against a nightly of Belle in about 30 seconds including the time taken to download and unpack the release (as shown in the screencast below).


Leave a comment