Changeset b8035bc792d9…
Parent bd14ef9c10fc…
by Daniel Pupek
Changes to 6 files · Browse files at b8035bc792d9 Showing diff from parent bd14ef9c10fc Diff from another changeset...
@@ -114,7 +114,7 @@ CheckArguments();
Migrator mig = GetMigrator();
- int currentVersion = mig.CurrentVersion;
+ int currentVersion = mig.LatestVersion;
Console.WriteLine("Available migrations:");
foreach (Type t in mig.MigrationsTypes)
|
|
|
@@ -32,7 +32,7 @@ {
#region Fields
private TransformationProvider _provider;
- private ArrayList _migrationsTypes = new ArrayList();
+ private IEnumerable<Type> _migrationsTypes = new List<Type>();
private ILogger _logger = new NullLogger();
private log4net.ILog _log4netlogger = log4net.LogManager.GetLogger("Migrator");
private Dictionary<string, string> _parameters;
@@ -210,15 +210,16 @@ /// <summary>
/// Returns the current version of the database.
/// </summary>
- public int CurrentVersion
+ public int LatestVersion
{
get { return _provider.CurrentVersion; }
+
}
/// <summary>
/// Returns registered migration <see cref="System.Type">types</see>.
/// </summary>
- public ArrayList MigrationsTypes
+ public IEnumerable<Type> MigrationsTypes
{
get { return _migrationsTypes; }
}
@@ -379,84 +380,180 @@ /// <param name="toVersion">Version that you wish the database to be at the end of the migration</param>
public void MigrateTo(int toVersion)
{
- int originalVersion = this.CurrentVersion;
- int executingMigration = this.CurrentVersion;
+ int originalVersion = this.LatestVersion;
+ int executingMigrationVersion = this.LatestVersion;
// check for cases where migration is unnecessary or impossible
- if (toVersion == this.CurrentVersion)
+ if (toVersion == this.LatestVersion)
{
- this._logger.Log(string.Format("Current Version is v.{0}, no migration necessary.", this.CurrentVersion));
+ this._logger.Log(string.Format("Current Version is v.{0}, no migration necessary.", this.LatestVersion));
return;
}
else if (
toVersion > 0 && (
- (this.CurrentVersion > toVersion && this.GetMigration(toVersion + 1) == null) ||
- (this.CurrentVersion < toVersion && this.GetMigration(toVersion) == null)
+ (this.LatestVersion > toVersion && this.GetMigration(toVersion + 1) == null) ||
+ (this.LatestVersion < toVersion && this.GetMigration(toVersion) == null)
)
)
{
- this._logger.Warn(string.Format("Cannot Migrate from version {0}, to {1}. Migration for version {1} does not exist", this.CurrentVersion, toVersion));
+ this._logger.Warn(string.Format("Cannot Migrate from version {0}, to {1}. Migration for version {1} does not exist", this.LatestVersion, toVersion));
return;
}
// Perform necessary migrations
try
{
- var goingUp = this.CurrentVersion < toVersion;
+ var goingUp = this.LatestVersion < toVersion;
// start transactions on all databases
this.BeginTransactions();
- this._logger.Started(CurrentVersion, toVersion);
+ this._logger.Started(LatestVersion, toVersion);
+ IEnumerable<int> migratedVersions = new List<int>();
- /// Execute the Begin Migration
- if (goingUp)
- InitializeMigration(BeginMigration).Up();
- else
- InitializeMigration(EndMigration).Down();
+ if (goingUp)
+ migratedVersions = InternalMigrateUpTo(toVersion);
+ else
+ migratedVersions = InternalMigrateDownTo(toVersion);
+
+ _provider.CreateSchemaInfoTable();
+ this.CommitTransactions();
- while (this.CurrentVersion != toVersion)
- {
- if (this.CurrentVersion < toVersion)
- {
- executingMigration = CurrentVersion + 1;
- Up();
- }
- else
- {
- executingMigration = CurrentVersion;
- Down();
- }
- }
-
- if (goingUp)
- InitializeMigration(EndMigration).Up();
- else
- InitializeMigration(BeginMigration).Down();
-
- this._logger.Finished(originalVersion, this.CurrentVersion);
- this.CommitTransactions();
+ this._logger.Finished(originalVersion, this.LatestVersion);
}
catch (Exception exception)
{
- this._logger.Exception(executingMigration, GetMigrationDescription(GetMigration(executingMigration).GetType()), exception);
+ this._logger.Exception(executingMigrationVersion, GetMigrationDescription(GetMigration(executingMigrationVersion).GetType()), exception);
this._logger.RollingBack(originalVersion);
- _log4netlogger.Fatal("Migration to version " + executingMigration + " failed going from version " + originalVersion + " to " + toVersion, exception);
+ _log4netlogger.Fatal("Migration to version " + executingMigrationVersion + " failed going from version " + originalVersion + " to " + toVersion, exception);
this.RollbackTransactions();
throw;
}
}
- /// <summary>
+ /// <summary>
+ /// Used internally to initiate a down migration
+ /// </summary>
+ /// <param name="toVersion"></param>
+ private IEnumerable<int> InternalMigrateDownTo(int toVersion)
+ {
+ InitializeMigration(EndMigration).Down();
+
+ Queue<Migration> migrations = GetMigrationsToBeRunDown(toVersion);
+
+ var migratedVersions = new List<int>();
+
+ while (migrations.Count > 0)
+ {
+ var currentMigration = migrations.Dequeue();
+ var version = GetMigrationVersion(currentMigration.GetType());
+
+ GetReady();
+
+ this._logger.MigrateDown(version, GetMigrationDescription(currentMigration.GetType()));
+ currentMigration.Down();
+
+ migratedVersions.Add(version);
+ }
+
+ InitializeMigration(BeginMigration).Down();
+
+ return migratedVersions;
+ }
+
+ /// <summary>
+ /// Used internally to initiate an UP migration
+ /// </summary>
+ /// <param name="toVersion"></param>
+ private IEnumerable<int> InternalMigrateUpTo(int toVersion)
+ {
+ // Execute the Begin Migration
+
+ InitializeMigration(BeginMigration).Up();
+
+
+ Queue<Migration> migrations = GetMigrationsToBeRunUP(toVersion);
+ var migratedVersions = new List<int>();
+
+ while (migrations.Count > 0)
+ {
+ var currentMigration = migrations.Dequeue();
+ var version = GetMigrationVersion(currentMigration.GetType());
+
+ this._logger.MigrateUp(version, GetMigrationDescription(currentMigration.GetType()));
+ currentMigration.Up();
+ migratedVersions.Add(version);
+ }
+
+ InitializeMigration(EndMigration).Up();
+ return migratedVersions;
+
+ }
+
+ /// <summary>
+ /// Returns all migrations that need to be run in order to migrate UP to this version.
+ /// </summary>
+ /// <param name="toVersion">Version to run UP to</param>
+ /// <returns></returns>
+ private Queue<Migration> GetMigrationsToBeRunUP(int toVersion)
+ {
+
+ var queue = new Queue<Migration>();
+
+ foreach (var mtype in MigrationsTypes)
+ {
+ var version = GetMigrationVersion(mtype);
+
+ if (version <= toVersion && !HasMigrationRun(version))
+ {
+ queue.Enqueue(GetMigration(version));
+ }
+
+ }
+
+ return queue;
+ }
+
+ /// <summary>
+ /// Returns all migrations that need to be run in order to migrate DOWN to this version.
+ /// </summary>
+ /// <param name="toVersion"></param>
+ /// <returns></returns>
+ private Queue<Migration> GetMigrationsToBeRunDown(int toVersion)
+ {
+
+ var queue = new Queue<Migration>();
+
+ foreach (var mtype in MigrationsTypes)
+ {
+ var version = GetMigrationVersion(mtype);
+
+ if (version >= toVersion && HasMigrationRun(version))
+ {
+ queue.Enqueue(GetMigration(version));
+ }
+
+ }
+
+ return queue;
+ }
+
+ private bool HasMigrationRun(int version)
+ {
+ _provider.ExecuteQuery("Select Top 1 From ")
+ throw new NotImplementedException();
+ }
+
+ /// <summary>
/// Migrate Up to the next version, if it exists
/// </summary>
public void MigrateUp()
{
GetReady();
- if (this.CurrentVersion < this.LastVersion)
- this.MigrateTo(this.CurrentVersion + 1);
+ if (this.LatestVersion < this.LastVersion)
+ this.MigrateTo(this.LatestVersion + 1);
else
- this.MigrateTo(this.CurrentVersion);
+ this.MigrateTo(this.LatestVersion);
}
/// <summary>
@@ -466,10 +563,10 @@ {
GetReady();
- if (this.CurrentVersion > 0)
- this.MigrateTo(this.CurrentVersion - 1);
+ if (this.LatestVersion > 0)
+ this.MigrateTo(this.LatestVersion - 1);
else
- this.MigrateTo(this.CurrentVersion);
+ this.MigrateTo(this.LatestVersion);
}
/// <summary>
@@ -535,31 +632,7 @@ }
}
- /// <summary>
- /// Migrate Up one version from the current version
- /// </summary>
- private void Up()
- {
- Migration migration = GetMigration(CurrentVersion + 1);
- this._logger.MigrateUp(this.CurrentVersion + 1, GetMigrationDescription(migration.GetType()));
- migration.Up();
- _provider.CurrentVersion++;
- }
-
- /// <summary>
- /// Migrate down one version from the current version
- /// </summary>
- private void Down()
- {
- GetReady();
-
- Migration migration = GetMigration(CurrentVersion);
- this._logger.MigrateDown(this.CurrentVersion - 1, GetMigrationDescription(migration.GetType()));
- migration.Down();
- _provider.CurrentVersion--;
- }
-
- /// <summary>
+ /// <summary>
///
/// </summary>
/// <returns></returns>
|
|
|
@@ -37,9 +37,13 @@ /// </summary>
public abstract class TransformationProvider : IDisposable
{
- protected string SCHEMA_INFO_TABLE = "SchemaInfo";
- protected const string VERSION_COLUMN = "Version";
- protected const string ASSEMBLY_ID_COLUMN = "AssemblyId";
+ [Obsolete("Stop Using", true)]
+ protected string SchemaInfoTable = "SchemaInfo";
+ protected string OldSchemaInfoTable = "SchemaInfo";
+ protected const string MigrationsTrackingTable = "MigrationTrackingInfo";
+ protected const string VersionColumn = "Version";
+ protected const string AssemblyIdColumn = "AssemblyId";
+
private ILogger _logger = new NullLogger();
private IDbConnection _connection;
@@ -162,8 +166,8 @@ /// <param name="schemaInfoTableName"></param>
internal void SetSchemaInfoTable(string schemaInfoTableName)
{
- SCHEMA_INFO_TABLE = schemaInfoTableName;
- Logger.Log("Using schema info table of [{0}]", schemaInfoTableName);
+ OldSchemaInfoTable = schemaInfoTableName;
+ Logger.Log("Using OLD schema info table of [{0}]", schemaInfoTableName);
}
#endregion
@@ -552,31 +556,40 @@ EnsureHasConnection();
// Does the old style schema info (without the assembly id column) table exist?
- int version = 0;
- if (this.TableExists(SCHEMA_INFO_TABLE) && !this.ColumnExists(SCHEMA_INFO_TABLE, ASSEMBLY_ID_COLUMN))
+ // We'll migrate the old info to the new format
+ int oldVersion = 0;
+ if (this.TableExists(OldSchemaInfoTable) )
{
- version = this.GetVersion(AssemblyId);
- this.RemoveTable(SCHEMA_INFO_TABLE);
+
+ oldVersion = (int?)ExecuteScalar(String.Format("SELECT Version FROM {1} WHERE AssemblyId='{0}'", AssemblyId, OldSchemaInfoTable)) ?? 0;
+ // We have what we needed
+ ExecuteNonQuery("DELETE from {0} WHERE AssemblyId = {1}", OldSchemaInfoTable, AssemblyId);
+
+
}
- // create the table
- if (!this.TableExists(SCHEMA_INFO_TABLE))
+ // create the Schema Info table to store the latest migration version
+ if (!this.TableExists(MigrationsTrackingTable))
{
Column[] columns = new Column[] {
- new Column(VERSION_COLUMN, typeof(int), ColumnProperties.NotNull),
- new Column(ASSEMBLY_ID_COLUMN, typeof(string), 255,ColumnProperties.PrimaryKey)
+ new Column(VersionColumn, typeof(int), ColumnProperties.NotNull),
+ new Column(AssemblyIdColumn, typeof(string), 255,ColumnProperties.NotNull),
+ new Column("UtcDateMigratedColumn", typeof(DateTime), 255,ColumnProperties.NotNull)
};
- this.AddTable(SCHEMA_INFO_TABLE, columns);
- this.InsertVersionRecord(AssemblyId);
- this.SetVersion(version, AssemblyId); // bring over any previous verion
+
+ this.AddTable(MigrationsTrackingTable, columns);
+ this.AddPrimaryKey("MigrationTracking_PK", MigrationsTrackingTable, AssemblyIdColumn, VersionColumn);
+
+ for (var i = 1; i <= oldVersion; i++)
+ {
+ this.RecordMigration(i, AssemblyId); // bring over any previous verion
+ }
+
}
-
- // enchure that a version info record exists for the given assemblyId
- if (!VersionRecordExists(AssemblyId))
- this.InsertVersionRecord(AssemblyId);
+
}
/// <summary>
- /// Get or set the current version of the database.
+ /// Get the current version of the database.
/// This determines if the migrator should migrate up or down
/// in the migration numbers.
/// </summary>
@@ -585,51 +598,38 @@ /// </remark>
public virtual int CurrentVersion
{
- get { return GetVersion(AssemblyId); }
- set { SetVersion(value, AssemblyId); }
+ get { return GetLatestVersion(AssemblyId); }
+
}
/// <summary>
/// Get the version from the SchemaInfo Table
/// </summary>
/// <returns>The current version</returns>
- protected virtual int GetVersion(String assemblyId)
+ protected virtual int GetLatestVersion(String assemblyId)
{
if (assemblyId == null) return 0; // No schema info.
- if (!TableExists(SCHEMA_INFO_TABLE)) return 0;
+ if (!TableExists(MigrationsTrackingTable)) return 0;
- return (int)ExecuteScalar(String.Format("SELECT Version FROM {1} WHERE AssemblyId='{0}'", assemblyId, SCHEMA_INFO_TABLE));
+ return (int)ExecuteScalar(String.Format("SELECT Top 1 Version FROM {1} WHERE AssemblyId='{0}' Order By Version Desc", assemblyId, MigrationsTrackingTable));
}
/// <summary>
- /// Set the current version in the SchemaInfo Table
+ /// Record the current version in the Migration Tracking Table
/// </summary>
/// <param name="version">the new version to set</param>
- protected virtual void SetVersion(int version, String assemblyId)
+ protected virtual void RecordMigration(int version, String assemblyId)
{
if (assemblyId == null)
throw new ArgumentException();
- if (!TableExists(SCHEMA_INFO_TABLE))
+ if (!TableExists(MigrationsTrackingTable))
CreateSchemaInfoTable();
-
- ExecuteNonQuery(string.Format("UPDATE {2} SET Version={0} WHERE AssemblyId='{1}'", version, assemblyId, SCHEMA_INFO_TABLE));
+ // TODO: needs to do an update if required
+ ExecuteNonQuery(string.Format("INSERT INTO {1} (Version, AssemblyId, UtcDateMigratedColumn) VALUES ({2}, '{0}', '{3}')", assemblyId, MigrationsTrackingTable, version, DateTime.UtcNow));
}
-
-
- protected virtual void InsertVersionRecord(String assemblyId)
- {
- ExecuteNonQuery(string.Format("INSERT INTO {1} (Version, AssemblyId) VALUES (0, '{0}')", assemblyId, SCHEMA_INFO_TABLE));
- }
-
- protected virtual bool VersionRecordExists(String assemblyId)
- {
- if (assemblyId == null) return false; // No schema info.
- if (!TableExists(SCHEMA_INFO_TABLE)) return false;
-
- int? ver = (int?)ExecuteScalar(String.Format("SELECT Version FROM {1} WHERE AssemblyId='{0}'", assemblyId, SCHEMA_INFO_TABLE));
- return ver != null;
- }
+
+
#endregion
#region General
|
@@ -45,7 +45,7 @@ SetUpCurrentVersion(1);
_migrator.MigrateTo(3);
- Assert.AreEqual(3, _migrator.CurrentVersion);
+ Assert.AreEqual(3, _migrator.LatestVersion);
Assert.AreEqual(2, _upCalled.Count);
Assert.AreEqual(0, _downCalled.Count);
@@ -124,9 +124,9 @@ public void CurrentVersion()
{
SetUpCurrentVersion(4);
- Assert.AreEqual(4, _migrator.CurrentVersion);
+ Assert.AreEqual(4, _migrator.LatestVersion);
SetUpCurrentVersion(1);
- Assert.AreEqual(1, _migrator.CurrentVersion);
+ Assert.AreEqual(1, _migrator.LatestVersion);
}
[Test]
|
@@ -157,13 +157,13 @@ Assert.AreEqual(0, _provider.CurrentVersion);
// Check that a "set" called after the first run works.
- _provider.CurrentVersion = 1;
+ // _provider.CurrentVersion = 1;
Assert.IsTrue(_provider.TableExists("SchemaInfo"), "No SchemaInfo table created"); // Setting a version should have caused the table to be created.
Assert.AreEqual(1, _provider.CurrentVersion);
_provider.RemoveTable("SchemaInfo");
// Check that a "set" call works on the first run.
- _provider.CurrentVersion = 1;
+ //_provider.CurrentVersion = 1;
Assert.AreEqual(1, _provider.CurrentVersion);
Assert.IsTrue(_provider.TableExists("SchemaInfo"), "No SchemaInfo table created");
}
|
@@ -31,7 +31,7 @@ #region SchemaTable Management
public override void CreateSchemaInfoTable() { }
/// <summary>
- /// Get or set the current version of the database.
+ /// Get the current version of the database.
/// This determines if the migrator should migrate up or down
/// in the migration numbers.
/// </summary>
@@ -40,15 +40,14 @@ /// </remark>
public override int CurrentVersion
{
- get { return GetVersion(_assemblyId); }
- set { SetVersion(value, _assemblyId); }
+ get { return GetLatestVersion(_assemblyId); }
}
/// <summary>
/// Get the version from the SchemaInfo Table
/// </summary>
/// <returns>The current version</returns>
- protected override int GetVersion(String assemblyId)
+ protected override int GetLatestVersion(String assemblyId)
{
if (assemblyId == null) return 0; // No schema info.
if (!_schemaRecords.ContainsKey(assemblyId)) return 0;
@@ -59,7 +58,7 @@ /// Set the current version in the SchemaInfo Table
/// </summary>
/// <param name="version">the new version to set</param>
- protected override void SetVersion(int version, String assemblyId)
+ protected override void RecordMigration(int version, String assemblyId)
{
if (assemblyId == null)
throw new ArgumentException();
@@ -67,18 +66,8 @@ _schemaRecords[assemblyId] = version;
}
-
- protected override void InsertVersionRecord(String assemblyId)
- {
- _schemaRecords[assemblyId] = 0;
- }
-
- protected override bool VersionRecordExists(String assemblyId)
- {
- if (assemblyId == null) return false; // No schema info.
- if (!_schemaRecords.ContainsKey(assemblyId)) return false;
- return true;
- }
+
+
#endregion
public override void RemoveTable(string tableName)
@@ -171,8 +160,8 @@
public int CurrentVersionBackDoor
{
- get { return GetVersion(AssemblyId); ; }
- set { SetVersion(value, AssemblyId); }
+ get { return GetLatestVersion(AssemblyId); ; }
+ set { RecordMigration(value, AssemblyId); }
}
}
}
|
Loading...