-
Notifications
You must be signed in to change notification settings - Fork 1
Using LiteMigrator
Damian edited this page Aug 9, 2020
·
2 revisions
This guide will step you through adding LiteMigrator to your project from scratch and how to add additional migration scripts. Remember, your scripts are only executed one time. Once they're registered in your database, the system will never execute the script again.
Both basic and advanced sample provide the same basic features
- Creating the migration controller
- Trigger your migration scripts
- Creating a migration script
We recommend you place your scripts in a subfolder below your migration controller class. In this example, we'll assume your scripts are placed in the subfolder named, Scripts
.
- Add LiteMigrator project to your solution
- Create a folder in your solution to hold the scripts
- Add SQL files as Embedded Resources
- You must use the naming convention, "YYYYMMDDhhmm-FileName.sql"
- Wire-up the controller
public async Task InstallMigrationsAsync()
{
// Your EXE/DLL with the scripts
var resourceAssm = Assembly.GetExecutingAssembly();
var dbPath = @"C:\TEMP\MyDatabase.db3";
var migsNamespace = "MyProjNamespace.Scripts";
var liteMig = new LiteMigration(dbPath, resourceAssm, migsNamespace);
bool = success = await liteMig.MigrateUpAsync();
}
First, you'll need a migration controller class that kicks off your migration scripts.
using System;
using System.IO;
using System.Threading.Tasks;
using Xeno.LiteMigrator;
namespace Xeno.Pos.Client.Business.Migrations
{
public class LocalMigrations
{
private const string DatabaseName = "MY_LOCAL_DATABASE.db3";
private LiteMigration _liteMigrator;
public LocalMigrations()
{
// Current namespace location with the subfolder, Scripts
var scriptLocation = GetType().Namespace + ".Scripts";
// Path to database file
var databasePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
DatabaseName);
_liteMigrator = new LiteMigration(databasePath, scriptLocation, DatabaseType.SQLite, Assembly.GetExecutingAssembly().Location);
}
/// <summary>Execute all unexecuted migration scripts.</summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task<bool> MigrateUp()
{
return await _liteMigrator.MigrateUpAsync();
}
}
}
private async Task LoadDatabaseMigrations()
{
var mig = new LocalMigrations();
await mig.MigrateUp();
}
Scripts MUST follow the naming format of YYYYMMDDhhmm-<NameOfScript>.sql
. As an example, 201909150000-BaseDDL.sql
(2019-09-15 12:00 AM)
- Add a new file to your project
- Set file as an Embedded Resource
- Done!