Search code examples
c#database-migrationfluent-migrator

FluentMigrator generate SQL Output from In-Process Migration runner


Is there a way to generate SQL file output using the In-Process Migration Runner similarly to what can be achieved using -output command argument from the dotnet-fm tool.

I couldn't find any property, field or method in the IMigrationRunner, IMigrationRunnerBuilder or in the IMigrationProcessorOptions that would set configure the output.

Am I missing something?


Solution

  • https://fluentmigrator.github.io/api/v3.x/FluentMigrator.Runner.Logging.FluentMigratorConsoleLoggerProvider.html This page explains how to use a logging provider in fluent migrator.

    https://fluentmigrator.github.io/api/v3.x/FluentMigrator.Runner.Logging.LogFileFluentMigratorLoggerOptions.html Here you can go over the different options, such as writing the logs to SQL database or to a file, or just showing the SQL in the console (if you use a consoleloggingprovider).

    Here is how to do it:

    services
        .AddSingleton<ILoggerProvider, LogFileFluentMigratorLoggerProvider>()
        .Configure<LogFileFluentMigratorLoggerOptions>(
            opt =>
            {
                opt.OutputFileName = options.OutputFileName;
                opt.OutputGoBetweenStatements = targetIsSqlServer;
                opt.ShowSql = true;
            });
    

    You could also write an extension method and a decorator to add custom logging providers, in case you want to log it somewhere else than the console. but I believe fluentMigrator already supports this out of the box.