26

Are there any tutorials or example code for executing FluentMigrator migrations from within code? Some "Getting Started..." tutorial would be just awesome. All I was able to find was FluentMigrator.Tests (unit tests), inside FluentMigrator source, which are not as helpful as "Getting Started..." would be.

I just want to add few classes to the project and run the migrations from that project, with no external tools. Is it possible in Fluent Migrator? Something like

FluentMigrator.Migrate("database path", typeof(Migration024));

which I would call from Program.Main()?

Servy
  • 202,030
  • 26
  • 332
  • 449
Paya
  • 5,124
  • 4
  • 45
  • 71
  • For those looking and using FluentMigrator 3.0, you can migrate via code by following their example found on their github docs (https://fluentmigrator.github.io/articles/quickstart.html?tabs=runner-in-process). – Kody May 10 '18 at 04:52

5 Answers5

19

One of the original authors of FluentMigrator just wrote this "Getting started" blogpost.

Christian Specht
  • 35,843
  • 15
  • 128
  • 182
16

I cribbed this from their source code...

using (IAnnouncer announcer = new TextWriterAnnouncer(Console.Out))
{
   IRunnerContext migrationContext = new RunnerContext(announcer) 
   { 
      Connection = "Data Source=test.db;Version=3", 
      Database = "sqlite", 
      Target = "migrations" 
   };

   TaskExecutor executor = new TaskExecutor(migrationContext);
   executor.Execute();
}

I use code similar to this in a custom action class in WiX. Target is the name of the assembly you want to execute. In your case, it would be whatever assembly is produced by your migration project. There are other options on the IRunnerContext you can set. Like Namespace, PreviewOnly, etc. Unfortunately, it isn't documented so you'll have to dig into the code to figure it out. The project that generates the Migrate.exe assembly is where I found most of this.

Justin Rudd
  • 5,334
  • 4
  • 22
  • 16
9

Here's an example of doing it in C# (rather than MSBuild, Nant or the console runner), based on scraps on Stackoverflow:

static void Main(string[] args)
{
    string connectionString = @"server=.\SQLEXPRESS;database=testdb;uid=sa2;pwd=Passw0rd";
    Announcer announcer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s));
    announcer.ShowSql = true;

    Assembly assembly = Assembly.GetExecutingAssembly();
    IRunnerContext migrationContext = new RunnerContext(announcer);

    var options = new ProcessorOptions 
    { 
        PreviewOnly = false,  // set to true to see the SQL
        Timeout = 60 
    };
    var factory = new SqlServer2008ProcessorFactory();
    using (IMigrationProcessor processor = factory.Create(connectionString, announcer, options))
    {
        var runner = new MigrationRunner(assembly, migrationContext, processor);
        runner.MigrateUp(true);

        // Or go back down
        //runner.MigrateDown(0);
    }
}

[Migration(1)]
public class CreateUserTable : Migration
{
    public override void Up()
    {
        Create.Table("person")
            .WithColumn("Id").AsGuid().PrimaryKey()
            .WithColumn("Name").AsString();
    }

    public override void Down()
    {
        Delete.Table("person");
    }
}

You'll have troubles doing it in C# with TaskExecutor as that class is meant purely for the console app (migrate.exe).

Marc L.
  • 3,296
  • 1
  • 32
  • 42
Chris S
  • 64,770
  • 52
  • 221
  • 239
  • FYI this code is now obsolete in FluentMigrator 3.0. – Kody May 10 '18 at 04:50
  • For those looking and using FluentMigrator 3.0, you can migrate via code by following their example found on their github docs (https://fluentmigrator.github.io/articles/quickstart.html?tabs=runner-in-process). – Kody May 10 '18 at 04:53
5

Since fluent migrator is a fork of Migrator .NET you might find the getting started for Migrator .net helpful

Mike Two
  • 44,935
  • 9
  • 80
  • 96
  • 2
    I've read it, but I still don't understand how it works. How to execute the migrations? I have an existing project, and I just want to add few classes to the project and run the migrations from THAT project, with no external tools. Is it possible in Fluent Migrator? Something like `FluentMigrator.Migrate("database path", typeof(Migration024));`, which I would call in `Program.Main()`. – Paya May 01 '10 at 20:46
1

This tutorial was useful for me to figure out how to build and use FluentMigrator with MSBuild, if you are using visual studios.

Also comes with the an example of backing up and restoring a database.

craastad
  • 6,222
  • 5
  • 32
  • 46