OpenSource » Database Migrator » Migrator2
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

Migrator Case 50595
Started work on redesigned migrator

Changeset b8035bc792d9

Parent bd14ef9c10fc

by Profile picture of Daniel PupekDaniel Pupek

Changes to 6 files · Browse files at b8035bc792d9 Showing diff from parent bd14ef9c10fc Diff from another changeset...

 
114
115
116
117
 
118
119
120
 
114
115
116
 
117
118
119
120
@@ -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
33
34
35
 
36
37
38
 
210
211
212
213
 
214
215
 
216
217
218
219
220
221
 
222
223
224
 
379
380
381
382
383
 
 
384
385
386
 
387
388
 
389
390
391
392
393
394
 
 
395
396
397
398
 
399
400
401
402
403
404
405
 
406
407
408
409
 
 
410
411
412
413
414
415
 
 
 
 
 
 
 
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
 
438
439
440
441
 
442
443
 
444
445
446
447
448
449
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
450
451
452
453
454
455
456
457
 
 
458
459
 
460
461
462
 
466
467
468
469
470
 
 
471
472
 
473
474
475
 
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
 
563
564
565
 
32
33
34
 
35
36
37
38
 
210
211
212
 
213
214
215
216
217
218
219
220
221
 
222
223
224
225
 
380
381
382
 
 
383
384
385
386
 
387
388
 
389
390
391
392
393
 
 
394
395
396
397
398
 
399
400
401
402
403
404
405
 
406
407
408
409
 
410
411
412
 
 
 
 
 
413
414
415
416
417
418
419
420
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
421
422
423
424
 
425
426
 
427
428
429
430
431
432
 
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
 
 
553
554
555
 
556
557
558
559
 
563
564
565
 
 
566
567
568
 
569
570
571
572
 
632
633
634
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
635
636
637
638
@@ -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
38
39
40
41
42
 
 
 
 
 
 
 
43
44
45
 
162
163
164
165
166
 
 
167
168
169
 
552
553
554
555
556
 
 
 
557
558
559
 
 
 
 
 
 
560
561
562
563
 
 
564
565
566
567
 
 
 
568
569
570
571
 
 
 
 
 
 
 
 
 
572
573
574
575
576
 
577
578
579
 
580
581
582
 
585
586
587
588
589
 
 
590
591
592
593
594
595
596
 
597
598
599
 
600
601
 
602
603
604
605
 
606
607
608
 
609
610
611
612
613
 
614
615
616
 
 
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
 
 
633
634
635
 
37
38
39
 
 
 
40
41
42
43
44
45
46
47
48
49
 
166
167
168
 
 
169
170
171
172
173
 
556
557
558
 
 
559
560
561
562
 
 
563
564
565
566
567
568
569
570
 
 
571
572
573
574
 
 
575
576
577
578
 
 
 
579
580
581
582
583
584
585
586
587
588
 
 
 
 
589
590
591
 
592
593
594
595
 
598
599
600
 
 
601
602
603
604
605
606
607
608
 
609
610
611
 
612
613
 
614
615
616
617
 
618
619
620
 
621
622
623
624
625
 
626
627
 
 
628
629
630
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631
632
633
634
635
@@ -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
46
47
48
 
49
50
51
 
124
125
126
127
 
128
129
 
130
131
132
 
45
46
47
 
48
49
50
51
 
124
125
126
 
127
128
 
129
130
131
132
@@ -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
158
159
160
 
161
162
163
164
165
166
 
167
168
169
 
157
158
159
 
160
161
162
163
164
165
 
166
167
168
169
@@ -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
32
33
34
 
35
36
37
 
40
41
42
43
44
 
45
46
47
48
49
50
51
 
52
53
54
 
59
60
61
62
 
63
64
65
 
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 
 
82
83
84
 
171
172
173
174
175
 
 
176
177
178
 
31
32
33
 
34
35
36
37
 
40
41
42
 
 
43
44
45
46
47
48
49
 
50
51
52
53
 
58
59
60
 
61
62
63
64
 
66
67
68
 
 
 
 
 
 
 
 
 
 
 
 
69
70
71
72
73
 
160
161
162
 
 
163
164
165
166
167
@@ -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); }   }   }  }