FluentMigrator not running migrations
Asked Answered
S

2

12

I have inherited a project that uses FluentMigrator to manage migrations. Originally the project was executing the migrations in-process when the application started up but I.T. has cracked down on that and we now have to provide scripts to a DBA for all of our database changes.

As part of this transition I have moved the migrations to a new project called Migrations. When I try to execute the migrations using the command line tool it seems to work but no migrations are applied to the database. The database string is correct because if the VersionInfo table does not exist it is created.enter image description here

There are a number of migrations but most of them are very simple. Here is an example of the first one:

enter image description here

I'm using SQL Server 2012 and FluentMigrator 1.2.1.

Here is the command line in text for gunr2171:

.\Packages\FluentMigrator.1.2.1.0\tools\migrate.exe -c "Data Source=.;Integrated Security=True;Initial Catalog=portal_test" -db sqlserver2012 -a .\source\Migrations\bin\Debug\migrations.dll

And the sample migration:

using System;
using System.Collections.Generic;
using System.Linq;
using FluentMigrator;

namespace Migrations
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
    [Migration(1)]
    public class M001_CreateAccountTable : Migration
    {
        public override void Up()
        {
            Create.Table("Accounts")
                .WithColumn("Id").AsInt32().NotNullable().Identity().Unique()
                .WithColumn("PartnerCode").AsString().Nullable()
                .WithColumn("AccountType").AsInt32().NotNullable()
                .WithColumn("Code").AsString().NotNullable().Unique().PrimaryKey()
                .WithColumn("Name").AsString().NotNullable()
                .WithColumn("PrimaryDomainName").AsString().Nullable()
                .WithColumn("IsFederated").AsBoolean().NotNullable()
                .WithColumn("IsActive").AsBoolean().Nullable().WithDefaultValue(1)
                .WithColumn("FederatedEndpoint").AsString().Nullable()
                .WithColumn("CreatedBy").AsString().NotNullable()
                .WithColumn("CreatedOn").AsDateTime().NotNullable().WithDefaultValue(DateTime.Now)
                .WithColumn("ModifiedBy").AsString().NotNullable()
                .WithColumn("ModifiedOn").AsDateTime().NotNullable().WithDefaultValue(DateTime.Now);
        }

        public override void Down()
        {
            Delete.Table("Accounts");
        }
    }
}
Sterol answered 27/8, 2014 at 18:10 Comment(2)
Are there any previous migrations in the VersionInfo table? Are you building the migrations dll as debug (and not release) in Visual Studio?Ironclad
Did you ever get to the bottom of this?Tartarus
I
10

I was getting the same thing, and it turned out to be that the assembly with the migrations in it had been written using version, let's say, 1.x, and I was running them with the Migrate.exe from version 2.x.

Using the Migrate.exe with the same version that was used to build the migrations DLL solved it for me.

Iwo answered 23/4, 2015 at 19:42 Comment(1)
This is what worked for me. Not being forward compatible, I can understand, but not producing an error of some sort when it fails to update seems like an oversight to me.Waterline
Y
1

I had a similar issue where I was running the migrate.exefrom a command prompt to test my first migration to see how it worked.

I found my issue was that I had added Migrator Tags as documented here to the top of the InitialMigration class

[Tags("Localhost","Development","Test","Production")] // Added these
public class InitialMigration : Migration
{
  // Migration here
}

When I ran the command from a command prompt I missed out the --tag parameter in the command so this command:

migrate.exe --conn="Server=.;Database=my_db;Trusted_Connection=True;Encrypt=True;Connection Timeout=30;" --provider=SqlServer --assembly="MyMigrations.dll" --task=migrate --output --outputFilename="src\migrated.sql"

should have been this:

migrate.exe --conn="Server=.;Database=my_db;Trusted_Connection=True;Encrypt=True;Connection Timeout=30;" --provider=SqlServer --assembly="MyMigrations.dll" --tag="localhost" --task=migrate --output --outputFilename="src\migrated.sql"

NOTE: The --tag parameter missing in the first script.

Yeeyegg answered 9/7, 2018 at 5:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.