This module is used to manage updates via update classes instead of stuffing too much code into your Updater.cs class. Each update should only be executed once per database making deployment updates really easy.
Example: Your client has your software installed at their location. In your most recent update, you made a change to the database that requires a complex script to correct. Rather than run the script on the database manually, you want to have the Updater.cs do this for you. The problem is, you only want this script to run once per database. This is where the Managed Update Script Module comes into play.
WarningYou should have a deployment strategy in place that takes a backup of the database before you deploy new versions
Getting Started
Install the LlamachantFramework.UpdateScripts.Module from NuGet into your solution
Include the LlamachantFrameworkUpdateScriptsModule in the RequiredModuleTypes collection of your .Module project
In your projects, add a new class that implements the IUpdateScript interface
Implement the IUpdateScriptProvider interface to your ModuleBase classes
Debugging NoticeThe updates will run every time the database is updated. If you want to prevent this from happening while debugging, set the UpdateScriptManager.Instance.UpdateWhenDebugging property to false.
public sealed partial class TestApplicationModule : ModuleBase, IUpdateScriptProvider {
public LlamaFrameworkTestModule() {
InitializeComponent();
this.RequiredModuleTypes
.Add(typeof(LlamachantFramework.UpdateScripts.Module.LlamachantFrameworkUpdateScriptsModule));
}
public IList GetPreUpdateScripts()
{
//This will automatically collect all IUpdateScripts in the current assembly
return UpdateScriptManager.Instance
.FindUpdateScriptsInAssembly(Assembly.GetExecutingAssembly(), IUpdateScriptType.PreUpdateScript);
}
public IList GetPostUpdateScripts()
{
//This approach lets you determine which IUpdateScripts to release
//return new List() { new MyOneTimeUpdate() };
//This will automatically collect all IUpdateScripts in the current assembly
return UpdateScriptManager.Instance
.FindUpdateScriptsInAssembly(Assembly.GetExecutingAssembly(), IUpdateScriptType.PostUpdateScript);
}
public override IEnumerable GetModuleUpdaters(IObjectSpace objectSpace, Version versionFromDB)
{
ModuleUpdater updater = new DatabaseUpdate.Updater(objectSpace, versionFromDB);
return new[]
{
updater,
new LlamachantFramework.UpdateScripts.Module.DatabaseUpdate.Updater(objectSpace, versionFromDB)
};
}
}
//Now you can create as many of these types of classes as you need
public class MyOneTimeUpdate : IUpdateScript
{
public string UpdateID => "6d7eb7fa-c283-489a-904b-13673899dda7"; //This must be unique
public string Description => "Perform a one time update";
public DateTime CreatedDate => DateTime.Parse("January 1, 2022");
public IUpdateScriptType ScriptType => IUpdateScriptType.PostUpdateScript;
public string Run(IObjectSpace space, Version databaseVersion)
{
try
{
//try to perform your update here using the IObjectSpace
return "Success";
}
catch (Exception ex)
{
return ex.Message;
}
}
}
Created Date NoticeThe update script manager determines the most recent update that has been performed on the database and only executes IUpdateScripts that have a CreatedDate that is greater than that date. The CreatedDate property is also used to determine which order in which to execute updates.