Skip to content

minimal setup to run migrations at startup - setup withouth  #86

@ssteiner

Description

@ssteiner

I'm building a proof of concept project in .NET 8.

    MongoMigrationSettings options = new()
    {
        ConnectionString = configuration.GetValue<string>("MongoDbConnectionString"),
        Database = configuration.GetValue<string>("DatabaseName"), 
        DatabaseMigrationVersion = new Mongo.Migration.Documents.DocumentVersion("1.0.0")
    };
    HostApplicationBuilder builder = Host.CreateApplicationBuilder();
    builder.Services.AddMigration(options);
    using IHost host = builder.Build();
    host.Run();

When I run the app, nothing happens. No MigrationService gets instantiated and nothing runs. So what's the trigger to get this to work? I do have a bunch of public classes that derive from DatabaseMigration in my namespace (MyNamespace.Migrations to be precise). I do not have any Tags (CollectionLocation, StartupVersion, RuntimeVersion) in my model yet, since I only run collection level updates.

I then build the lib from the source, exposing MigrationService as public, then instantiating an IMigrationService manually after building the host:

var migrator = host.Services.GetRequiredService<IMigrationService>();

Now I get things rolling, but it's not really the way it's meant to be used, is it? Plus, this then throws a MongoMigrationNoMongoClientException in the StartUpDocumentMigrationRunner constructor since settings.ClientSettings is null. And looking at the code, either the documentation is wrong and clientSettings is mandatory when not re-using the IMongoClient, or the code is wrong (imho):

if (settings.ConnectionString == null && settings.Database == null || settings.ClientSettings == null)
{
    throw new MongoMigrationNoMongoClientException();
}

if (settings.ClientSettings != null)
{
    this._client = new MongoClient(settings.ClientSettings);
}
else
{
    this._client = new MongoClient(settings.ConnectionString);
}

this._databaseName = settings.Database;

Shouldn't it be 'if there's no Database => throw, if there's no ConnectionString and no ClientSettings => throw. If there's either a ConnectionString or ClientSettings, use it to instantiate the new MongoClient, and thus the code should look like:

if ((settings.ConnectionString == null && settings.ClientSettings == null) && settings.Database == null)
{
    throw new MongoMigrationNoMongoClientException();
}

if (settings.ClientSettings != null)
{
    this._client = new MongoClient(settings.ClientSettings);
}
else
{
    this._client = new MongoClient(settings.ConnectionString);
}

this._databaseName = settings.Database;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions