NHibernate has a feature named “Schema Update”. This feature help updating schema of existing database based on new changes in mapping files. Schema update do not change current data, just changes schema (table, view, … structure) in an additive manner. Castle ActiveRecord exposes this feature too.
The problem is with NHibernate you have choice for seeing update scripts first and if you were satisfied then do actual update. But in Castle ActiveRecord you must do actual update without any preview of possible changes. This is all because ActiveRecordStarter.UpdateSchema() does not emit NHibernate.SchemaUpdate.Execute’s parameters “scriptAction” and “doUpdate” to the user. It calls SchemaUpdate as follow:
updater.Execute(false, true);
As I needed schema update in Castle ActiveRecord with preview update scripts option, I wrote a method for it:
private static IList UpdateSchema(Actionaction, bool doUpdate)
{
CheckInitialized();
ArrayList exceptions = new ArrayList();
foreach (Configuration config in ActiveRecordMediator.GetSessionFactoryHolder().GetAllConfigurations())
{
SchemaUpdate updater = CreateSchemaUpdate(config);
try
{
updater.Execute(action, doUpdate);
exceptions.AddRange((IList)updater.Exceptions);
}
catch (Exception ex)
{
throw new ActiveRecordException("Could not update the schema", ex);
}
}
return exceptions;
}
private static void CheckInitialized()
{
if (!ActiveRecordStarter.IsInitialized)
{
throw new ActiveRecordException("Framework must be Initialized first.");
}
}
private static SchemaUpdate CreateSchemaUpdate(Configuration cfg)
{
return new SchemaUpdate(cfg);
}
Hope Castle ActiveRecord add an overload to ActiveRecordStarter.UpdateSchema that let seeing update schema scripts before actually doing the update.
Comments
Hi Afshar
you an code example about this
@Joil: Consider this:
public static List GenerateScriptForUpdateSchema()
{
List scripts = new List();
UpdateSchema(scripts.Add, false);
return scripts;
}