Skip to content

amityagov/dotnet-sql-migrations

Repository files navigation

dotnet-sql-migrations

Experimental project

https://www.nuget.org/packages/DotnetMigrations8/

DotnetMigrations8

Nuget

.NET Core tool for running migrations from command line.

dotnet tool install --global DotnetMigrations8

DotnetMigrations8.Lib

Nuget

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();

DotnetMigrations8.Command

Nuget

Migration command for https://github.com/natemcmaster/CommandLineUtils.

See ApplyMigrationCommand.cs.