erikbra / grate

grate - the SQL scripts migration runner
MIT License
210 stars 40 forks source link

Programatically provide new connection string and Migrate on demand #602

Open OzBob opened 1 week ago

OzBob commented 1 week ago

Following on from closed issue #384

Provide a Http API endpoint that retrieves the connection string from Keyvault and then runs grateMigrator.Migrate();

All my clients use Azure SQLServer DBs, could the connection string be updated in the IGrateMigrator service? or can the service be built on demand. The client provides the connection string and my intention is to run Grate against their DB.

The code sample in https://www.nuget.org/packages/grate.sqlserver#readme-body-tab

Shows a hard coded connection string in to the IGrateMigrator service but I would like to get the connection string just before calling Migrate()

 serviceCollection.AddGrate(builder =>
    {
        builder
            .WithSqlFilesDirectory("/db")
            .WithConnectionString("sqlserver connection string here")
    })
    .UseSqlServer(); // Important!, you need to specify the database type to use.
    var serviceProvider = serviceCollection.BuildServiceProvider();
    var grateMigrator = serviceProvider.GetRequiredService<IGrateMigrator>();
hoangthanh28 commented 3 days ago

Just to clarify, you want to do something like this?

var grateMigrator = serviceProvider.GetRequiredService<IGrateMigrator>();
grateMigrator.Builder.WithConnectionString("new-connection-string"); // override the connection string
await grateMigrator.Migrate();