OpenSource » Database Migrator » Migrator2
Clone URL:  

Migrator Add an attribute that will allow migration assemblies to specify the name of the schema information storage table.
Case 42946

Changeset 87a50ce52a32

Parent eeb1f70b4867

by danielpupek

Changes to 6 files · Browse files at 87a50ce52a32 Showing diff from parent eeb1f70b4867 Diff from another changeset...

Change 1 of 1 Show Entire File .hgignore Stacked
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
@@ -0,0 +1,10 @@
+syntax: glob +app/console/bin/ +app/console/obj +app/core/bin +app/core/obj +app/nant/bin +app/nant/obj +test/bin +test/obj +*.suo
 
230
231
232
233
 
 
234
235
236
 
237
238
239
 
230
231
232
 
233
234
235
236
 
237
238
239
240
@@ -230,10 +230,11 @@
    private Migrator GetMigrator()   { - GuidAttribute guidAttribute = (GuidAttribute)Attribute.GetCustomAttribute(GetAssembly(), typeof(GuidAttribute)); + Assembly migAssem = GetAssembly(); + GuidAttribute guidAttribute = (GuidAttribute)Attribute.GetCustomAttribute(migAssem, typeof(GuidAttribute));   // This is done incase we are scanning a 4.0 dll, the console is compiled against 4.0 so it can do all the assembly scanning   var logger = new Loggers.ConsoleLogger(_trace); - return new Migrator(_provider, _connectionString, GetGeneralMigrations(), GetBeginMigration(), GetEndMigration(), guidAttribute, logger); + return new Migrator(_provider, _connectionString, migAssem, logger);   }     private void ParseArguments(string[] argv)
 
1
2
3
4
5
6
7
8
9
10
 
26
27
28
 
29
30
31
 
120
121
122
 
123
124
125
 
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
 
404
405
406
407
408
 
 
 
 
 
 
409
410
411
 
 
 
 
 
 
 
 
1
2
3
 
19
20
21
22
23
24
25
 
114
115
116
117
118
119
120
 
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
 
388
389
390
 
 
391
392
393
394
395
396
397
398
399
@@ -1,10 +1,3 @@
-// Project: Migrator, File: Migrator.cs -// Namespace: Migrator, Class: Migrator -// Path: C:\sandboxes\NexPort.Build\ThirdParty\Migrator\app\core, Author: danielpupek -// Code lines: 348, Size of file: 10.13 KB -// Creation date: 4/2/2007 1:36 PM -// Last modified: 4/3/2007 9:29 AM -// Version: $Revision: 1.6 $      #region License @@ -26,6 +19,7 @@
 using System.Runtime.InteropServices;  using System.Text.RegularExpressions;  using System.Collections.Generic; +using System.Linq;    #endregion   @@ -120,6 +114,7 @@
  _provider = provider;   _logger = logger;   _provider.Logger = _logger; + ConfigMigrationSettings(migrationAssembly);     if (provider == null)   throw new ArgumentNullException("provider"); @@ -155,39 +150,28 @@
  CheckForDuplicatedVersion();   }   - /// <summary> - /// / - /// </summary> - /// <param name="provider"></param> - /// <param name="connectionString"></param> - /// <param name="migrations"></param> - /// <param name="beginMigration"></param> - /// <param name="endMigration"></param> - /// <param name="assemblyAttribute"></param> - /// <param name="logger"></param> - public Migrator(string provider, string connectionString, ArrayList migrations, Migration beginMigration, Migration endMigration, GuidAttribute assemblyAttribute, ILogger logger) - : this(CreateProvider(provider, connectionString), migrations, beginMigration, endMigration, assemblyAttribute, logger) - { } +   - /// <summary> - /// - /// </summary> - /// <param name="provider"></param> - /// <param name="migrations"></param> - /// <param name="beginMigration"></param> - /// <param name="endMigration"></param> - /// <param name="assemblyAttribute"></param> - /// <param name="logger"></param> - public Migrator(TransformationProvider provider, ArrayList migrations, Migration beginMigration, Migration endMigration, GuidAttribute assemblyAttribute, ILogger logger) + /// <summary> + /// Extracts settings from the assembly + /// </summary> + /// <param name="migratingAssembly"></param> + private void ConfigMigrationSettings(Assembly migratingAssembly)   { - logger = logger ?? new Log4NetLogger(); - _provider = provider; - _provider.AssemblyId = assemblyAttribute != null ? assemblyAttribute.Value : null; - _logger = logger; - _provider.Logger = _logger; - BeginMigration = beginMigration ?? new NullMigration(); - EndMigration = endMigration ?? new NullMigration(); - MigrationsTypes.AddRange(migrations); + var attributes = migratingAssembly.GetCustomAttributes(typeof(MigratorSettingsAttribute), false).Cast<MigratorSettingsAttribute>().ToList(); + if (attributes.Count() > 0) + { + MigratorSettingsAttribute settings = attributes[0]; + + if(!string.IsNullOrEmpty(settings.SchemaInfoTableName)) _provider.SetSchemaInfoTable(settings.SchemaInfoTableName); + + var guid = migratingAssembly.GetCustomAttributes(typeof(GuidAttribute), false).Cast<GuidAttribute>().ToList(); + if (guid.Count > 0) + { + _provider.AssemblyId = guid[0].Value; + } + + }   }   #endregion   @@ -404,8 +388,12 @@
  this._logger.Log(string.Format("Current Version is v.{0}, no migration necessary.", this.CurrentVersion));   return;   } - else if ((this.CurrentVersion > toVersion && this.GetMigration(toVersion + 1) == null) || - (this.CurrentVersion < toVersion && this.GetMigration(toVersion) == null)) + else if ( + toVersion > 0 && ( + (this.CurrentVersion > toVersion && this.GetMigration(toVersion + 1) == null) || + (this.CurrentVersion < 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));   return;
 
81
82
83
 
84
85
86
 
81
82
83
84
85
86
87
@@ -81,6 +81,7 @@
  <Compile Include="Mappers\AbstractMapper.cs" />   <Compile Include="Mappers\MySqlMapper.cs" />   <Compile Include="Mappers\SqlMapper.cs" /> + <Compile Include="MigratorSettingsAttribute.cs" />   <Compile Include="PrimaryKey.cs" />   <Compile Include="Providers\SqlParser.cs" />   <Compile Include="Table.cs" />
Change 1 of 1 Show Entire File app/​core/​MigratorSettingsAttribute.cs Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@@ -0,0 +1,18 @@
+using System; +using System.Collections.Generic; +using System.Linq; + +namespace Migrator +{ + /// <summary> + /// When placed on an assembly with migrations the migrator will extract settings from this attribute. + /// </summary> + [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)] + public class MigratorSettingsAttribute : Attribute + { + public string SchemaInfoTableName; + public MigratorSettingsAttribute() : this(string.Empty) { } + public MigratorSettingsAttribute(string schemaInfoTableName) { SchemaInfoTableName = schemaInfoTableName; } + + } +}
 
37
38
39
40
 
41
42
43
 
154
155
156
 
 
 
 
 
 
 
 
 
 
157
158
159
 
37
38
39
 
40
41
42
43
 
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
@@ -37,7 +37,7 @@
  /// </summary>   public abstract class TransformationProvider : IDisposable   { - protected const string SCHEMA_INFO_TABLE = "SchemaInfo"; + protected string SCHEMA_INFO_TABLE = "SchemaInfo";   protected const string VERSION_COLUMN = "Version";   protected const string ASSEMBLY_ID_COLUMN = "AssemblyId";   @@ -154,6 +154,16 @@
  /// <param name="tableName">Table name</param>   /// <returns><c>true</c> if the table exists</returns>   public abstract bool TableExists(string tableName); + + /// <summary> + /// Sets the name of the schema info table + /// </summary> + /// <param name="schemaInfoTableName"></param> + internal void SetSchemaInfoTable(string schemaInfoTableName) + { + SCHEMA_INFO_TABLE = schemaInfoTableName; + Logger.Log("Using schema info table of [{0}]", schemaInfoTableName); + }   #endregion     #region Columns