Changeset 8a9876c93d0e…
Parent 839c8d0d8152…
by
Changes to 3 files · Browse files at 8a9876c93d0e Showing diff from parent 839c8d0d8152 Diff from another changeset...
|
|
@@ -26,11 +26,13 @@ using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Collections.Generic;
+using System.Data.Linq;
+using System.Linq;
#endregion
namespace Migrator
{
- /// <summary>
+ /// <summary>
/// Migrations mediator.
/// </summary>
public class Migrator
@@ -102,6 +104,10 @@ // only load the migrations for the given assembly
_migrationsTypes.AddRange(GetMigrationTypes(migrationAssembly));
+ // Load the begin and end migrations
+ this.BeginMigration = GetBeginMigration(migrationAssembly);
+ this.EndMigration = GetEndMigration(migrationAssembly);
+
CheckForDuplicatedVersion();
// Enshure that the Schema Info table is created
@@ -120,6 +126,14 @@ }
/// <summary>
+ /// The migration that will be run before all others...everytime
+ /// </summary>
+ public Migration BeginMigration { get; private set; }
+ /// <summary>
+ /// The migration that will be run after all others...everytime
+ /// </summary>
+ public Migration EndMigration { get; private set; }
+ /// <summary>
/// Allow a migration to create a TransformationProvider to another database so it can migrate data from that database
/// </summary>
/// <param name="providerName"></param>
@@ -130,7 +144,8 @@ return new ProviderFactory().Create(providerName, constr);
}
- /// <summary>
+
+ /// <summary>
/// Migrate the database to a specific version.
/// Runs all migration between the actual version and the
/// specified version.
@@ -166,6 +181,12 @@
this._logger.Started(CurrentVersion, toVersion);
+ /// Execute the Begin Migration
+ if(this.CurrentVersion < toVersion)
+ InitializeMigration(BeginMigration).Up();
+ else
+ InitializeMigration(BeginMigration).Down();
+
while (this.CurrentVersion != toVersion)
{
if (this.CurrentVersion < toVersion)
@@ -180,6 +201,11 @@ }
}
+ if (this.CurrentVersion < toVersion)
+ InitializeMigration(EndMigration).Up();
+ else
+ InitializeMigration(EndMigration).Down();
+
this._logger.Finished(originalVersion, this.CurrentVersion);
this.CommitTransactions();
}
@@ -440,7 +466,42 @@ return migrations;
}
- #region Helper methods
+ /// <summary>
+ /// Collect migrations in one <c>Assembly</c>.
+ /// </summary>
+ /// <param name="asm">The <c>Assembly</c> to browse.</param>
+ /// <returns>The migrations collection</returns>
+ public static Migration GetBeginMigration(Assembly asm)
+ {
+
+ foreach (Type t in asm.GetTypes())
+ {
+ MigrationBeginAttribute attrib = (MigrationBeginAttribute)
+ Attribute.GetCustomAttribute(t, typeof(MigrationBeginAttribute));
+ if (attrib != null && typeof(Migration).IsAssignableFrom(t))
+ {
+ return (Migration)Activator.CreateInstance(t);
+ }
+ }
+
+ return new NullMigration();
+ }
+
+ private Migration GetEndMigration(Assembly asm)
+ {
+ foreach (Type t in asm.GetTypes())
+ {
+ MigrationEndAttribute attrib = (MigrationEndAttribute)
+ Attribute.GetCustomAttribute(t, typeof(MigrationEndAttribute));
+ if (attrib != null && typeof(Migration).IsAssignableFrom(t))
+ {
+ return (Migration)Activator.CreateInstance(t);
+ }
+ }
+
+ return new NullMigration();
+ }
+ #region Helper methods
private Type GetMigrationType(int version)
{
// since the list is sorted and contiguous getting an item is simple:
@@ -451,14 +512,24 @@ {
Type t = GetMigrationType(version);
Migration m = (Migration)Activator.CreateInstance(t);
- // initialize Migration variables injected from the Migrator
- m.TransformationProvider = _provider;
- m.Databases = this._additionalProviders;
- m.Parameters = this._parameters;
+ InitializeMigration(m);
return m;
}
/// <summary>
+ /// Prepares the migration
+ /// </summary>
+ /// <param name="migration"></param>
+ /// <returns></returns>
+ private Migration InitializeMigration(Migration migration)
+ {
+ // initialize Migration variables injected from the Migrator
+ migration.TransformationProvider = _provider;
+ migration.Databases = this._additionalProviders;
+ migration.Parameters = this._parameters;
+ return migration;
+ }
+ /// <summary>
/// Return a new migrator initialized from an active record/NHibernate config file
/// </summary>
/// <param name="configFilePath">Full path to the Active Record Config File</param>
|
@@ -22,6 +22,7 @@ <OldToolsVersion>2.0</OldToolsVersion>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
+ <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -43,7 +44,13 @@ </PropertyGroup>
<ItemGroup>
<Reference Include="System" />
+ <Reference Include="System.Core">
+ <RequiredTargetFramework>3.5</RequiredTargetFramework>
+ </Reference>
<Reference Include="System.Data" />
+ <Reference Include="System.Data.Linq">
+ <RequiredTargetFramework>3.5</RequiredTargetFramework>
+ </Reference>
<Reference Include="System.Xml" />
<Reference Include="log4net">
<HintPath>..\..\lib\log4net.dll</HintPath>
@@ -72,9 +79,12 @@ <Compile Include="Loggers\NullLogger.cs" />
<Compile Include="Migration.cs" />
<Compile Include="MigrationAttribute.cs" />
+ <Compile Include="MigrationBeginAttribute.cs" />
<Compile Include="MigrationComparer.cs" />
+ <Compile Include="MigrationEndAttribute.cs" />
<Compile Include="MigrationException.cs" />
<Compile Include="Migrator.cs" />
+ <Compile Include="NullMigration.cs" />
<Compile Include="Providers\CascadeBehavior.cs" />
<Compile Include="Providers\MySqlTransformationProvider.cs" />
<Compile Include="Providers\PostgreSQLTransformationProvider.cs" />
|
Loading...