Skip to content

Instantly share code, notes, and snippets.

@NightOwl888
Created October 25, 2013 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NightOwl888/7161082 to your computer and use it in GitHub Desktop.
Save NightOwl888/7161082 to your computer and use it in GitHub Desktop.
Example of how to determine latest assembly version and latest database version of FluentMigrator.
using FluentMigrator;
using FluentMigrator.Infrastructure;
using FluentMigrator.Runner;
using FluentMigrator.Runner.Announcers;
using FluentMigrator.Runner.Extensions;
using FluentMigrator.Runner.Initialization;
using FluentMigrator.Runner.Initialization.AssemblyLoader;
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
/// <summary>
/// Gets the latest FluentMigrator version number in the supplied migrations file.
/// </summary>
public class LatestAssemblyVersion : Task
{
public LatestAssemblyVersion()
{
}
private string migrationAssembly;
private long version;
public string Target { get { return migrationAssembly; } set { migrationAssembly = value; } }
public string MigrationAssembly { get { return migrationAssembly; } set { migrationAssembly = value; } }
public string Namespace { get; set; }
public string Tags { get; set; }
[Output]
public string Version { get { return this.version.ToString(); } }
public override bool Execute()
{
if (string.IsNullOrEmpty(migrationAssembly))
{
Log.LogError("You must specify a migration assembly");
return false;
}
IAnnouncer announcer = new ConsoleAnnouncer();
Log.LogMessage(MessageImportance.Low, "Creating Context");
IRunnerContext runnerContext = new RunnerContext(announcer)
{
Target = Target,
Namespace = Namespace,
Tags = Tags.ToTags()
};
try
{
MigratorInfo info = new MigratorInfo(runnerContext);
this.version = info.GetLatestAssemblyVersion();
}
catch (ProcessorFactoryNotFoundException ex)
{
Log.LogError("While finding the latest assembly version the following error was encountered: {0}", ex.Message);
return false;
}
catch (Exception ex)
{
Log.LogError("While finding the latest assembly version the following error was encountered: {0}, {1}", ex.Message, ex.StackTrace);
return false;
}
return true;
}
}
using FluentMigrator.Runner;
using FluentMigrator.Runner.Announcers;
using FluentMigrator.Runner.Extensions;
using FluentMigrator.Runner.Initialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
/// <summary>
/// Gets the latest FluentMigrator version number in database of the supplied connection string.
/// </summary>
public class LatestDatabaseVersion : Task
{
public LatestDatabaseVersion()
{
}
private string databaseType;
private string migrationAssembly;
private long version;
[Required]
public string Connection { get; set; }
public string ConnectionStringConfigPath { get; set; }
public string Target { get { return migrationAssembly; } set { migrationAssembly = value; } }
public string MigrationAssembly { get { return migrationAssembly; } set { migrationAssembly = value; } }
public string Namespace { get; set; }
public string Tags { get; set; }
public string Database { get { return databaseType; } set { databaseType = value; } }
public string DatabaseType { get { return databaseType; } set { databaseType = value; } }
[Output]
public string Version { get { return this.version.ToString(); } }
public override bool Execute()
{
if (string.IsNullOrEmpty(databaseType))
{
Log.LogError("You must specific a database type. i.e. mysql or sqlserver");
return false;
}
if (string.IsNullOrEmpty(migrationAssembly))
{
Log.LogError("You must specify a migration assembly");
return false;
}
IAnnouncer announcer = new ConsoleAnnouncer();
Log.LogMessage(MessageImportance.Low, "Creating Context");
IRunnerContext runnerContext = new RunnerContext(announcer)
{
Database = databaseType,
Connection = Connection,
ConnectionStringConfigPath = ConnectionStringConfigPath,
Target = Target,
Namespace = Namespace,
Tags = Tags.ToTags()
};
try
{
MigratorInfo info = new MigratorInfo(runnerContext);
this.version = info.GetLatestDatabaseVersion();
}
catch (ProcessorFactoryNotFoundException ex)
{
Log.LogError("While finding the latest database version the following error was encountered: {0}", ex.Message);
return false;
}
catch (Exception ex)
{
Log.LogError("While finding the latest database version the following error was encountered: {0}, {1}", ex.Message, ex.StackTrace);
return false;
}
return true;
}
}
using FluentMigrator;
using FluentMigrator.Infrastructure;
using FluentMigrator.Runner;
using FluentMigrator.Runner.Announcers;
using FluentMigrator.Runner.Extensions;
using FluentMigrator.Runner.Initialization;
using FluentMigrator.Runner.Initialization.AssemblyLoader;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
/// <summary>
/// Retrieves information whether a migration is required and which direction (up/down).
/// </summary>
public class MigratorInfo
{
protected IRunnerContext RunnerContext { get; private set; }
public MigratorInfo(IRunnerContext runnerContext)
{
if (runnerContext == null)
{
throw new ArgumentNullException("runnerContext");
}
this.RunnerContext = runnerContext;
}
public long GetLatestAssemblyVersion()
{
if (string.IsNullOrEmpty(RunnerContext.Target))
{
throw new ArgumentException("You must specify a migration assembly");
}
Assembly assembly = AssemblyLoaderFactory.GetAssemblyLoader(RunnerContext.Target).Load();
MigrationConventions conventions = new MigrationConventions();
MigrationLoader loader = new MigrationLoader(conventions, assembly, RunnerContext.Namespace, RunnerContext.Tags);
long result = 0;
foreach (var migration in loader.FindMigrations())
{
if (result < migration.Version)
result = migration.Version;
}
return result;
}
public long GetLatestDatabaseVersion()
{
if (string.IsNullOrEmpty(RunnerContext.Connection))
{
throw new ArgumentException("You must specify a database connection");
}
if (string.IsNullOrEmpty(RunnerContext.Database))
{
throw new ArgumentException("You must specify a database type. i.e. mysql or sqlserver");
}
if (string.IsNullOrEmpty(RunnerContext.Target))
{
throw new ArgumentException("You must specify a migration assembly");
}
var executor = new MigratorTaskExecutor(this.RunnerContext);
executor.Initialize();
var runner = (MigrationRunner)executor.MigrationRunner;
long result = runner.VersionLoader.VersionInfo.Latest();
runner.Processor.CommitTransaction();
// Remove circular referenced object
runner.VersionLoader.Runner = null;
return result;
}
}
using FluentMigrator;
using FluentMigrator.Infrastructure;
using FluentMigrator.Runner;
using FluentMigrator.Runner.Initialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// Custom task executor that allows access to the migration runner and initialize methods.
/// </summary>
public class MigratorTaskExecutor : TaskExecutor
{
public MigratorTaskExecutor(IRunnerContext runnerContext) : base(runnerContext)
{
}
public void Initialize()
{
base.Initialize();
}
public IMigrationRunner MigrationRunner { get { return Runner; } }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment