Experimental project
.NET Core tool for running migrations from command line.
dotnet tool install --global DotnetMigrations8
Library for running migrations.
Startup.cs
public override void ConfigureServices(IServiceCollection collection)
{
collection.AddMigrations((provider, options) =>
{
var configuration = provider.GetRequiredService<IConfiguration>();
var environment = provider.GetRequiredService<IHostEnvironment>();
var connectionString = configuration.GetConnectionString("Default");
options.DryRun = false; // do not commit transaction after migrations applied, default = false
options.Pattern = "*.sql"; // default = *.sql
// Supported providers
options.ProviderType = Providers.Npgsql; // default
// options.ProviderType = Providers.SqlServer;
// options.ProviderType = Providers.Sqlite;
options.ConnectionString = connectionString;
options.FileProviders = new IFileProvider[]
{
new PhysicalFileProvider(Path.Combine(environment.ContentRootPath, "sql")), // folder
new EmbeddedFileProvider(GetType().Assembly, "SqlResourcesNamespace") // embedded sql files
};
});
...
}
Program.cs
var host = hostBuilder.Build();
var migrationRunner = host.Services.GetRequiredService<IMigrationRunner>(); // Get service...
await migrationRunner.ExecuteAsync(CancellationToken.None); // ...and run migrations
await host.RunAsync();
Migration command for https://github.com/natemcmaster/CommandLineUtils.