Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Feature] Including IScript instances #266

@SaphuA

Description

@SaphuA

The only way to include an IScript instance I was able to find is by using the extension method WithScriptsAndCodeEmbeddedInAssembly. I needed a little more control and did not want to include the CodeEmbedded part, so I made my own provider. Perhaps this can be included in DbUp in some way?

internal class ScriptInstanceProvider : IScriptProvider
{
    private readonly IScript _script;

    /// <summary>
    /// Provider used to directly include an IScript instance during migrations
    /// </summary>
    /// <param name="script">The IScript instance to include</param>
    public ScriptInstanceProvider(IScript script)
    {
        _script = script ?? throw new ArgumentNullException(nameof(script));
    }

    public IEnumerable<SqlScript> GetScripts(IConnectionManager connectionManager)
    {
        return connectionManager.ExecuteCommandsWithManagedConnection(dbCommandFactory =>
            new SqlScript[] {
                new LazySqlScript(_script.GetType().FullName + ".cs", () => _script.ProvideScript(dbCommandFactory))
            }
        );
    }
}

It's really easy to use:

DeployChanges.To.SqlDatabase(connectionString)
    .WithScripts(new ScriptInstanceProvider(new MyScript()))
    .Build()
    .PerformUpgrade();

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions