Skip to content

Instantly share code, notes, and snippets.

@andreabalducci
Created July 2, 2010 15:48
Show Gist options
  • Save andreabalducci/461541 to your computer and use it in GitHub Desktop.
Save andreabalducci/461541 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.Core.Logging;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
namespace Lucilla.Framework.Core.Data
{
public class DatabaseManager : IDatabaseManager
{
private ILogger _logger;
private NHibernate.Cfg.Configuration _configuration;
public DatabaseManager(Configuration configuration, ILogger logger)
{
_configuration = configuration;
_logger = logger;
}
public void CheckAndUpgrade()
{
try
{
if (ShouldUpdateSchema())
UpdateSchema();
}
catch (Exception ex)
{
_logger.Fatal("Schema upgrade", ex);
throw;
}
}
public void CreateSchema()
{
SchemaExport create = new SchemaExport(_configuration);
create.Execute(false, true,false);
}
public void DropSchema()
{
SchemaExport create = new SchemaExport(_configuration);
create.Execute(false, true, true);
}
public void CheckSchema()
{
SchemaValidator validator = new SchemaValidator(_configuration);
validator.Validate();
}
private void UpdateSchema()
{
SchemaUpdate update = new SchemaUpdate(_configuration);
update.Execute(false, true);
}
public bool ShouldUpdateSchema()
{
try
{
SchemaValidator validator = new SchemaValidator(_configuration);
validator.Validate();
return false;
}
catch (HibernateException)
{
return true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment