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 405057187903

Parent eabffc86b1eb

by Profile picture of Daniel PupekDaniel Pupek

Changes to 22 files · Browse files at 405057187903 Showing diff from parent eabffc86b1eb Diff from another changeset...

Change 1 of 1 Show Entire File app/​console/​HelpText.txt Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
@@ -0,0 +1,24 @@
+Database migrator - v@(Model.Version.Major).@(Model.Version.Minor).@(Model.Version.Revision) + +usage: +Migrator.Console.exe provider connectionString + migrationsAssembly [options] + +provider The database provider + (SqlServer, MySql, Postgre) +connectionString Connection string to the + database +migrationAssembly Path to the assembly + containing the migrations + +Options: +-to=<version> Migrates to specified version +-up Migrates up a version +-down Migrates down a version +-list List migrations +-trace Show debug informations +-h, -help, -? Prints this message + +Reamarks: +-up, -down, and -to options are mutually +exclusive \ No newline at end of file
 
9
10
11
12
 
13
14
15
 
44
45
46
 
 
 
47
48
 
 
 
49
50
51
 
62
63
64
 
 
 
 
65
66
67
 
 
9
10
11
 
12
13
14
15
 
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 
68
69
70
71
72
73
74
75
76
77
 
@@ -9,7 +9,7 @@
  <OutputType>Exe</OutputType>   <AppDesignerFolder>Properties</AppDesignerFolder>   <RootNamespace>Migrator.Console</RootNamespace> - <AssemblyName>Migrator.Console</AssemblyName> + <AssemblyName>migrator-cli</AssemblyName>   <SccProjectName>   </SccProjectName>   <SccLocalPath> @@ -44,8 +44,14 @@
  <WarningLevel>4</WarningLevel>   </PropertyGroup>   <ItemGroup> + <Reference Include="RazorEngine"> + <HintPath>..\..\packages\RazorEngine.3.3.0\lib\net40\RazorEngine.dll</HintPath> + </Reference>   <Reference Include="System" />   <Reference Include="System.Data" /> + <Reference Include="System.Web.Razor"> + <HintPath>..\..\packages\Microsoft.AspNet.Razor.2.0.30506.0\lib\net40\System.Web.Razor.dll</HintPath> + </Reference>   <Reference Include="System.Xml" />   </ItemGroup>   <ItemGroup> @@ -62,6 +68,10 @@
  <None Include="app.config">   <SubType>Designer</SubType>   </None> + <None Include="packages.config" /> + </ItemGroup> + <ItemGroup> + <EmbeddedResource Include="HelpText.txt" />   </ItemGroup>   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />  </Project> \ No newline at end of file
 
15
16
17
 
18
19
20
 
27
28
29
30
 
31
32
33
34
35
36
37
38
 
39
40
41
 
51
52
53
54
 
55
56
57
 
61
62
63
64
65
66
67
68
69
70
71
72
73
 
 
 
 
 
 
 
 
 
 
74
75
76
 
128
129
130
 
 
 
131
132
133
 
143
144
145
146
 
 
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
 
 
 
 
163
164
165
 
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
 
222
223
224
225
226
227
228
 
 
 
 
 
 
 
 
 
 
229
230
231
 
243
244
245
246
 
247
 
 
 
 
248
249
250
 
269
270
271
272
273
274
275
276
277
278
279
280
281
 
282
283
 
 
 
 
 
 
 
284
285
 
15
16
17
18
19
20
21
 
28
29
30
 
31
32
33
34
35
36
37
 
 
38
39
40
41
 
51
52
53
 
54
55
56
57
 
61
62
63
 
 
64
65
 
 
 
 
 
 
66
67
68
69
70
71
72
73
74
75
76
77
78
 
130
131
132
133
134
135
136
137
138
 
148
149
150
 
151
152
153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
155
156
157
158
159
160
 
194
195
196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
198
199
200
 
 
 
 
201
202
203
204
205
206
207
208
209
210
211
212
213
 
225
226
227
 
228
229
230
231
232
233
234
235
236
 
255
256
257
 
 
 
 
 
 
 
 
 
 
258
259
 
260
261
262
263
264
265
266
267
268
@@ -15,6 +15,7 @@
 using System.Collections.Generic;  using System.Collections;  using System.Runtime.InteropServices; +using RazorEngine;    namespace Migrator.MigratorConsole  { @@ -27,15 +28,14 @@
  private string _provider;   private string _connectionString;   private string _migrationsAssembly; - private bool _list = false; + private bool ListMigrations = false;   private bool _trace = false;   private string _dumpTo;   private int _migrateTo = -1;   private bool _migrateUp = false;   private bool _migrateDown = false;   - private bool allParamsSpecified = true; - +   /// <summary>   /// Builds a new console   /// </summary> @@ -51,7 +51,7 @@
  catch (ArgumentException ex)   {   Console.Out.Write(ex.Message); - allParamsSpecified = false; +   }   }   @@ -61,16 +61,18 @@
  /// <returns>-1 if error, else 0</returns>   public int Run()   { - if (!allParamsSpecified) - return 1;   try   { - if (_list) - List(); - else if (_dumpTo != null) - Dump(); - else - Migrate(); + if (ShowHelp) + { + PrintUsage(); + } + else if (ListMigrations) + List(); + else if (_dumpTo != null) + Dump(); + else + Migrate();   }   catch (ArgumentException aex)   { @@ -128,6 +130,9 @@
  }   }   + /// <summary> + /// + /// </summary>   public void Dump()   {   CheckArguments(); @@ -143,23 +148,13 @@
  public void PrintUsage()   {   int tab = 17; - Version ver = Assembly.GetExecutingAssembly().GetName().Version; + var executingAssembly = Assembly.GetExecutingAssembly(); + Version ver = executingAssembly.GetName().Version;   - Console.WriteLine("Database migrator - v{0}.{1}.{2}", ver.Major, ver.Minor, ver.Revision); - Console.WriteLine(); - Console.WriteLine("usage:\nMigrator.Console.exe provider connectionString migrationsAssembly [options]"); - Console.WriteLine(); - Console.WriteLine("\t{0} {1}", "provider".PadRight(tab), "The database provider (SqlServer, MySql, Postgre)"); - Console.WriteLine("\t{0} {1}", "connectionString".PadRight(tab), "Connection string to the database"); - Console.WriteLine("\t{0} {1}", "migrationAssembly".PadRight(tab), "Path to the assembly containing the migrations"); - Console.WriteLine("Options:"); - Console.WriteLine("\t-{0}{1}", "to=<version>".PadRight(tab), "Migrats to specified version"); - Console.WriteLine("\t-{0}{1}", "up".PadRight(tab), "Migrates up a version"); - Console.WriteLine("\t-{0}{1}", "down".PadRight(tab), "Migrates down a version"); - Console.WriteLine("\t-{0}{1}", "list".PadRight(tab), "List migrations"); - Console.WriteLine("\t-{0}{1}", "trace".PadRight(tab), "Show debug informations"); - Console.WriteLine(); - Console.WriteLine("-up, -down, and -to options are mutually exclusive"); + + var text = new StreamReader(executingAssembly.GetManifestResourceStream("Migrator.Console.HelpText.txt")).ReadToEnd(); + Console.Write(Razor.Parse(text, new {Version=ver}, "helpText")); +   }     #region Private helper methods @@ -199,33 +194,20 @@
    }   - private ArrayList GetGeneralMigrations() - { - ArrayList migrations = new ArrayList(); - - foreach (Type t in GetAssembly().GetTypes()) - { - MigrationAttribute attrib = (MigrationAttribute) - Attribute.GetCustomAttribute(t, typeof(MigrationAttribute)); - if (attrib != null && typeof(Migration).IsAssignableFrom(t)) - { - if (!attrib.Ignore) - { - migrations.Add(t); - } - } - } - - migrations.Sort(new MigrationTypeComparer(true)); - return migrations; - } +     private void CheckArguments()   { - if (_connectionString == null) - throw new ArgumentException("Connection string missing", "connectionString"); - if (_migrationsAssembly == null) - throw new ArgumentException("Migrations assembly missing", "migrationsAssembly"); + List<String> messages = new List<String>(); + if (!ListMigrations && !ShowHelp) + { + if (!(_migrateUp ^ _migrateDown ^ _migrateTo > 0) && !(_migrateTo < 0)) messages.Add("You must specify only one up, down, or to command."); + if (_provider == null) messages.Add("Provider not specified (SqlServer, MySql, Postgre)."); + if (_connectionString == null) messages.Add("Connection string not specified."); + if (_migrationsAssembly == null) messages.Add("Migration assembly not specified"); + if (messages.Count > 0) + throw new ArgumentException("\n\n" + String.Join(" \n", messages.ToArray()) + "\n\n"); + }   }     private Migrator GetMigrator() @@ -243,8 +225,12 @@
  {   if (argv[i].ToLower().Equals("-list"))   { - _list = true; + ListMigrations = true;   } + else if (argv[i].ToLower().Equals("-?") || argv[i].ToLower().Equals("-h") || argv[i].ToLower().Equals("-help")) + { + ShowHelp = true; + }   else if (argv[i].ToLower().Equals("-trace"))   {   _trace = true; @@ -269,17 +255,14 @@
  if (i == 2) _migrationsAssembly = argv[i];   }   } - List<String> messages = new List<String>(); - if (!_list && argv.Length > 0) - { - if (!(_migrateUp ^ _migrateDown ^ _migrateTo > 0) && !(_migrateTo < 0)) messages.Add("You must specify only one up, down, or to command."); - if (_provider == null) messages.Add("Provider not specified (SqlServer, MySql, Postgre)."); - if (_connectionString == null) messages.Add("Connection string not specified."); - if (_migrationsAssembly == null) messages.Add("Migration assembly not specified"); - if (messages.Count > 0) - throw new ArgumentException("\n\n" + String.Join(" \n", messages.ToArray()) + "\n\n"); - } +   } - #endregion + + /// <summary> + /// If true then the user would like to see help + /// </summary> + public bool ShowHelp { get; set; } + + #endregion   }  }
Change 1 of 1 Show Entire File app/​console/​packages.config Stacked
 
 
 
 
 
 
 
 
1
2
3
4
5
 
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="Microsoft.AspNet.Razor" version="2.0.30506.0" targetFramework="net40" /> + <package id="RazorEngine" version="3.3.0" targetFramework="net40" /> +</packages> \ No newline at end of file
 
10
11
12
 
13
14
15
16
17
18
19
 
20
21
22
 
 
 
 
23
24
25
26
27
28
 
29
30
31
 
 
32
33
34
 
10
11
12
13
14
15
16
17
18
19
 
20
21
22
23
24
25
26
27
28
29
30
31
32
 
33
34
 
 
35
36
37
38
39
@@ -10,25 +10,30 @@
 #endregion  using System;  using System.Collections; +using System.Collections.Generic;    namespace Migrator  {   /// <summary>   /// Comparer of Migration by their version attribute.   /// </summary> - public class MigrationTypeComparer : IComparer + public class MigrationTypeComparer : IComparer<Type>   {   private bool _ascending = true;   + /// <summary> + /// + /// </summary> + /// <param name="ascending"></param>   public MigrationTypeComparer(bool ascending)   {   _ascending = ascending;   }   - public int Compare(object x, object y) + public int Compare(Type x, Type y)   { - MigrationAttribute attribOfX = (MigrationAttribute) Attribute.GetCustomAttribute((Type) x, typeof(MigrationAttribute)); - MigrationAttribute attribOfY = (MigrationAttribute) Attribute.GetCustomAttribute((Type) y, typeof(MigrationAttribute)); + var attribOfX = (MigrationAttribute) Attribute.GetCustomAttribute( x, typeof(MigrationAttribute)); + var attribOfY = (MigrationAttribute) Attribute.GetCustomAttribute( y, typeof(MigrationAttribute));     if (_ascending)   return attribOfX.Version - attribOfY.Version;
 
32
33
34
35
 
36
37
38
 
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
 
192
193
194
195
 
196
197
 
 
 
 
 
 
198
199
200
 
212
213
214
215
 
216
217
218
219
220
221
222
 
223
224
225
 
344
345
346
347
 
348
349
 
350
351
352
 
380
381
382
383
 
 
 
384
385
386
 
415
416
417
418
 
419
420
421
 
446
447
448
449
450
451
452
453
 
499
500
501
502
 
503
504
505
506
 
507
508
509
 
523
524
525
526
 
527
528
529
530
 
531
532
533
 
537
538
539
540
541
542
543
544
 
545
546
547
 
549
550
551
 
 
 
 
 
552
553
554
555
556
 
 
 
 
 
 
557
558
559
 
563
564
565
566
567
568
569
 
 
 
 
 
 
 
 
570
571
572
 
574
575
576
577
578
579
580
 
638
639
640
641
 
642
643
644
645
646
647
648
649
650
651
652
653
 
654
655
656
 
663
664
665
666
 
667
668
669
 
694
695
696
697
 
698
699
700
 
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
 
32
33
34
 
35
36
37
38
 
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
 
191
192
193
 
194
195
 
196
197
198
199
200
201
202
203
204
 
216
217
218
 
219
220
221
222
223
224
225
 
226
227
228
229
 
348
349
350
 
351
352
 
353
354
355
356
 
384
385
386
 
387
388
389
390
391
392
 
421
422
423
 
424
425
426
427
 
452
453
454
 
 
455
456
457
 
503
504
505
 
506
507
508
509
 
510
511
512
513
 
527
528
529
 
530
531
532
533
 
534
535
536
537
 
541
542
543
 
 
 
 
 
544
545
546
547
 
549
550
551
552
553
554
555
556
557
 
 
 
 
558
559
560
561
562
563
564
565
566
 
570
571
572
 
 
 
 
573
574
575
576
577
578
579
580
581
582
583
 
585
586
587
 
588
589
590
 
648
649
650
 
651
652
653
654
655
656
 
 
 
 
 
 
 
657
658
659
660
 
667
668
669
 
670
671
672
673
 
698
699
700
 
701
702
703
704
 
708
709
710
 
 
 
 
 
 
 
 
 
711
712
713
@@ -32,7 +32,7 @@
  {   #region Fields   private TransformationProvider _provider; - private IEnumerable<Type> _migrationsTypes = new List<Type>(); + private List<Type> _migrationsTypes = new List<Type>();   private ILogger _logger = new NullLogger();   private log4net.ILog _log4netlogger = log4net.LogManager.GetLogger("Migrator");   private Dictionary<string, string> _parameters; @@ -103,56 +103,55 @@
  : this(CreateProvider(provider, connectionString), Assembly.GetExecutingAssembly(), new Log4NetLogger())   { }   - /// <summary> - /// - /// </summary> - /// <param name="provider"></param> - /// <param name="migrationAssembly"></param> - /// <param name="logger"></param> - public Migrator(TransformationProvider provider, Assembly migrationAssembly, ILogger logger) - { - _provider = provider; - _logger = logger; - _provider.Logger = _logger; - ConfigMigrationSettings(migrationAssembly); + /// <summary> + /// + /// </summary> + /// <param name="provider"></param> + /// <param name="migrationAssembly"></param> + /// <param name="logger"></param> + public Migrator(TransformationProvider provider, Assembly migrationAssembly, ILogger logger) + { + _provider = provider; + _logger = logger; + _provider.Logger = _logger; + ConfigMigrationSettings(migrationAssembly);   - if (provider == null) - throw new ArgumentNullException("provider"); + if (provider == null) + throw new ArgumentNullException("provider");   - if (migrationAssembly == null) - { - _logger.Exception("Migration assembly not provided.", new ArgumentNullException("migrationAssembly")); - throw new Exception("Assembly not found"); - } + if (migrationAssembly == null) + { + _logger.Exception("Migration assembly not provided.", new ArgumentNullException("migrationAssembly")); + throw new Exception("Assembly not found"); + }   - if (migrationAssembly != null) - _logger.Log("Performing migrations for {0}", migrationAssembly.FullName);   - // add all providers to the "all providers" list - if (this._additionalProviders != null) - { - foreach (TransformationProvider p in this._additionalProviders.Values) - { - this._allProviders.Add(p); - } - } - this._allProviders.Add(_provider); + _logger.Log("Performing migrations for {0}", migrationAssembly.FullName);   + // add all providers to the "all providers" list + if (this._additionalProviders != null) + { + foreach (TransformationProvider p in this._additionalProviders.Values) + { + this._allProviders.Add(p); + } + } + this._allProviders.Add(_provider);   - // only load the migrations for the given assembly - if (migrationAssembly != null) - { - _migrationsTypes.AddRange(GetMigrationTypes(migrationAssembly));   - this.BeginMigration = GetBeginMigration(migrationAssembly); - this.EndMigration = GetEndMigration(migrationAssembly); - } - CheckForDuplicatedVersion(); - } + // only load the migrations for the given assembly   - + _migrationsTypes = GetMigrationTypes(migrationAssembly).ToList();   - /// <summary> + this.BeginMigration = GetBeginMigration(migrationAssembly); + this.EndMigration = GetEndMigration(migrationAssembly); + + CheckForDuplicatedVersion(); + } + + + + /// <summary>   /// Extracts settings from the assembly   /// </summary>   /// <param name="migratingAssembly"></param> @@ -192,9 +191,14 @@
  {   get   { - if (_migrationsTypes.Count == 0) + if (!MigrationsTypes.Any())   return 0; - return GetMigrationVersion((Type)_migrationsTypes[_migrationsTypes.Count - 1]); + + var migrations = from m in MigrationsTypes + orderby GetMigrationVersion(m) descending + select GetMigrationVersion(m); + + return migrations.First();   }   }   @@ -212,14 +216,14 @@
  /// </summary>   public int LatestVersion   { - get { return _provider.CurrentVersion; } + get { return _provider.GetLatestVersion(_provider.AssemblyId); }     }     /// <summary>   /// Returns registered migration <see cref="System.Type">types</see>.   /// </summary> - public IEnumerable<Type> MigrationsTypes + public List<Type> MigrationsTypes   {   get { return _migrationsTypes; }   } @@ -344,9 +348,9 @@
  /// </summary>   /// <param name="asm">The <c>Assembly</c> to browse.</param>   /// <returns>The migrations collection</returns> - public static ArrayList GetMigrationTypes(Assembly asm) + public static IEnumerable<Type> GetMigrationTypes(Assembly asm)   { - ArrayList migrations = new ArrayList(); + var migrations = new List<Type>();     foreach (Type t in asm.GetTypes())   { @@ -380,7 +384,9 @@
  /// <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.LatestVersion; + GetReady(); + + int originalVersion = this.LatestVersion;   int executingMigrationVersion = this.LatestVersion;     // check for cases where migration is unnecessary or impossible @@ -415,7 +421,7 @@
  else   migratedVersions = InternalMigrateDownTo(toVersion);   - _provider.CreateSchemaInfoTable(); + _provider.CreateMigrationTrackingTable();   this.CommitTransactions();     this._logger.Finished(originalVersion, this.LatestVersion); @@ -446,8 +452,6 @@
  {   var currentMigration = migrations.Dequeue();   var version = GetMigrationVersion(currentMigration.GetType()); - - GetReady();     this._logger.MigrateDown(version, GetMigrationDescription(currentMigration.GetType()));   currentMigration.Down(); @@ -499,11 +503,11 @@
    var queue = new Queue<Migration>();   - foreach (var mtype in MigrationsTypes) + foreach (var mtype in MigrationsTypes.OrderBy(GetMigrationVersion))   {   var version = GetMigrationVersion(mtype);   - if (version <= toVersion && !HasMigrationRun(version)) + if (version <= toVersion && !_provider.HasMigrationRun(version))   {   queue.Enqueue(GetMigration(version));   } @@ -523,11 +527,11 @@
    var queue = new Queue<Migration>();   - foreach (var mtype in MigrationsTypes) + foreach (var mtype in MigrationsTypes.OrderByDescending(GetMigrationVersion))   {   var version = GetMigrationVersion(mtype);   - if (version >= toVersion && HasMigrationRun(version)) + if (version >= toVersion && _provider.HasMigrationRun(version))   {   queue.Enqueue(GetMigration(version));   } @@ -537,11 +541,7 @@
  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 @@ -549,11 +549,18 @@
  public void MigrateUp()   {   GetReady(); + + if (this.LatestVersion < this.LastVersion) + { + // Find the next version + var nextVersion = GetMigrationVersion(MigrationsTypes.Where(m=>GetMigrationVersion(m) > LatestVersion).OrderBy(GetMigrationVersion).First());   - if (this.LatestVersion < this.LastVersion) - this.MigrateTo(this.LatestVersion + 1); - else - this.MigrateTo(this.LatestVersion); + this.MigrateTo(nextVersion); + } + else + { + this.MigrateTo(this.LatestVersion); + }   }     /// <summary> @@ -563,10 +570,14 @@
  {   GetReady();   - if (this.LatestVersion > 0) - this.MigrateTo(this.LatestVersion - 1); - else - this.MigrateTo(this.LatestVersion); + if (this.LatestVersion > 0) + { + // Find the next version + var nextVersion = GetMigrationVersion(MigrationsTypes.Where(m => GetMigrationVersion(m) < LatestVersion).OrderByDescending(GetMigrationVersion).First()); + this.MigrateTo(nextVersion); + } + else + this.MigrateTo(this.LatestVersion);   }     /// <summary> @@ -574,7 +585,6 @@
  /// </summary>   public void MigrateToLastVersion()   { - GetReady();   MigrateTo(LastVersion);   }   @@ -638,19 +648,13 @@
  /// <returns></returns>   private void GetReady()   { - if (MigrationsTypes.Count < 1) + if (!MigrationsTypes.Any())   {   _logger.Exception("Migration assembly not provided.", new ArgumentNullException("migrationAssembly"));   throw new Exception("No migrations found");   }   - //// Enshure that the Schema Info table is created - //GuidAttribute guid = AssemblyGuid; - //if (guid != null) - // _provider.CreateSchemaInfoTable(guid.Value); - //else - // _provider.CreateSchemaInfoTable(); - +   // Print a list of loaded Migrations   _logger.Trace("Loaded migrations:");   foreach (Type t in _migrationsTypes) @@ -663,7 +667,7 @@
  private Type GetMigrationType(int version)   {   // since the list is sorted and contiguous getting an item is simple: - return (Type)_migrationsTypes[version - 1]; + return _migrationsTypes.FirstOrDefault(m => GetMigrationVersion(m) == version);   }     private Migration GetMigration(int version) @@ -694,7 +698,7 @@
  /// <exception cref="CheckForDuplicatedVersion">CheckForDuplicatedVersion</exception>   public void CheckForDuplicatedVersion()   { - ArrayList versions = new ArrayList(); + var versions = new List<int>();     foreach (Type t in _migrationsTypes)   { @@ -704,15 +708,6 @@
    versions.Add(version);   } - - // check that the versions of the migrations are contiguous from 1 to n: - versions.Sort(); - if (versions.Count > 0) - { - int maxVersion = (int)versions[versions.Count - 1]; - if (versions.Count != maxVersion) - throw new ArgumentOutOfRangeException(String.Format("There are {0} Migrations but the highest version is {1}. The Migrations are not contiguous.", versions.Count, maxVersion)); - }   }   #endregion   }
 
551
552
553
554
 
555
556
557
 
565
566
567
568
569
570
571
 
582
583
584
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
 
614
615
616
 
 
 
 
 
 
 
 
 
 
 
617
618
 
 
619
620
621
622
623
 
624
625
626
 
 
 
 
627
628
629
630
631
 
 
 
 
 
 
 
 
 
 
 
 
 
632
633
 
 
 
 
 
 
 
 
 
 
 
634
635
636
 
700
701
702
703
 
 
 
 
 
 
 
 
 
 
 
 
704
705
706
 
551
552
553
 
554
555
556
557
 
565
566
567
 
568
569
570
 
581
582
583
 
584
585
586
587
588
589
 
 
 
 
 
 
 
 
 
 
 
 
 
590
591
592
593
594
595
 
596
597
598
599
 
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
636
637
638
639
640
641
 
642
643
644
645
646
647
648
649
650
651
652
653
654
655
 
719
720
721
 
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
@@ -551,7 +551,7 @@
  /// already existed but the AssemblyId column is missing   /// </remarks>   /// <param name="assemblyId"></param> - public virtual void CreateSchemaInfoTable() + public virtual void CreateMigrationTrackingTable()   {   EnsureHasConnection();   @@ -565,7 +565,6 @@
  // We have what we needed   ExecuteNonQuery("DELETE from {0} WHERE AssemblyId = {1}", OldSchemaInfoTable, AssemblyId);   -   }     // create the Schema Info table to store the latest migration version @@ -582,31 +581,19 @@
    for (var i = 1; i <= oldVersion; i++)   { - this.RecordMigration(i, AssemblyId); // bring over any previous verion + this.RecordMigration(i); // bring over any previous verion   }     }     } - /// <summary> - /// Get the current version of the database. - /// This determines if the migrator should migrate up or down - /// in the migration numbers. - /// </summary> - /// <remark> - /// This value should not be modified inside a migration. - /// </remark> - public virtual int CurrentVersion - { - get { return GetLatestVersion(AssemblyId); } - - } +     /// <summary>   /// Get the version from the SchemaInfo Table   /// </summary>   /// <returns>The current version</returns> - protected virtual int GetLatestVersion(String assemblyId) + public virtual int GetLatestVersion(String assemblyId)   {   if (assemblyId == null) return 0; // No schema info.   if (!TableExists(MigrationsTrackingTable)) return 0; @@ -614,23 +601,55 @@
  return (int)ExecuteScalar(String.Format("SELECT Top 1 Version FROM {1} WHERE AssemblyId='{0}' Order By Version Desc", assemblyId, MigrationsTrackingTable));   }   + /// <summary> + /// Record the current version in the Migration Tracking Table + /// USING UTCNOW + /// </summary> + /// <param name="version">the new version to set</param> + /// <param name="date"></param> + protected virtual void RecordMigration(int version) + { + RecordMigration(version, DateTime.UtcNow); + } +   /// <summary> - /// Record the current version in the Migration Tracking Table + /// Record the current version in the Migration Tracking Table + /// USING THE GIVEN DATE   /// </summary>   /// <param name="version">the new version to set</param> - /// <param name="assemblyId"></param>   /// <param name="date"></param> - protected virtual void RecordMigration(int version, String assemblyId, DateTime date = DateTime.UtcNow) + protected virtual void RecordMigration(int version, DateTime date)   { - if (assemblyId == null) - throw new ArgumentException(); + if (AssemblyId == null) + throw new NullReferenceException("AssemblyId cannot be null."); + + CreateMigrationTrackingTable();   - if (!TableExists(MigrationsTrackingTable)) - CreateSchemaInfoTable(); - // TODO: needs to do an update if required - ExecuteNonQuery(string.Format("INSERT INTO {1} (Version, AssemblyId, UtcDateMigratedColumn) VALUES ({2}, '{0}', '{3}')", assemblyId, MigrationsTrackingTable, version, date)); + if (HasMigrationRun(version)) + { + ExecuteNonQuery( + "UPDATE {1} Set UtcDateMigratedColumn = '{3}' WHERE AssemblyId = '{0}' AND Version = {2}", + AssemblyId, MigrationsTrackingTable, version, date); + } + else + { + + ExecuteNonQuery( + "INSERT INTO {1} (Version, AssemblyId, UtcDateMigratedColumn) VALUES ({2}, '{0}', '{3}')", + AssemblyId, MigrationsTrackingTable, version, date); + }   } - + + /// <summary> + /// Determins if a migration has been run already + /// </summary> + /// <param name="version"></param> + /// <returns></returns> + public bool HasMigrationRun(int version) + { + var reader = ExecuteQuery("Select Top 1 Version From {0} WHERE Version = {1} AND AssemblyId = '{2}'", MigrationsTrackingTable, version, AssemblyId); + return reader.Read(); + }     #endregion   @@ -700,7 +719,18 @@
  return cmd.ExecuteReader();   }   - /// <summary> + /// <summary> + /// Execute an SQL query returning results. + /// </summary> + /// <param name="sql">The SQL command.</param> + /// <param name="format"></param> + /// <returns>A data iterator, <see cref="System.Data.IDataReader">IDataReader</see>.</returns> + public virtual IDataReader ExecuteQuery(string format, params object[] args) + { + return ExecuteQuery(String.Format(format, args)); + } + + /// <summary>   /// Execute an SQL query returning the first result cast to type T.   /// </summary>   /// <param name="sql">The SQL command.</param>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
@@ -0,0 +1,16 @@
+<?xml version="1.0"?> +<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> + <metadata> + <id>Microsoft.AspNet.Razor</id> + <version>2.0.30506.0</version> + <title>Microsoft ASP.NET Razor 2</title> + <authors>Microsoft</authors> + <owners>Microsoft</owners> + <licenseUrl>http://www.microsoft.com/web/webpi/eula/webpages_2_eula_enu.htm</licenseUrl> + <projectUrl>http://www.asp.net/web-pages</projectUrl> + <requireLicenseAcceptance>true</requireLicenseAcceptance> + <description>This package contains the runtime assemblies for ASP.NET Web Pages. ASP.NET Web Pages and the new Razor syntax provide a fast, terse, clean and lightweight way to combine server code with HTML to create dynamic web content.</description> + <language>en-US</language> + <tags>Microsoft AspNet WebPages AspNetWebPages Razor</tags> + </metadata> +</package> \ No newline at end of file
 
 
This file's diff was not loaded because this changeset is very large. Load changes
 
 
Change 1 of 1 Show Entire File packages/​RazorEngine.3.3.0/​RazorEngine.3.3.0.nuspec Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
@@ -0,0 +1,18 @@
+<?xml version="1.0"?> +<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> + <metadata> + <id>RazorEngine</id> + <version>3.3.0</version> + <title>RazorEngine</title> + <authors>Matthew Abbott, Ben Dornis</authors> + <owners>Matthew Abbott, Ben Dornis</owners> + <requireLicenseAcceptance>false</requireLicenseAcceptance> + <description>Simple templating using Razor syntax.</description> + <releaseNotes /> + <copyright /> + <language /> + <dependencies> + <dependency id="Microsoft.AspNet.Razor" version="2.0.30506.0" /> + </dependencies> + </metadata> +</package> \ No newline at end of file
This file's diff was not loaded because this changeset is very large. Load changes
 
 
 
 
Change 1 of 1 Show Entire File packages/​repositories.config Stacked
 
 
 
 
 
 
 
1
2
3
4
 
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?> +<repositories> + <repository path="..\app\console\packages.config" /> +</repositories> \ No newline at end of file
 
10
11
12
 
13
14
15
 
26
27
28
29
30
31
32
33
34
35
 
 
 
36
37
38
 
42
43
44
45
 
46
47
48
 
10
11
12
13
14
15
16
 
27
28
29
 
 
 
 
 
 
 
30
31
32
33
34
35
 
39
40
41
 
42
43
44
45
@@ -10,6 +10,7 @@
 #endregion  using System;  using System.Collections; +using System.Collections.Generic;  using NUnit.Framework;    namespace Migrator.Tests @@ -26,13 +27,9 @@
  [Test]   public void SortAscending()   { - ArrayList list = new ArrayList(); - - list.Add(_types[1]); - list.Add(_types[0]); - list.Add(_types[2]); - - list.Sort(new MigrationTypeComparer(true)); + var list = new List<Type> {_types[1], _types[0], _types[2]}; + + list.Sort(new MigrationTypeComparer(true));     for (int i = 0; i < 3; i++) {   Assert.AreSame(_types[i], list[i]); @@ -42,7 +39,7 @@
  [Test]   public void SortDescending()   { - ArrayList list = new ArrayList(); + var list = new List<Type>();     list.Add(_types[1]);   list.Add(_types[0]);
 
70
71
72
73
74
75
76
 
70
71
72
 
73
74
75
@@ -70,7 +70,6 @@
  <Compile Include="Providers\PostgreSQLTransformationProviderTest.cs" />   <Compile Include="Providers\SqlServerTransformationProviderTest.cs" />   <Compile Include="Providers\TransformationProviderBase.cs" /> - <Compile Include="Providers\TransformationProviderTestAdapter.cs" />   <Compile Include="Tools\SchemaDumperTest.cs" />   </ItemGroup>   <ItemGroup>
 
31
32
33
34
 
35
36
37
 
141
142
143
144
145
 
 
146
147
148
 
31
32
33
 
34
35
36
37
 
141
142
143
 
 
144
145
146
147
148
@@ -31,7 +31,7 @@
  [SetUp]   public void SetUp()   { - _provider = new TransformationProviderTestAdapter(); + _provider = new SqlServerTransformationProvider("");   _migrator = new Migrator(_provider, typeof(FirstMigration).Assembly, true);   _upCalled.Clear();   _downCalled.Clear(); @@ -141,8 +141,8 @@
  private void SetUpCurrentVersion(int version)   {   if(_provider == null) - _provider = new TransformationProviderTestAdapter(); - (_provider as TransformationProviderTestAdapter).CurrentVersionBackDoor = version; + _provider = new SqlServerTransformationProvider(""); +   }   #endregion   }
 
154
155
156
157
 
158
159
160
161
162
 
163
164
165
166
167
 
168
169
170
 
176
177
178
179
 
180
181
182
 
154
155
156
 
157
158
159
160
161
 
162
163
164
165
166
 
167
168
169
170
 
176
177
178
 
179
180
181
182
@@ -154,17 +154,17 @@
  Assert.IsFalse(_provider.TableExists("SchemaInfo"));     // Check that a "get" call works on the first run. - Assert.AreEqual(0, _provider.CurrentVersion); + Assert.AreEqual(0, _provider.GetLatestVersion(_provider.AssemblyId));     // Check that a "set" called after the first run works.   // _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); + Assert.AreEqual(1, _provider.GetLatestVersion(_provider.AssemblyId));     _provider.RemoveTable("SchemaInfo");   // Check that a "set" call works on the first run.   //_provider.CurrentVersion = 1; - Assert.AreEqual(1, _provider.CurrentVersion); + Assert.AreEqual(1, _provider.GetLatestVersion(_provider.AssemblyId));   Assert.IsTrue(_provider.TableExists("SchemaInfo"), "No SchemaInfo table created");   }   @@ -176,7 +176,7 @@
  public void CommitTwice()   {   _provider.Commit(); - Assert.AreEqual(0, _provider.CurrentVersion); + Assert.AreEqual(0, _provider.GetLatestVersion(_provider.AssemblyId));   _provider.Commit();   }  
Change 1 of 1 Show Entire File test/​Providers/​TransformationProviderTestAdapter.cs Stacked
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
63
64
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
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
160
161
162
163
164
165
166
167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
@@ -1,167 +0,0 @@
-#region License -//The contents of this file are subject to the Mozilla Public License -//Version 1.1 (the "License"); you may not use this file except in -//compliance with the License. You may obtain a copy of the License at -//http://www.mozilla.org/MPL/ -//Software distributed under the License is distributed on an "AS IS" -//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the -//License for the specific language governing rights and limitations -//under the License. -#endregion - -using System; -using System.Linq; -using NUnit.Framework; - -using Migrator.Loggers; -using System.Collections.Generic; - -namespace Migrator.Providers.Tests -{ - - public class TransformationProviderTestAdapter : TransformationProvider - { - private int _currentVersion = 0; - private string _assemblyId = "TestAssemblyId"; - private List<Table> _tables = new List<Table>(); - private Dictionary<String, int> _schemaRecords = new Dictionary<String, int>(); - - public TransformationProviderTestAdapter() : base() { this.AssemblyId = _assemblyId; } - - #region SchemaTable Management - public override void CreateSchemaInfoTable() { } - /// <summary> - /// Get the current version of the database. - /// This determines if the migrator should migrate up or down - /// in the migration numbers. - /// </summary> - /// <remark> - /// This value should not be modified inside a migration. - /// </remark> - public override int CurrentVersion - { - get { return GetLatestVersion(_assemblyId); } - } - - /// <summary> - /// Get the version from the SchemaInfo Table - /// </summary> - /// <returns>The current version</returns> - protected override int GetLatestVersion(String assemblyId) - { - if (assemblyId == null) return 0; // No schema info. - if (!_schemaRecords.ContainsKey(assemblyId)) return 0; - return _schemaRecords[assemblyId]; - } - - /// <summary> - /// Set the current version in the SchemaInfo Table - /// </summary> - /// <param name="version">the new version to set</param> - protected override void RecordMigration(int version, String assemblyId) - { - if (assemblyId == null) - throw new ArgumentException(); - - _schemaRecords[assemblyId] = version; - } - - - - #endregion - - public override void RemoveTable(string tableName) - { - if (TableExists(tableName)) - _tables.Remove(GetTable(tableName)); - } - - public override void RenameTable(string tableName, string newName) - { - if (TableExists(tableName)) - GetTable(tableName).Name = newName; - } - - public override Table GetTable(string tableName) - { - return (from table in _tables where table.Name.Equals(tableName) select table).FirstOrDefault(); - } - - public override string[] GetTables() - { - return new string[] { }; - } - - public override bool TableExists(string tableName) - { - return (from table in _tables where table.Name.Equals(tableName) select table).Count() == 1; - } - - public override void AddColumn(string tableName, string column, Type type, int size, ColumnProperties property, object defaultValue) - { - } - - public override void AlterColumn(string tableName, string column, Type type, int size) - { - } - - public override void AlterColumn(string tableName, string column, Type dataType, int size, ColumnProperties props, object defaultValue) - { - } - - public override bool ColumnExists(string tableName, string column) - { - return GetTable(tableName).Column[column] != null; - } - - public override void RemoveColumn(string tableName, string column) - { - var table = GetTable(tableName); - table.Columns.Remove(table.Column[column]); - } - - public override void RenameColumn(string tableName, string column, string newName) - { - GetTable(tableName).Column[column].Name = newName; - } - - public override void AddPrimaryKey(string name, string tableName, params string[] columns) - { - } - - public override void AddForeignKey(string name, string foreignKeyTable, string[] foreignKeyColumns, string entityTableName, string[] primaryKeyColumns) - { - GetTable(foreignKeyTable).ForeignKeys.Add(new ForeignKey(name, foreignKeyColumns, entityTableName, primaryKeyColumns)); - } - - public override void RemoveForeignKey(string name, string tableName) - { - var table = GetTable(tableName); - table.ForeignKeys.Remove(table.ForeignKey[name]); - } - - public override void AddUniqueConstraint(string constraint, string tableName, params string[] columns) - { - } - - public override void RemoveConstraint(string constraint, string tableName) - { - } - - public override bool ConstraintExists(string name, string tableName) - { - return true; - } - - public override int Insert(string tableName, string[] columns, params object[] values) - { - return 0; - } - - public int CurrentVersionBackDoor - { - get { return GetLatestVersion(AssemblyId); ; } - set { RecordMigration(value, AssemblyId); } - } - } -}