Search code examples
c#.net-coreservicestackormlite-servicestack

Changing the model of the table Migration in the migrator process


Good Day everyone,

I make a process to run migrations files. but for consistency reasons, I need to rename the table "Migration" for "migration" and the columns too, not using the uppercase.

Is possible to assign an Alias name to the Migration table and columns?

This is my process:

public void Migrations(IDbConnectionFactory dbFactory){
   Logger.Info("---------------- Start Migrations Items ------------------");
   var migrator = new Migrator(dbFactory, typeof(Migration0000).Assembly);
   var result = migrator.Run();
   Logger.Info($@"Succeeded: {result.Succeeded}");
   Logger.Info($@"TasksRun: {result.TasksRun.ToList().Select(x => x.GetType()).ToList().Select(y => y.Name).ToJsv()}");
   Logger.Info($@"TypesCompleted: {result.TypesCompleted.Select(x => x.Name).ToJsv()}");
   Logger.Info("---------------- Finish Run Migrations -------------------");
}

Solution

  • As Migration is a built-in table you wont be able to add declarative attributes to the type but you should be able to dynamically Add attributes by adding them before using the Migration table with OrmLite, e.g:

    var type = typeof(Migration)
        .AddAttributes(new AliasAttribute("migration"));
    type.GetProperty(nameof(Migration.Id))
        .AddAttributes(new AliasAttribute("id"));
    type.GetProperty(nameof(Migration.Name))
        .AddAttributes(new AliasAttribute("name"));
    
    //...
    var migrator = new Migrator(dbFactory, typeof(Migration0000).Assembly);
    var result = migrator.Run();