Execute FluentMigrator migrations from code [closed]
Asked Answered
W

5

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()?

Waterer answered 1/5, 2010 at 20:2 Comment(1)
For those looking and using FluentMigrator 3.0, you can migrate via code by following their example found on their github docs (fluentmigrator.github.io/articles/…).Almira
L
5

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

Langsyne answered 1/5, 2010 at 20:10 Comment(1)
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().Waterer
D
19

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

Dina answered 4/4, 2011 at 11:34 Comment(0)
P
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.

Passionless answered 27/9, 2010 at 22:32 Comment(1)
MigrationRunner is probably better class to use for running migrations from code etc...Nonce
N
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).

Navarino answered 8/3, 2013 at 13:45 Comment(2)
FYI this code is now obsolete in FluentMigrator 3.0.Almira
For those looking and using FluentMigrator 3.0, you can migrate via code by following their example found on their github docs (fluentmigrator.github.io/articles/…).Almira
L
5

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

Langsyne answered 1/5, 2010 at 20:10 Comment(1)
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().Waterer
E
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.

Eve answered 28/9, 2012 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.