1
votes

I work with two databases and want to use fluent migrator for it. They're two SQL Databases, which can be on different servers and have different connectionstrings. They have different tables and data. But I have only one Startup and one project. I want to specify on every Migration the used database. How can I manage this? I'm currently trying to solve it with attributes, but I'm not sure if it works somehow:

public class MyAttribute : Attribute
{
    public MyAttribute(string name)
    {
        Name = name;
    }

    public string Name;
}

[MyAttribute("Database1")]
[Migration(1)]
public class FooScript : Migration
{
    public override void Down()
    {
        Delete.Table("Foo");
    }

    public override void Up()
    {
        Create.Table("Foo")
            .WithColumn("id").AsInt16().PrimaryKey()
            .WithColumn("Body").AsString(4000).NotNullable();
    }
}

And in the startup:

        services.AddFluentMigratorCore()
            .ConfigureRunner(rb => rb
                .AddSqlServer()
                .WithGlobalConnectionString("")
                .ScanIn(typeof(database1).Assembly, typeof(database1).Assembly).For.Migrations())
            .Configure<ProcessorOptions>(x =>
            {
                var connection = typeof(FooScript).GetCustomAttributes(typeof(MyAttribute), true);
                var name = "0"; //here I would need the current executing Migration class
                switch (name)
                {
                    case "Database1":
                        x.ConnectionString = Configuration.GetConnectionString("Database1");
                        break;
                    case "Database1":
                        x.ConnectionString = Configuration.GetConnectionString("Database1");
                        break;
                }
            })
            .BuildServiceProvider();

But it doesn't work. I would need the attribute of the current script to overwrite the database. But proucessorOptions seems not to have those. Is there another solution or another configuration I could overwrite? Any other ideas how I can run fluentmigration with two or more different sql databases?

Theoretically I would end up with a VersionInfo for every database and the current migration for those database. Hopefully that works.

1

1 Answers

2
votes

Well sometimes it's easier than you think. For everyone with the same question:

startup.cs

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
    {
        //...
        Migration();
    }

    private void Migration()
    {
        var serviceProviderDb1 = new ServiceCollection()
        .AddFluentMigratorCore()
        .ConfigureRunner(rb => rb
            .AddSqlServer()
            .WithGlobalConnectionString(Configuration.GetConnectionString("Database1"))
            .ScanIn(typeof(DB1Context).Assembly).For.Migrations())
        .Configure<RunnerOptions>(opt => {
            opt.Tags = new[] { "Database1" };
        })
        .BuildServiceProvider(false);

        using (var scope = serviceProviderDb1.CreateScope())
        {
            var runner = serviceProviderDb1.GetRequiredService<IMigrationRunner>();
            runner.MigrateUp();
        }

        var serviceProviderDb2 = new ServiceCollection()
            .AddFluentMigratorCore()
            .ConfigureRunner(rb => rb
                .AddSqlServer()
                .WithGlobalConnectionString(Configuration.GetConnectionString("Database2"))
                .ScanIn(typeof(DB2Context).Assembly).For.Migrations())
                .Configure<RunnerOptions>(opt => {
                    opt.Tags = new[] { "Database2" };
                })
            .BuildServiceProvider(false);

        using (var scope = serviceProviderDb2.CreateScope())
        {
            var runner = serviceProviderDb2.GetRequiredService<IMigrationRunner>();
            runner.MigrateUp();
        }
    }

And in the migrationfile you only need:

[Tags("Database1")]
[Migration(1)]
public class FooScript : Migration
{
}

More infos here: https://fluentmigrator.github.io/articles/migration/migration-filter-tags.html?tabs=runner-internal

The only bad thing is, you don't add the FluentMigration to your ServiceCollection and can't use it with dependency injection later. But well, mostly you only run the scripts on startup, so it shouldn't be a problem