question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Maintenance migrations doesn't get executed when registering migrations via .ScanIn(typeof(Program).Assembly).For.Migrations()

See original GitHub issue

Describe the bug

Maintenance migrations don’t seem to be executed.

To Reproduce I have a maintenance migration defined as

[Maintenance(MigrationStage.BeforeAll, TransactionBehavior.None)]
[UsedImplicitly]
public class EnsureMigrationsCannotBeRunSimultaneouslyByMultipleInstances : Migration
{
    public override void Down() => throw new NotImplementedException("Down migrations are not supported for sp_getapplock");

    public override void Up()
    {
        throw new Exception("x");
        Console.WriteLine("START!!!!!!!!!!!!!!!!!!!!!!!!!!");
        Execute.Sql($@"
            DECLARE @result INT
            EXEC @result = sp_getapplock '{GetType().Namespace}', 'Exclusive', 'Session'

            IF @result < 0
            BEGIN
                DECLARE @msg NVARCHAR(1000) = 'Received error code ' + CAST(@result AS VARCHAR(10)) + ' from sp_getapplock during migrations';
                THROW 99999, @msg, 1;
            END
        ");
    }
}

Note that I throw an exception in Up().

When I run the migrations in a unit-test, they all run through just fine and I never hit the exception. In fact, all the structure described in my migrations is created happily.

So I am under the impression this specific ‘before-all’ migration is never executed.

Expected behavior The migration is executed and thus the exception thrown.

Information (please complete the following information):

  • OS: Windows 11
  • Platform: Net6
  • FluentMigrator version: 3.3.1
  • FluentMigrator runner: “in-process runner”
  • Database Management System SQLServer (docker image mcr.microsoft.com/mssql/server:2019-CU5-ubuntu-18.04)
  • Database Management System Version: see the answer to the last question, sqlserver 2019-CU5

Additional context Basically, I’ve been following the guidance from here to prevent multiple service instances running migrations at the same time.

However, I just got round to check whether this actually works and it seems these special maintenance migrations are never called.

I tried to step into the code of FluentMigrator, as much as VS and the nugets let me, and in MigrationRunner.MigrateUp() where it calls ApplyMaintenance(MigrationStage.BeforeAll, useAutomaticTransactionManagement); which leads to MaintenanceLoader.LoadMaintenance - and in there _maintenace is empty. Dunno if that helps.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
jzabroskicommented, Jul 15, 2022

I added a comment and updated the sample to help avoid this booby trap until we have time to do a proper fix. https://github.com/fluentmigrator/fluentmigrator/commit/d0c4c362138bd4bb60cdc0d317236bcf2d54ded5

1reaction
jzabroskicommented, Jun 6, 2022

See: https://github.com/fluentmigrator/fluentmigrator/issues/1062#issuecomment-616598419 - I think this is actually the heart of it.

Nobody ever did a PR to fix this/smooth it over, but we did get to the bottom of it / provide workarounds.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MigrationStage.AfterAll doesn't get executed when ...
MigrationStage.AfterAll doesn't get executed when registering migrations via .ScanIn(typeof(Program).Assembly).For.Migrations() #1062.
Read more >
Add migration with different assembly
The following class must be present in the project. Note there the migration assembly name. //var migrationAssembly = typeof(SqlServerContext). ...
Read more >
Using a Separate Migrations Project - EF Core
Using a separate migration project for managing database schemas with Entity Framework Core.
Read more >
Migrations and Seed Data With Entity Framework Core
We are going to learn about Migrations and Seed data in Entity ... The Up() method consists of commands that will be executed...
Read more >
Upgrade Guide from 3.0 to 3.1 | FluentMigrator ...
Migrations ());. The interesting part here is the new ScanIn function which allows one to specify which assemblies should be scanned for the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found