Search code examples
c#.net-coredatabase-schemafluent-migrator

Fluentmigrator in database schema


I use Fluentmigrator in my Visual Studio project to version and migrate a single MS SQL Server database.

Within that db, there are multiple customer dedicated database schemas, and wish to migrate them independently from each other, hence each schema would have a dedicated VersionInfo table.

I see this might be possible with this, by overriding the meta data: https://fluentmigrator.github.io/articles/version-table-metadata.html

The next step would be to execute sql scripts against the target schema using

Execute.EmbeddedScript("...") 

statement in the migration class, but I don't see the InSchema member method.

(Unfortunately I need to use embedded scripts, as I have complex temporal table structure setups.)

How could I run Execute.EmbeddedScript against different schemas? The schema name is passed to my fluent migrator application via command line argument.


Solution

  • Glad its open source, found it in the code. EmbeddedScript function has a 2nd optional parameter which is a dictionary, where you can list tokens to be replaced in your to be executed sql code.

    This is where it got implemented, but since then it got improved https://github.com/fluentmigrator/fluentmigrator/issues/625 and now the token key has to be in $(mytokentoreplace) in the sql script, and just "mytokentoreplace" in your c# dictionary which you pass to Embeddedscript to replace the tokens to whatever you want.

    You can pass in your dictionary to your migration classes using dependency injection: https://fluentmigrator.github.io/articles/obsolete/app-context.html

    then just call Execute.EmbeddedScript("mysqlscript.sql", service.Tokens); to replace the tokens, which in my case is schema.