Skip to content

Dealing with Multiple Database Types

iceclow edited this page Aug 25, 2011 · 3 revisions

FluentMigrator allows you to target multiple database types from the same migration project. Every FluentMigrator project is database agnostic and can be run against any of the supported database types.

However, there are times when only some migrations in a project need to be executed against one of the database types. For supporting that scenario FluentMigrator includes the IfDatabaseType expression.

Using IfDatabaseType

Supose you have a migration that executes a script file to create a view:

public class CreateViewsMigration : Migration
{
    public override void Up()
    {
        Execute.Script("CreateViewsMigrationUp.sql");
    }

    public override void Down()
    {
        Execute.Script("CreateViewsMigrationDown.sql");
    }
}

However the project needs to create some views in an SqlServer database and others in an Oracle database, but you want both tasks to be part of the same migration, sharing the same migration number in both databases. You handle this by creating scripts for each database and specifying wich one needs to be executed:

public class CreateViewsMigration : Migration
{
     public override void Up()
     {
        IfDatabaseType("oracle").Execute.Script("CreateViewsOracleMigrationUp.sql");
        IfDatabaseType("sqlserver").Execute.Script("CreateViewsSqlServerMigrationUp.sql");
     }

     public override void Down()
     {
         IfDatabaseType("oracle").Execute.Script("CreateViewsOracleMigrationDown.sql");
         IfDatabaseType("sqlserver").Execute.Script("CreateViewsSqlServerMigrationDown.sql");
     }
}
Clone this wiki locally