Check if migration is needed instead of executing it

Issue #14 open
Former user created an issue

Hi,

I'd like to check if a migration is needed.

E. g. instead of

new MigrationEngine().....Run(...)

something like

new MigrationEngine().....IsMigrationNeeded(...)

I think, I could make this comparison somehow by myself, too. E. g.

var databaseManager = new DatabaseManager(...);
var lastAppliedVersion = databaseManager.GetLastAppliedMigration().Ver;

var migrationManager = new MigrationManager(...);
var allMigrations = migrationManager.GetAllMigrations(...);
var newestLocalVersion = migrationManager.GetNewestLocalVersion();

but MigrationManager is inaccessible due to its protection level (internal).

Comments (3)

  1. Arthur Osmokiesku repo owner

    Hi, thanks for this ticket, and sorry for this extremely late answer. This functionality is already in place, you can use different methods from class

    MongoDatabaseStateChecker.IsDatabaseOutdated(...)
    or
    MongoDatabaseStateChecker.ThrowIfDatabaseOutdated(...)
    

    Let me know if it solves your issue.

  2. Frank

    Hi, now it is up to me to apologize very much for my very extremely late answer.

    I just re-integrated MongoDBMigrations into my project and tried your suggestion. Unfortunately, I get an exception when executing

    var migrationIsRequired = MongoDatabaseStateChecker.IsDatabaseOutdated(connectionString, databaseName, _migrationAssembly);
    

    I replaced the NuGet package with the source code to find out more. The exception occures here (DatabaseManager.cs):

    I found a workaround for this: If I change the code to this, then it works:

    _ = new MigrationEngine()
                    .UseDatabase(connectionString, databaseName)
                    .UseAssembly(_migrationAssembly);
    
    var migrationIsRequired = MongoDatabaseStateChecker.IsDatabaseOutdated(connectionString, databaseName, _migrationAssembly);
    

    So it seems that this

    _ = new MigrationEngine()
                    .UseDatabase(connectionString, databaseName)
                    .UseAssembly(_migrationAssembly);
    

    has to be executed once before I use the other…?

    MongoDB driver version is 2.14.1 but it makes no difference when I update to 2.17.1.

    Hint: It is an ASP.net CORE 5 application. The application uses System.Text.Json for serializing/deserializing. At least this serializer is unable to handle your objects, because you are using fields everywhere instead of properties. For example, I’m unable to return the result from you “Run” method, because ASP.net is unable to serialize the fields to JSON (result is always “{}“…

    _migrations repository looks fine to me:

    /* 1 */
    {
        "_id" : "631c77233b0ee0fe91408248",
        "n" : "1. offizielle DB-Version.",
        "v" : "1.0.0",
        "d" : true,
        "applied" : ISODate("2022-09-10T11:38:11.544Z")
    }
    
    /* 2 */
    {
        "_id" : "631c77233b0ee0fe91408249",
        "n" : "Refresh-Sync-Option",
        "v" : "1.0.1",
        "d" : true,
        "applied" : ISODate("2022-09-10T11:38:11.627Z")
    }
    

    Other people seem to have similar problems:

    https://github.com/SRoddis/Mongo.Migration/issues/45

    https://www.mongodb.com/community/forums/t/problem-with-deserialization/7075/3

  3. Log in to comment