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

Migrator Case 55694 -
Added Index Migration functionality

Added the ability to down migrations from other migrations

Changeset 1b5faf072403

Parent 8c6cf6675089

by Profile picture of Daniel PupekDaniel Pupek

Changes to 9 files · Browse files at 1b5faf072403 Showing diff from parent 8c6cf6675089 Diff from another changeset...

 
71
72
73
 
74
75
76
 
97
98
99
 
 
100
101
102
 
139
140
141
142
 
 
 
 
 
 
 
 
 
 
143
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
146
147
148
149
150
151
152
 
 
153
154
155
 
 
 
 
 
 
 
 
156
 
71
72
73
74
75
76
77
 
98
99
100
101
102
103
104
105
 
142
143
144
 
145
146
147
148
149
150
151
152
153
154
155
 
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
 
 
 
 
 
189
190
191
192
 
193
194
195
196
197
198
199
200
201
@@ -71,6 +71,7 @@
  public abstract class Migration   {   private TransformationProvider _transformationProvider; + private Migrator _migrator;   private Dictionary<string, string> _parameters;   private Dictionary<string, TransformationProvider> _additionalProviders;   @@ -97,6 +98,8 @@
  }   }   + +   /// <summary>   /// Allow the migrations access to additional providers   /// </summary> @@ -139,18 +142,60 @@
  }   }   - internal void Prepare(Providers.TransformationProvider _provider,Dictionary<string,Providers.TransformationProvider> dictionary,Dictionary<string,string> dictionary_2) + #region "Other Migrations" + + /// <summary> + /// Used to migrate a SINGLE migration down from another migration. During normal migration processing + /// any migrations that are downed by this method will be up'd again after all migrations have run. + /// The down for a migration will only be called on the firtst execution. Subsequent calls will + /// just return. + /// </summary> + /// <param name="migrationVersion">Version to migrate down</param> + public void MigrateDown(int migrationVersion)   { - TransformationProvider = _provider; + // If the migration isn't in the migrations table then just return + if (!Database.HasMigrationRun(migrationVersion)) + return; + + var currentMigration = _migrator.GetMigration(migrationVersion); + + if (currentMigration == null) + throw new MigrationNotFoundException(migrationVersion); + + currentMigration.Down(); + Database.RemoveMigrationRecord(migrationVersion); + + } + /// <summary> + /// Used to migrate a migration down from another migration. During normal migration processing + /// any migrations that are downed by this method will be up'd again after all migrations have run. + /// The down for a migration will only be called on the firtst execution. Subsequent calls will + /// just return. + /// </summary> + /// <typeparam name="TM">Migration to migrate down</typeparam> + public void MigrateDown<TM>() + { + MigrateDown(Migrator.GetMigrationVersion(typeof (TM))); + } + + #endregion + + internal void Prepare(TransformationProvider provider, Dictionary<string, TransformationProvider> dictionary, Migrator migrator) + { + TransformationProvider = provider;   Databases = this._additionalProviders;   Parameters = this._parameters;   TransformationProvider.Timeout = null; - var guidAttribute = (GuidAttribute)Attribute.GetCustomAttribute(this.GetType().Assembly, typeof(GuidAttribute)); - - if (guidAttribute == null || guidAttribute.Value == null || guidAttribute.Value.Trim().Equals(String.Empty)) - throw new ArgumentException("No guid identifier detected for migration"); - TransformationProvider.AssemblyId = guidAttribute.Value; + this._migrator = migrator; +   }   } - + + /// <summary> + /// These migrations are run after all other migrations + /// </summary> + public abstract class IndexMigration : Migration + { + + }  }
 
15
16
17
18
 
 
19
20
21
 
34
35
36
37
38
39
40
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
43
44
 
15
16
17
 
18
19
20
21
22
 
