Search code examples
c#fluent-migrator

FluentMigrator Versioning Refresh Views


I am using Fluent Migrator for keeping the database updated. The Up and Down functions works perfectly. Next step is that I want views to be created. These I would like to run from an .SQL file which i have. I want this to be run after all migrations has been run, everytime.

What i have currently is:

var blah = new MigrationConventions();
var maintenanceLoader = new MaintenanceLoader(_migrate, blah);
maintenanceLoader.ApplyMaintenance(MigrationStage.AfterAll);

and a class

[Maintenance(MigrationStage.AfterAll)]
public class ViewMaintenance
{
    public ViewMaintenance() {
        var blah = 123;
    }
}

This is not fired because in maintenanceLoader there are 0 elements that it can find. I am inserting the _migrate, which is defined like this:

var runnerContext = new RunnerContext(new TextWriterAnnouncer(UpdateText));
_migrate = new MigrationRunner(
 Assembly.GetExecutingAssembly(), 
 runnerContext,
  new SqlServerProcessor(
  new SqlConnection(connectionString), 
  new SqlServer2012Generator(), 
  new TextWriterAnnouncer(UpdateText), 
  new ProcessorOptions(), 
  new SqlServerDbFactory()));

Why can't the Assembly.GetExecutingAssembly() be scanned, and find the [Maintenance(MigrationStage.AfterAll)] be found?

I would also like for the ViewMaintenance class to be able to run the Execute.Sql( that the Migration classes has.


Solution

  • I downloaded the source code and figured it out.

    In the class I want the maintenance to be run, I need to inherit from the : Migration class, just like with migrations (duh..). It will then have access to everything it has access to in migrations, including Execute.Sql(.

    When it is inherited from, the Reflection in Fluent Migrator will search for it, find it, and use the attribute that is set to run it after all migrations is run.

    This part is not needed:

    var blah = new MigrationConventions();
    var maintenanceLoader = new MaintenanceLoader(_migrate, blah);
    maintenanceLoader.ApplyMaintenance(MigrationStage.AfterAll);
    

    Neat :)