Automate Umbraco database creation to quick launch Belle

November 12, 2013

This is an old archived post, content maybe out of date, links may be broken and layout may be broken.

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">
      <references>
        <include name="System.Data.dll" />
        <include name="${web.root}/bin/Umbraco.Core.dll" />
        <include name="${web.root}/bin/MiniProfiler.dll" />
      </references>
      <imports>
        <import namespace="System.Data.SqlClient" />
        <import namespace="System.Reflection" />
        <import namespace="Umbraco.Core.Persistence" />
        <import namespace="Umbraco.Core.Persistence.SqlSyntax" />
      </imports>
      <code>
      <![CDATA[
        [Function("InitDb")]
        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 });
        }
      ]]>
      </code>
    </script>
    <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).

Comments

Ismail Mayat - November 12, 2013

Darren,

Awesome I am going to try this. Quick question though I guess you have the nant script as part of your build so would it not run each time? Or is it called from a batch file so only ever run once?

Regards

Ismail

Darren - November 12, 2013

Hi Ismail, the extract above is from a build that we run on demand whenever we want a new empty install.

Warren Buckley - November 12, 2013

Hey Darren nice bit of work. I use the same piece of code with Morten’s help last week at the hackathon to achieve the same thing of setting up the DB Schema.

I am using the same code in my Visual Studio File New Project Umbraco Template http://visualstudiogallery.msdn.microsoft.com/e8893648-f2f1-47cf-909c-53fff87a679a http://www.screenr.com/slJH

Which may be of use to you or perhaps anyone else reading this.

Thanks, Warren :)

Dipun Mistry - November 12, 2013

I just want to say this is awesome and genius… I will be using this gem of knowledge to my advantage.

Umbraco is full of Einsteins. End of.

Sean Feldman - May 28, 2014

Thank you for this tip, helped a lot.

I have modified the code to go straight to PetaPocoExtensions - not sure why reflection was needed (using umbraco 7.1) like so: PetaPocoExtensions.CreateDatabaseSchema(db, false);

We’re struggling with collaboration and identity fields (ints)… I wish those were GUIDs as working on the same DB is just not an option for us.

Leave a Comment

Comments are manually moderated and added once reviewed.

© 2021 Darren Ferguson, Built with Gatsby.