Created
July 2, 2010 15:48
-
-
Save andreabalducci/461541 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment