3

I want stop my Build process through MSBuild, if there is pending migrations.

But I don't want to trigger the Migrate target while building my project.

So, How can I check only pending migrations with Migratordotnet ?
I just want to use it as a flag to stop my Build process.. !!

Thanks in advance !

Rup
  • 33,765
  • 9
  • 83
  • 112
Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197

2 Answers2

1

Check this other question which shows some code to allow you to check if there are pending migrations. It could be quite possible to have a target set up in MSBuild to run this code and exit from the target if it returns one or more migrations available.

Here's some basic code for a console app that pulls together the code of the other question. It simply writes a message to the console window if migrations are available or not. You'll need to expand it into what your needs are, but it should work. TestMigration1 should be replaced by one of your migration classes in the assembly where your migrations are. You'll obviously need to make a reference to that project from your console app.

internal class Program {
    private static void Main(string[] args) {
        Assembly asm = Assembly.GetAssembly(typeof (TestMigration1));
        const string myConnectionString =
            "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;";
        ITransformationProvider provider = ProviderFactory.Create("SqlServer", myConnectionString);
        var loader = new MigrationLoader(provider, asm, false);
        List<long> availableMigrations = loader.GetAvailableMigrations();

        Console.WriteLine(availableMigrations.Count > 0 ? "Migrations available" : "No migrations");
    }
}
Community
  • 1
  • 1
Sumo
  • 4,066
  • 23
  • 40
  • I want to do it externally from MSBuild, anyways can you suggest some sample code ? – Yugal Jindle Aug 07 '11 at 03:39
  • Using the code from that other question, you can just write a small console utility to do it outside of MSBuild and execute it whichever way you'd like. – Sumo Aug 08 '11 at 01:41
  • The last line of that code returns the number of migrations that are pending to bring the db to the current version. If zero is returned, there are no migrations to be done. – Sumo Aug 08 '11 at 01:51
  • Hm.. This is not exactly what I was looking for, but if this solved my issue - I will revert back. – Yugal Jindle Aug 09 '11 at 08:04
  • I think you need to think through your question then and explain what your build process exactly is. If your process is actually just building a project or a solution, then no, it's not going to work to stop the build process as you need to finish the build before you check for pending migrations. – Sumo Aug 09 '11 at 10:05
  • However, if you have your own custom MSBuild script for your build, then yes, that console app above can be tweaked to work for you as you can make it a step in the build process that gets executed after the project/solution is compiled. However, without those details as part of your question, I'm afraid nobody will be able to answer your question. – Sumo Aug 09 '11 at 10:07
  • Your point holds good points.. basically, I have shifted to another project. So, I will come back to this in a few days, and will reply back here. Thankyou for your effort. – Yugal Jindle Aug 09 '11 at 14:24
0

I think that it is not supported by Migratordotnet !

They only provide 1 Target for MSBuild, that aims at executing the migrations.. so there is no other way in which you could interact with it to check the migrations.

Community
  • 1
  • 1
Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197