39

I have an application where I use Fluent Nhibernate to create my database. This far I've been recreating the database schema each time. The code that does this is this:

public NhibernateSessionFactory(IPersistenceConfigurer config)
{
    _sessionFactory = Fluently.Configure().
        Database(config).
        Mappings(m => m.FluentMappings.AddFromAssemblyOf<MappingsPersistenceModel>()).
        ExposeConfiguration(BuildSchema).
        BuildSessionFactory();
}

private static void BuildSchema(Configuration config)
{
    // if (DbExists(config))
    //    return; 

    new SchemaExport(config).Create(false, true);
}

Note the "if (DbExists(config))". This is what I'd like to do. I'd like to create the schema only if it actually doesn't already exist. And in the next step - I'd like to update it to be created if it isn't up to date.

How do I achieve this? I am expecting a config.DatabaseExists(), but I can't see anything like this. I see some possibilities of a hacky solution, but what is the typical recommended way of handling this?

Keith Hoffman
  • 608
  • 5
  • 16
stiank81
  • 25,418
  • 43
  • 131
  • 202

2 Answers2

72

You can just use SchemaUpdate instead, it will update the schema if it exists and create it if it does not:

public NhibernateSessionFactory(IPersistenceConfigurer config)
{
    _sessionFactory = Fluently.Configure().
        Database(config).
        Mappings(m => m.FluentMappings.AddFromAssemblyOf<MappingsPersistenceModel>()).
        ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true)).
        BuildSessionFactory();
}

One caveat: SchemaUpdate does not do destructive updates (dropping tables, columns, etc.). It will only add them.

Gabe Moothart
  • 31,211
  • 14
  • 77
  • 99
10

UPDATED (thanks dotjoe)

Hbm2ddl is only capable of doing a schema diff and only updating what has changed with the SchemaUpdate class. However this class is pretty rudimentary in that it only takes a look at the current entities and how the schema differs. If significant changes have been made (ie entities removed or link tables removed) it won't be able to figure that out.

On an earlier project we used hbm2ddl, however we've since moved onto use Fluent Migrator. I would say your best bet is to use a migration tool, like Fluent Migrator or Migrator.NET.

http://github.com/schambers/fluentmigrator/

http://code.google.com/p/migratordotnet/

Vadim
  • 17,897
  • 4
  • 38
  • 62
  • actually Hbm2ddl can generate alter scripts with `SchemaUpdate`...but, it doesn't catch everything and I'd never rely on it. – dotjoe May 04 '11 at 14:42
  • @dotjoe are you just referring to the fact that it doesn't do destructive updates? I use SchemaUpdate in production and have never had a problem with it not catching something. – Gabe Moothart May 04 '11 at 14:53
  • @Gabe Moothart it won't catch changes like column null-ability, but I guess that would be considered a destructive change if moving from null column to non-null column. – dotjoe May 04 '11 at 16:03
  • Thanx! Will look into this further down the road, but for now SchemaUpdate was what I was looking for. – stiank81 May 05 '11 at 07:27