Maintenance migrations doesn't get executed when registering migrations via .ScanIn(typeof(Program).Assembly).For.Migrations()
See original GitHub issueDescribe 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:
- Created a year ago
- Comments:6
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
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.