Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

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.

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).

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top