35
36
37
 
 
 
 
 
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@@ -15,7 +15,8 @@
 namespace Migrator  {   /// <summary> - /// Comparer of Migration by their version attribute. + /// Comparer of Migration by their version attribute and Index yType. + /// Use to sort a list of migrations by Version with all IndexMigrations at the end   /// </summary>   public class MigrationTypeComparer : IComparer<Type>   { @@ -34,11 +35,27 @@
  {   var attribOfX = (MigrationAttribute) Attribute.GetCustomAttribute( x, typeof(MigrationAttribute));   var attribOfY = (MigrationAttribute) Attribute.GetCustomAttribute( y, typeof(MigrationAttribute)); - - if (_ascending) - return attribOfX.Version - attribOfY.Version; - else - return attribOfY.Version - attribOfX.Version; + + var bothIndexes = x.IsSubclassOf(typeof(IndexMigration)) && y.IsSubclassOf(typeof(IndexMigration)); + var bothNotIndexes = !x.IsSubclassOf(typeof(IndexMigration)) && !y.IsSubclassOf(typeof(IndexMigration)); + + if (_ascending) + { + if (bothIndexes || bothNotIndexes) + return attribOfX.Version - attribOfY.Version; + if (x.IsSubclassOf(typeof(IndexMigration))) + return 1; + return -1; + } + else + { + if (bothIndexes || bothNotIndexes) + return attribOfY.Version - attribOfX.Version; + if (x.IsSubclassOf(typeof(IndexMigration))) + return -1; + return 1; + + }   }   }  }
Change 1 of 1 Show Entire File app/​core/​MigrationNotFoundException.cs Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
@@ -0,0 +1,17 @@
+using System; + +namespace Migrator +{ + /// <summary> + /// Thrown when migrate up or down is called on a version that does not exist + /// </summary> + public class MigrationNotFoundException : Exception + { + public MigrationNotFoundException(int migrationVersion): base("The migration was not found. Migration version " + migrationVersion.ToString()) + { + VersionNotFound = migrationVersion; + } + + public int VersionNotFound { get; private set; } + } +} \ No newline at end of file
 
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
 
89
90
91
92
93
94
95
 
96
97
98
 
100
101
102
103
 
104
105
106
 
248
249
250
251
252
253
254
255
256
257
 
 
 
 
 
 
 
 
 
258
259
260
 
414
415
416
417
418
419
420
421
422
423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
424
425
426
 
536
537
538
539
 
540
541
542
 
 
 
543
544
 
545
546
547
 
560
561
562
563
 
564
565
566
 
567
568
 
 
 
569
570
571
 
721
722
723
724
 
725
726
727
 
740
741
742
743
 
744
745
746
 
65
66
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
69
70
71
72
73
 
 
74
75
76
77
 
79
80
81
 
82
83
84
85
 
227
228
229
 
 
 
 
 
 
 
230
231
232
233
234
235
236
237
238
239
240
241
 
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
 
532
533
534
 
535
536
537
538
539
540
541
542
 
543
544
545
546
 
559
560
561
 
562
563
564
565
566
567
 
568
569
570
571
572
573
 
723
724
725
 
726
727
728
729
 
742
743
744
 
745
746
747
748
@@ -65,34 +65,13 @@
  : this(CreateProvider(provider, connectionString), migrationAssembly, logger)   { }   - /// <summary> - /// - /// </summary> - /// <param name="provider"></param> - /// <param name="connectionString"></param> - /// <param name="migrationAssembly"></param> - /// <param name="trace"></param> - public Migrator(string provider, string connectionString, Assembly migrationAssembly, bool trace) - : this(CreateProvider(provider, connectionString), migrationAssembly, new Log4NetLogger()) - { } - - /// <summary> - /// - /// </summary> - /// <param name="provider"></param> - /// <param name="connectionString"></param> - /// <param name="migrationAssembly"></param> - public Migrator(string provider, string connectionString, Assembly migrationAssembly) - : this(CreateProvider(provider, connectionString), migrationAssembly, new Log4NetLogger()) - { } - +   /// <summary>   ///   /// </summary>   /// <param name="provider"></param>   /// <param name="migrationAssembly"></param> - /// <param name="trace"></param> - public Migrator(TransformationProvider provider, Assembly migrationAssembly, bool trace) + public Migrator(TransformationProvider provider, Assembly migrationAssembly)   : this(provider, migrationAssembly, new Log4NetLogger())   { }   @@ -100,7 +79,7 @@
  /// Runs Migrations in the executing <c>Assembly</c>   /// </summary>   public Migrator(string provider, string connectionString) - : this(CreateProvider(provider, connectionString), Assembly.GetExecutingAssembly(), new Log4NetLogger()) + : this(provider, connectionString ,Assembly.GetExecutingAssembly(), new Log4NetLogger())   { }     /// <summary> @@ -248,13 +227,15 @@
  #endregion     #region Static Methods - /// <summary> - /// Allow a migration to create a TransformationProvider to another database so it can migrate data from that database - /// </summary> - /// <param name="providerName"></param> - /// <param name="constr"></param> - /// <returns></returns> - public static TransformationProvider CreateProvider(string providerName, string constr) + + /// <summary> + /// Allow a migration to create a TransformationProvider to another database so it can migrate data from that database + /// </summary> + /// <param name="providerName"></param> + /// <param name="constr"></param> + /// <param name="migrator"></param> + /// <returns></returns> + public static TransformationProvider CreateProvider(string providerName, string constr)   {   return new ProviderFactory().Create(providerName, constr);   } @@ -414,13 +395,28 @@
    this._logger.Started(HighestDatabaseVersion, toVersion);   IEnumerable<int> migratedVersions = new List<int>(); -   - if (goingUp) - migratedVersions = InternalMigrateUpTo(toVersion); - else - migratedVersions = InternalMigrateDownTo(toVersion); - + for (;;) + { + IEnumerable<int> versionsMigratedLastRun; + + if (goingUp) + versionsMigratedLastRun = InternalMigrateUpTo(toVersion); + else + versionsMigratedLastRun = InternalMigrateDownTo(toVersion); + + // Enumerate just once + var migratedLastRun = versionsMigratedLastRun as int[] ?? versionsMigratedLastRun.ToArray(); + // If we migrated no new migrations then break + if (!migratedLastRun.Any()) + break; + + // Add the new set of migrations and continue + migratedVersions = migratedVersions.Concat(migratedLastRun); + + _logger.Log("Checking for additional migrations to run."); + + }   this.CommitTransactions();     _logger.Log("{0} migrations were run {1}", migratedVersions.Count(), goingUp ? "UP":"DOWN"); @@ -536,12 +532,15 @@
  /// </summary>   /// <param name="toVersion">Version to run UP to</param>   /// <returns></returns> - private Queue<Migration> GetMigrationsToBeRunUP(int toVersion) + internal Queue<Migration> GetMigrationsToBeRunUP(int toVersion)   {     var queue = new Queue<Migration>(); + var migs = MigrationsTypes.ToList(); + + migs.Sort(new MigrationTypeComparer(true));   - foreach (var mtype in MigrationsTypes.OrderBy(GetMigrationVersion)) + foreach (var mtype in migs)   {   var version = GetMigrationVersion(mtype);   @@ -560,12 +559,15 @@
  /// </summary>   /// <param name="toVersion"></param>   /// <returns></returns> - private Queue<Migration> GetMigrationsToBeRunDown(int toVersion) + internal Queue<Migration> GetMigrationsToBeRunDown(int toVersion)   {     var queue = new Queue<Migration>(); + var migs = MigrationsTypes.ToList();   - foreach (var mtype in MigrationsTypes.OrderByDescending(GetMigrationVersion)) + migs.Sort(new MigrationTypeComparer(false)); + + foreach (var mtype in migs)   {   var version = GetMigrationVersion(mtype);   @@ -721,7 +723,7 @@
  return MigrationsTypes.FirstOrDefault(m => GetMigrationVersion(m) == version);   }   - private Migration GetMigration(int version) + internal Migration GetMigration(int version)   {   Type t = GetMigrationType(version);   if (t == null) @@ -740,7 +742,7 @@
  private Migration InitializeMigration(Migration migration)   {   // initialize Migration variables injected from the Migrator - migration.Prepare(_provider, this._additionalProviders, this._parameters); + migration.Prepare(_provider, this._additionalProviders, this);   return migration;   }   #endregion
 
79
80
81
 
82
83
84
 
79
80
81
82
83
84
85
@@ -79,6 +79,7 @@
  <Compile Include="Mappers\AbstractMapper.cs" />   <Compile Include="Mappers\MySqlMapper.cs" />   <Compile Include="Mappers\SqlMapper.cs" /> + <Compile Include="MigrationNotFoundException.cs" />   <Compile Include="MigratorSettingsAttribute.cs" />   <Compile Include="PrimaryKey.cs" />   <Compile Include="Providers\SqlParser.cs" />
 
22
23
24
25
 
26
27
 
 
 
28
29
30
 
22
23
24
 
25
26
27
28
29
30
31
32
33
@@ -22,9 +22,12 @@
    string providerName = GuessProviderName(name);   - return (TransformationProvider) Activator.CreateInstance( + var provider = (TransformationProvider) Activator.CreateInstance(   Type.GetType(string.Format("Migrator.Providers.{0}TransformationProvider, Migrator", providerName), true),   new object[] { connectionString }); + + return provider; +   }     private Dictionary<String, String> _provider_Map = new Dictionary<String, String> {
 
94
95
96
 
97
98
99
 
836
837
838
839
840
 
 
 
 
841
842
843
 
94
95
96
97
98
99
100
 
837
838
839
 
 
840
841
842
843
844
845
846
@@ -94,6 +94,7 @@
  set { this._timeout = value; }   }   +   #endregion     #region Tables @@ -836,8 +837,10 @@
    #endregion   - #region Data Manipulation - /// <summary> + + + #region Data Manipulation + /// <summary>   /// Copies rows from one table to another   /// </summary>   /// <param name="fromTable">The name of the table being copied from</param>
Change 1 of 1 Show Entire File migrator.sln.DotSettings.user Stacked
 
 
 
 
 
 
 
1
2
3
4
 
@@ -0,0 +1,4 @@
+<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> + <s:Boolean x:Key="/Default/Housekeeping/UnitTestingMru/UnitTestSessionPersistentData/=2AD4CB150C434A419F25B593C19872DC/@KeyIndexDefined">True</s:Boolean> + <s:String x:Key="/Default/Housekeeping/UnitTestingMru/UnitTestSessionPersistentData/=2AD4CB150C434A419F25B593C19872DC/Name/@EntryValue">MigratorTest</s:String> + <s:String x:Key="/Default/Housekeeping/UnitTestingMru/UnitTestSessionPersistentData/=2AD4CB150C434A419F25B593C19872DC/XmlSerializedElements/@EntryValue">&lt;Session&gt;&lt;Elements&gt;&lt;UnitTestElement Provider="nUnit" Id="Migrator.Tests.MigratorTest" type="NUnitTestFixtureElement" Project="882B6A93-67B8-45BF-8636-5796B1B1CBF8" /&gt;&lt;UnitTestElement Provider="nUnit" Id="Migrator.Tests.MigratorTest.CheckForDuplicatedVersion" ParentId="Migrator.Tests.MigratorTest" type="NUnitTestElement" TypeName="Migrator.Tests.MigratorTest" MethodName="CheckForDuplicatedVersion" Project="882B6A93-67B8-45BF-8636-5796B1B1CBF8" /&gt;&lt;UnitTestElement Provider="nUnit" Id="Migrator.Tests.MigratorTest.CurrentVersion" ParentId="Migrator.Tests.MigratorTest" type="NUnitTestElement" TypeName="Migrator.Tests.MigratorTest" MethodName="CurrentVersion" Project="882B6A93-67B8-45BF-8636-5796B1B1CBF8" /&gt;&lt;UnitTestElement Provider="nUnit" Id="Migrator.Tests.MigratorTest.LastVersion" ParentId="Migrator.Tests.MigratorTest" type="NUnitTestElement" TypeName="Migrator.Tests.MigratorTest" MethodName="LastVersion" Project="882B6A93-67B8-45BF-8636-5796B1B1CBF8" /&gt;&lt;UnitTestElement Provider="nUnit" Id="Migrator.Tests.MigratorTest.MigrateDownwardWithRollback" ParentId="Migrator.Tests.MigratorTest" type="NUnitTestElement" TypeName="Migrator.Tests.MigratorTest" MethodName="MigrateDownwardWithRollback" Project="882B6A93-67B8-45BF-8636-5796B1B1CBF8" /&gt;&lt;UnitTestElement Provider="nUnit" Id="Migrator.Tests.MigratorTest.MigrateToCurrentVersion" ParentId="Migrator.Tests.MigratorTest" type="NUnitTestElement" TypeName="Migrator.Tests.MigratorTest" MethodName="MigrateToCurrentVersion" Project="882B6A93-67B8-45BF-8636-5796B1B1CBF8" /&gt;&lt;UnitTestElement Provider="nUnit" Id="Migrator.Tests.MigratorTest.MigrateUpward" ParentId="Migrator.Tests.MigratorTest" type="NUnitTestElement" TypeName="Migrator.Tests.MigratorTest" MethodName="MigrateUpward" Project="882B6A93-67B8-45BF-8636-5796B1B1CBF8" /&gt;&lt;UnitTestElement Provider="nUnit" Id="Migrator.Tests.MigratorTest.MigrateUpwardWithRollback" ParentId="Migrator.Tests.MigratorTest" type="NUnitTestElement" TypeName="Migrator.Tests.MigratorTest" MethodName="MigrateUpwardWithRollback" Project="882B6A93-67B8-45BF-8636-5796B1B1CBF8" /&gt;&lt;UnitTestElement Provider="nUnit" Id="Migrator.Tests.MigratorTest.TestOrder" ParentId="Migrator.Tests.MigratorTest" type="NUnitTestElement" TypeName="Migrator.Tests.MigratorTest" MethodName="TestOrder" Project="882B6A93-67B8-45BF-8636-5796B1B1CBF8" /&gt;&lt;UnitTestElement Provider="nUnit" Id="Migrator.Tests.MigratorTest.TestOrderDesc" ParentId="Migrator.Tests.MigratorTest" type="NUnitTestElement" TypeName="Migrator.Tests.MigratorTest" MethodName="TestOrderDesc" Project="882B6A93-67B8-45BF-8636-5796B1B1CBF8" /&gt;&lt;/Elements&gt;&lt;/Session&gt;</s:String></wpf:ResourceDictionary> \ No newline at end of file
 
10
11
12
 
 
13
14
15
 
31
32
33
34
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
37
38
 
42
43
44
45
46
 
47
48
 
 
49
50
51
52
53
54
55
56
57
58
 
59
60
61
62
63
64
65
66
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
71
72
 
147
148
149
150
 
 
 
 
 
 
 
 
 
 
 
151
152
 
 
 
 
 
 
 
 
 
 
 
 
153
154
 
 
 
 
 
 
 
 
 
 
 
 
155
156
 
 
 
 
 
 
 
 
 
 
 
 
157
158
 
159
160
161
162
 
163
164
165
166
 
167
168
 
169
170
171
 
172
173
 
174
175
 
 
176
177
 
 
 
178
179
 
180
181
182
 
10
11
12
13
14
15
16
17
 
33
34
35
 
 
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 
66
67
68
 
 
69
70
 
71
72
73
 
 
 
 
 
74
75
76
 
77
78
 
 
 
 
 
 
 
 
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
 
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
 
234
235
236
 
237
238
239
240
241
242
243
244
245
246
247
248
 
249
250
251
252
253
254
255
256
257
258
259
260
261
 
262
263
264
265
266
267
268
269
270
271
272
273
274
 
275
276
277
278
279
280
281
282
283
284
285
286
287
 
288
289
290
291
 
292
293
294
295
 
296
297
298
299
300
 
 
301
302
 
303
304
 
305
306
307
 
308
309
310
311
 
312
313
314
315
@@ -10,6 +10,8 @@
 #endregion  using System;  using System.Collections; +using System.Data; +using System.Linq;  using NUnit.Framework;  using NMock;  using Migrator.Providers; @@ -31,8 +33,30 @@
  [SetUp]   public void SetUp()   { - _provider = new SqlServerTransformationProvider(""); - _migrator = new Migrator(_provider, typeof(FirstMigration).Assembly, true); + var dprovider = new SqlServerTransformationProvider("Server=localhost;Database=Master;Trusted_Connection=True;"); + var dropsql = @"/****** Object: Database [MigratorTest] Script Date: 04/22/2014 12:55:41 ******/ + IF EXISTS (SELECT name FROM sys.databases WHERE name = N'MigratorTest') + DROP DATABASE [MigratorTest] + "; + var createSql = @" + /****** Object: Database [MigratorTest] Script Date: 04/22/2014 12:55:41 ******/ + CREATE DATABASE [MigratorTest] "; + // dprovider.BeginTransaction(); + var cmd = dprovider.Connection.CreateCommand(); + cmd.CommandText = dropsql; + cmd.CommandType = CommandType.Text; + cmd.ExecuteNonQuery(); + cmd.Dispose(); + + cmd = dprovider.Connection.CreateCommand(); + cmd.CommandText = createSql; + cmd.CommandType = CommandType.Text; + cmd.ExecuteNonQuery(); + cmd.Dispose(); + + + _provider = new SqlServerTransformationProvider("Server=localhost;Database=MigratorTest;Trusted_Connection=True;"); + _migrator = new Migrator(_provider, typeof(FirstMigration).Assembly);   _upCalled.Clear();   _downCalled.Clear();   @@ -42,31 +66,94 @@
  [Test]   public void MigrateUpward()   { - SetUpCurrentVersion(1); - _migrator.MigrateTo(3); + _migrator.MigrateToLastVersion();   - Assert.AreEqual(3, _migrator.HighestDatabaseVersion); + Assert.AreEqual(6, _migrator.HighestDatabaseVersion); + Assert.IsTrue(_migrator.HasMigrationRun(5));   - Assert.AreEqual(2, _upCalled.Count); - Assert.AreEqual(0, _downCalled.Count); - - Assert.AreEqual(2, _upCalled[0]); - Assert.AreEqual(3, _upCalled[1]);   }     [Test] - public void MigrateBackward() + public void TestOrder()   { - SetUpCurrentVersion(3); - _migrator.MigrateTo(1); - - Assert.AreEqual(0, _upCalled.Count); - Assert.AreEqual(2, _downCalled.Count); - - Assert.AreEqual(3, _downCalled[0]); - Assert.AreEqual(2, _downCalled[1]); + var migs = _migrator.MigrationsTypes.ToList(); + + migs.Sort(new MigrationTypeComparer(true)); + + Assert.AreEqual(6, migs.Count); + for (var i = 0; i < migs.Count; i++) + { + switch (i) + { + case 0: + //1 + Assert.AreEqual(1, Migrator.GetMigrationVersion(migs[i])); + break; + case 1: + //3 + Assert.AreEqual(3, Migrator.GetMigrationVersion(migs[i])); + break; + case 2: + //5 + Assert.AreEqual(5, Migrator.GetMigrationVersion(migs[i])); + break; + case 3: + //6 + Assert.AreEqual(6, Migrator.GetMigrationVersion(migs[i])); + break; + case 4: + //2 + Assert.AreEqual(2, Migrator.GetMigrationVersion(migs[i])); + break; + case 5: + //4 + Assert.AreEqual(4, Migrator.GetMigrationVersion(migs[i])); + break; + } + } +   } - + + [Test] + public void TestOrderDesc() + { + var migs = _migrator.MigrationsTypes.ToList(); + + migs.Sort(new MigrationTypeComparer(false)); + + Assert.AreEqual(6, migs.Count); + for (var i = 0; i < migs.Count; i++) + { + switch (i) + { + case 5: + //1 + Assert.AreEqual(1, Migrator.GetMigrationVersion(migs[i])); + break; + case 4: + //3 + Assert.AreEqual(3, Migrator.GetMigrationVersion(migs[i])); + break; + case 3: + //5 + Assert.AreEqual(5, Migrator.GetMigrationVersion(migs[i])); + break; + case 2: + //6 + Assert.AreEqual(6, Migrator.GetMigrationVersion(migs[i])); + break; + case 1: + //2 + Assert.AreEqual(2, Migrator.GetMigrationVersion(migs[i])); + break; + case 0: + //4 + Assert.AreEqual(4, Migrator.GetMigrationVersion(migs[i])); + break; + } + } + + }   [Test]   public void MigrateUpwardWithRollback()   { @@ -147,36 +234,82 @@
  #endregion   }   [Migration(1, Ignore = false)] - public class FirstMigration : AbstractTestMigration { } + public class FirstMigration : Migration { + public override void Up() + { + + } + + public override void Down() + { + + } + }   [Migration(2, Ignore = false)] - public class SecondMigration : AbstractTestMigration { } + public class SecondMigration : IndexMigration + { + public override void Up() + { + + } + + public override void Down() + { + + } + }   [Migration(3, Ignore = false)] - public class ThirdMigration : AbstractTestMigration { } + public class ThirdMigration : Migration + { + public override void Up() + { + + } + + public override void Down() + { + + } + }   [Migration(4, Ignore = false)] - public class ForthMigration : AbstractTestMigration { } + public class ForthMigration : IndexMigration + { + public override void Up() + { + + } + + public override void Down() + { + + } + }   [Migration(5, Ignore = false)] - public class BadMigration : AbstractTestMigration + public class BadMigration : Migration   {   override public void Up()   { - throw new Exception("oh uh!"); + //throw new Exception("oh uh!");   }   override public void Down()   { - throw new Exception("oh uh!"); + //throw new Exception("oh uh!");   }   } +   [Migration(6, Ignore = false)] - public class SixthMigration : AbstractTestMigration { } - public class AbstractTestMigration : Migration + public class SixthMigration : Migration   { - override public void Up() + public override void Up()   { - MigratorTest._upCalled.Add(Migrator.GetMigrationVersion(GetType())); + MigrateDown(5); +   } - override public void Down() + + + public override void Down()   { - MigratorTest._downCalled.Add(Migrator.GetMigrationVersion(GetType())); +   }   }  }