Maintenance migrations

Place ordered work at the runner's lifecycle stages.

Examples use Classic and Fluent tabs. Your choice follows you through the manual.

Choose a stage

StageWhen
BeforeRunBefore versioned migration work in this run.
BeforeMigrationBefore each executed versioned migration.
AfterMigrationAfter each executed versioned migration.
AfterRunAfter the run's migration/profile work.

Maintenance classes accept Order and Scope. They use Up and do not acquire version records. Hooks stop on failure; later stages are not finally blocks or guaranteed cleanup paths. Lock release and connection/transaction restoration are runner responsibilities.

A scoped maintenance operation

The example expects an existing DeploymentLog table. Choose a table that already exists at the selected stage. Fluent callbacks execute at the corresponding operation position.

Write a deployment marker

Classic

using System;
using System.Data;
using DotNetProjects.Migrator;
using DotNetProjects.Migrator.Framework;

[Maintenance(MaintenanceStage.AfterRun, Order = 10)]
public class RecordDeployment : Migration
{
    public override void Up()
        => Database.Insert("DeploymentLog", new[] { "Message" }, new object[] { "Migration run finished" });
    public override void Down() { }
}

Fluent

using System;
using System.Data;
using DotNetProjects.Migrator;
using DotNetProjects.Migrator.Framework;
using DotNetProjects.Migrator.Framework.Fluent;

[Maintenance(MaintenanceStage.AfterRun, Order = 10)]
public class RecordDeployment : FluentMigration
{
    public override void BuildUp(MigrationBuilder migration)
        => migration.Insert.IntoTable("DeploymentLog")
            .Row(new[] { "Message" }, new object[] { "Migration run finished" });
    public override void BuildDown(MigrationBuilder migration) { }
}

Choose one authoring style

Post-commit callbacks

Migration.AfterUp and AfterDown run after commit, with the migration context restored. In WholeSession they wait until the entire session commits. Their failure reports an error after durable changes; it cannot reverse that commit. Do not confuse maintenance stages with a guaranteed post-commit delivery system.