-
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.
- 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
.
First, you'll need a migration controller class that kicks off your migration scripts.
/* Copyright Xeno Innovations, Inc. 2019
* Date: 2019-9-15
* Author: Damian Suess
* File: LocalMigrations.cs
* Description:
* Local helpers for LiteMigrator Engine
*/
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()
{
// Namespace location to our 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);
}
/// <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!