OpenSource » Database Migrator » Legacy Migrator
Clone URL:  

Migrator Added Begin and End migrations

Changeset 8a9876c93d0e

Parent 839c8d0d8152

by danielpupek

Changes to 3 files · Browse files at 8a9876c93d0e Showing diff from parent 839c8d0d8152 Diff from another changeset...

 
139
140
141
 
142
 
139
140
141
142
143
@@ -139,4 +139,5 @@
      } +  }
 
26
27
28
 
 
29
30
31
32
33
 
34
35
36
 
102
103
104
 
 
 
 
105
106
107
 
120
121
122
 
 
 
 
 
 
 
 
123
124
125
 
130
131
132
133
 
 
134
135
136
 
166
167
168
 
 
 
 
 
 
169
170
171
 
180
181
182
 
 
 
 
 
183
184
185
 
440
441
442
443
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
444
445
446
 
451
452
453
454
455
456
457
 
458
459
460
461
 
 
 
 
 
 
 
 
 
 
 
 
 
462
463
464
 
26
27
28
29
30
31
32
33
34
 
35
36
37
38
 
104
105
106
107
108
109
110
111
112
113
 
126
127
128
129
130
131
132
133
134
135
136
137
138
139
 
144
145
146
 
147
148
149
150
151
 
181
182
183
184
185
186
187
188
189
190
191
192
 
201
202
203
204
205
206
207
208
209
210
211
 
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
 
512
513
514
 
 
 
 
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
@@ -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
23
24
 
25
26
27
 
43
44
45
 
 
 
46
 
 
 
47
48
49
 
72
73
74
 
75
 
76
77
 
78
79
80
 
22
23
24
25
26
27
28
 
44
45
46
47
48
49
50
51
52
53
54
55
56
 
79
80
81
82
83
84
85
86
87
88
89
90
@@ -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" />