Search code examples
c#fluent-nhibernatedatabase-migration

FluentNHibernate Database Deployment


I'm looking into using FluentNHibernate as my ORM but I'm wondering how best to write the database deployment section. I've prior experience with Entity Framework and code first migrations, but I'm curious about FluentNHibernate so I'm playing around. I've looked at FunnelWeb Blog but I haven't got the time, yet, to really get to grips with how they're doing it, so I'm after some help with a very specific part of the project, which I hope someone can summarise.

My Visual Studio solution is separated in the following assemblies (I'm going to look into using an IoC like Autofac, but that will come later):

Solution
    Domain Assembly
        DatabaseDeployment Namespace
        Model Namespace
    Web UI

So, in my domain assembly I'd like to have a DBMigrator class who's contract will look something like this:

public interface IDBMigrator
{
    bool NeedsUpdating();
    void UpdateDatabase();
}

Now, in my WebUI assembly (which is an MVC3 project) I would like to do something like the following (in the global.asax):

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    var migrator = new DBMigrator();
    if (migrator.NeedsUpdating())
    {
        migrator.UpdateDatabase();
    }
}

This migrator will using .sql scripts (so I can have my database version controlled by Subversion) and likely have a table in the database that tells me what scripts have been executed against the schema, and what scripts haven't yet. So, to summarise I suppose my actual questions are:

  1. Am I on the right track to version controlling (and automating) my database deployment?
  2. At what point should I be checking for (and applying) schema changes to my project, should this really be automated or developer instigated?

I'm looking at some frameworks now such as DbUp and MigratorDotNet but I'd like to get my strategy right in my head first, before I go and adopt a framework for this. How do you handle your database migrations?


Solution

  • I couldn't find a specific tool that did what I wanted, so have made my own following the convention in my question. If anyone else comes across this and wants to use it, they can grab it from here:

    NuGet Package Manager link

    User Guide