Running database migrations with FluentMigrator

INFO

This documentation is for FAKE.exe before version 5 (or the non-netcore version). The documentation needs to be updated, please help!

FluentMigrator is a .NET library which helps to version database schema using incremental migrations which are described in C#. The basic idea of the FAKE helper is to run FluentMigrator over the existing database using compiled assembly with migrations.

Migrating your database to the latest version

FAKE's support for FluentMigrator ships with a separate NuGet package called FAKE.FluentMigrator. Usually your FAKE setup will look as follows:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
// Reference required dlls
#r @"packages/FAKE/tools/FakeLib.dll"
#r @"packages/FAKE/tools/Fake.FluentMigrator.dll"

open Fake
open Fake.FluentMigratorHelper

// Assemblies with migrations
let assembly = "Migrations.dll"

// Using SQL Server 2014 LocalDB
let connection = 
  ConnectionString(
      @"Data Source=(localdb)\MSSQLLocalDb;Initial Catalog=MyDB;Integrated Security=True", 
      SqlServer(V2014))
      
// Specify additional options or just use the defaults
let options = {DefaultMigrationOptions with Profile="Staging"; Tags = ["US"; "Canada"]}

Target "Build" (fun _ ->
    // Build your Migrations.dll assembly using MSBuild or whatever
)

Target "MigrateDatabase" (fun _ ->
    MigrateToLatest connection [assembly] options
)

"Build" ==> "MigrateDatabase"

RunTargetOrDefault "MigrateDatabase"

ConnectionStrings

Specify connection string in build script:

1: 
let connection = ConnectionString("Server=.;Database=TestDb;User Id=admin;Password=pss;", SqlServer(V2008))

Use connection string from config file:

1: 
let connection = ConnectionStringFromConfig("ConnectionStringKey", "Project\\Web.config", SqlServer(V2012))

Providers / drivers / SQL dialects

The following drivers are supported:

  • SqlServer (SqlServerVersion.Default, V2000, V2005, V2008, V2012, V2014)
  • SqlServer (CE)
  • Oracle (OracleVersion.Default, Managed, DotConnect)
  • DB2
  • Firebird
  • HANA
  • Jet
  • MySql
  • PostgreSQL
  • SQLite

Available commands

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
// Migrate to the latest available version
MigrateToLatest connection assemblies options

// Migrate to the specified version
MigrateUp version connection assemblies options

// Rollback to the specified version
MigrateDown version connection assemblies options

// Rollback N migrations
Rollback N connection assemblies options

// Rollback N migrations
Rollback N connection assemblies options

// List applied migrations
ListAppliedMigrations connection assemblies

Advanced usage

For advanced usage see the source code.

val assembly : string
val connection : obj
val options : obj