Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ssteiner opened this issue Aug 5, 2024 · 1 comment
Open

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

ssteiner opened this issue Aug 5, 2024 · 1 comment

Comments

@ssteiner
Copy link

ssteiner commented Aug 5, 2024

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;
@ssteiner
Copy link
Author

ssteiner commented Aug 5, 2024

so I managed to find the right approach (I hope). It seems IMongoMigration is the interface I want. Thus, running the migrations would entail

        using IHost host = builder.Build();
        var migration = host.Services.GetRequiredService<IMongoMigration>();
        migration.Run();

that works without having to expose any classes/interfaces. Still, the problem with the ConnectionString and ClientSettings persists when you're not reusing the MongoClient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant