MigrationNotFoundException: Migrations not found in assembly when I try down to concrete version for multiple migrations

Issue #2 resolved
Valeriy Abakumov created an issue

When I have multiple migrations then I get an error when try down-update to one of previous version:

MongoDBMigrations.MigrationNotFoundException: Migrations not found in assembly ***, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

For demonstrate this error I was added this simple test (see here https://bitbucket.org/abakumov-v/mongodbmigrations/src/62e33536a608530e8b154a31beaf171989f2f753/MongoDBMigrations.Test/SmokeTestsPositive.cs#lines-152):

[TestMethod]
        public void UpdateTo_TryDownVersionWithMultipleMigrationsButWithoutSchemaValidation_LatestVersionWasIncreased()
        {
            // Arrange
            var options = new MigrationRunnerOptions
            {
                ConnectionString = Const.TestDatabase.ConnectionString,
                DatabaseName = Const.TestDatabase.DatabaseName
            };

            var runner = new MigrationRunner(options);
            runner.Locator.LookInAssemblyOfType<_1_1_0_TestMigration>();
            runner.MigrationApplied += Handle;

            var version110 = new Version(1, 1, 0);
            var version111 = new Version(1, 1, 1);
            runner.UpdateTo(version110);
            runner.UpdateTo(version111);

            var expectedVersion = version110.ToString();
            // Act
            var result = runner.UpdateTo(version110);
            // Assert
            Assert.AreEqual(expectedVersion, result.TargetVersion.ToString());
        }

When I’m try to down version from 1.3.0 back to 1.1.0 then I’m get an error:

MongoDBMigrations.MigrationNotFoundException: Migrations not found in assembly MongoDBMigrations.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Line that raised exception is (MigrationLocator.cs method public IEnumerable<IMigration> GetMigrations(Version currentVersion, Version targetVersion)):

if (targetVersion != Version.V1() && migrations.Last().Version != targetVersion)
  throw new MigrationNotFoundException(_assembly.FullName, null);

P.S. Also failed test Database_Migrate_Simple_WithValidation_Success()

P.P.S. Failure of test Database_Migrate_Simple_WithValidation_Success is floating

Comments (8)

  1. Valeriy Abakumov reporter

    I think that is incorrect Where condition in this line:

                else if (targetVersion < currentVersion)
                {
                    migrations = migrations
                        .Where(x => x.Version <= currentVersion && x.Version > targetVersion)
                        .OrderByDescending(x => x.Version);
                }
    

    When I have 2 migrations:

    • 1_1_0_TestMigration
    • 1_2_0_TestMigration2

    and I want rollback from v120 to v110 then this condition return 1_2_0_TestMigration2 that is correct? I think that this condition must return first previous migration - 1_1_0_TestMigration

    To be honest, not really understand, why this checking is needed?

                if (targetVersion != Version.V1() && migrations.Last().Version != targetVersion)
                    throw new MigrationNotFoundException(_assembly.FullName, null);
    

  2. Log in to comment