OpenSource » Database Migrator » Legacy Migrator
Clone URL:  

Migrator Removed migrator dependancy from nant task now that it only calls the console tool.

Changeset 957b10626f18

Parent 62c445c1f06c

by jasoncline

Changes to 6 files · Browse files at 957b10626f18 Showing diff from parent 62c445c1f06c Diff from another changeset...

 
23
24
25
26
 
27
28
29
 
23
24
25
 
26
27
28
29
@@ -23,7 +23,7 @@
  <OldToolsVersion>3.5</OldToolsVersion>   <UpgradeBackupLocation>   </UpgradeBackupLocation> - <TargetFrameworkVersion>v2.0</TargetFrameworkVersion> + <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>   <TargetFrameworkProfile />   </PropertyGroup>   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
 
1
2
3
 
 
1
2
 
3
@@ -1,3 +1,3 @@
 <?xml version="1.0"?>  <configuration> - <startup><supportedRuntime version="v2.0.50727"/></startup></configuration> + <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
Change 1 of 1 Show Entire File app/​nant/​Loggers/​TaskLogger.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
@@ -1,151 +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 NAnt.Core; -using log4net; - -using Migrator.Loggers; - -namespace Migrator.NAnt.Loggers -{ - /// <summary> - /// NAnt task logger for the migration mediator - /// </summary> - public class TaskLogger : ILogger - { - private int _widthFirstColumn = 5; - private Task _task; - - public TaskLogger(Task task) - { - _task = task; - } - - protected void LogInfo(string format, params object[] args) - { - _task.Log(Level.Info, format, args); - } - - protected void LogError(string format, params object[] args) - { - _task.Log(Level.Error, format, args); - } - - public void Started(int currentVersion, int finalVersion) - { - LogInfo("Current version : {0}", currentVersion); - } - - public void MigrateUp(int version, string migrationName) - { - LogInfo("{0} {1}", version.ToString().PadLeft(_widthFirstColumn), migrationName); - } - - public void MigrateDown(int version, string migrationName) - { - MigrateUp(version, migrationName); - } - - public void Skipping(int version) - { - MigrateUp(version, "<Migration not found>"); - } - - public void RollingBack(int originalVersion) - { - LogInfo("Rolling back to migration {0}", originalVersion); - } - - public void Exception(int version, string migrationName, Exception ex) - { - LogInfo("{0} Error in migration {1} : {2}", "".PadLeft(_widthFirstColumn), version, ex.Message); - this.TraceException(ex); - } - - public void Exception(string format, Exception ex, params object[] args) - { - try - { - LogInfo("{0} Exception thrown: {1}", "".PadLeft(_widthFirstColumn), EscapeFormatBraces(string.Format(format, args))); - LogInfo("{0} Exception Mesage: {1}", "".PadLeft(_widthFirstColumn), args, ex.Message); - this.TraceException(ex); - } - catch - { - return; - } - } - - private void TraceException(Exception ex) - { - LogError(ex.Message); - LogError(ex.StackTrace); - Exception iex = ex.InnerException; - while (ex.InnerException != null) - { - LogError("Caused by: {0}", ex.InnerException); - LogError(ex.InnerException.StackTrace); - iex = iex.InnerException; - } - } - - public void Finished(int originalVersion, int currentVersion) - { - LogInfo("Migrated to version {0}", currentVersion); - } - - public void Log(string format, params object[] args) - { - try - { - LogInfo("{0} {1}", "".PadLeft(_widthFirstColumn), EscapeFormatBraces(string.Format(format, args))); - } - catch - { - return; - } - } - - public void Warn(string format, params object[] args) - { - try - { - LogInfo("{0} [Warning] {1}", "".PadLeft(_widthFirstColumn), EscapeFormatBraces(string.Format(format, args))); - } - catch - { - return; - } - } - - public void Trace(string format, params object[] args) - { - try - { - _task.Log(Level.Debug, "{0} {1}", "".PadLeft(_widthFirstColumn), EscapeFormatBraces(string.Format(format, args))); - } - catch - { - return; - } - } - - public static String EscapeFormatBraces(string toEscape) - { - return toEscape.Replace("{", "{{").Replace("}", "}}"); - - } - - - - } -}
 
16
17
18
19
20
21
22
23
24
25
26
27
 
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
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
 
198
199
200
201
 
202
203
204
205
 
206
207
208
 
227
228
229
230
 
231
232
233
 
236
237
238
239
240
 
 
241
242
243
244
245
 
 
246
247
248
249
250
251
252
253
254
255
256
 
16
17
18
 
19
20
21
22
 
23
24
25
 
120
121
122
123
124
125
126
127
128
129
130
131
132
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
135
136
 
137
138
 
 
 
 
 
 
 
 
139
140
141
 
144
145
146
 
147
148
149
150
 
151
152
153
154
 
173
174
175
 
176
177
178
179
 
182
183
184
 
 
185
186
187
188
189
190
 
191
192
193
194
195
 
 
 
 
 
196
197
198
@@ -16,12 +16,10 @@
 using NAnt.Core.Attributes;  using NAnt.Core.Util;   -using Migrator.NAnt.Loggers;  using System.Xml;  using System.Collections;  using System.Collections.Generic;  using NAnt.Core.Tasks; -using Migrator.Providers;  using System.Diagnostics;    namespace Migrator.NAnt @@ -122,74 +120,22 @@
  {   Migrate();   } + private String GetConnectionString() + { + System.Xml.XmlDocument source = new System.Xml.XmlDocument(); + source.Load(this.ConfigFile);   + //Pulling a connection string out of the ActiveRecord config file + return (source.SelectSingleNode("//add[@key='connection.connection_string']/@value")).Value; + + }   private void Migrate()   { - // determin the default provider - TransformationProvider defaultProvider; - if (this.ConfigFile != null) - { - defaultProvider = Migrator.CreateProvider(this.ConfigFile); - } - else - { - if (this.ConnectionString == null || this.Provider == null) - { - this.Log(Level.Error, "Not enough information to make a DB connection."); - return; - } - - defaultProvider = Migrator.CreateProvider(this.Provider, this.ConnectionString); - } - - // load any additional providers: - Dictionary<string, TransformationProvider> additionalProviders = new Dictionary<string, TransformationProvider>(); - if (this.Providers != null && this.Providers.Count > 0) - { - foreach (ProviderTask providerTask in this.Providers) - { - additionalProviders.Add(providerTask.ProviderName, Migrator.CreateProvider(providerTask.ConfigFile)); - } - } - if (Executable != null) - { - CallMigratorExecutable(defaultProvider); - } - else - { - var migrator = new Migrator(defaultProvider, additionalProviders, Assembly.LoadFrom(this.MigrationsAssembly.FullName), new TaskLogger(this)); - migrator.Logger = new TaskLogger(this); - migrator.MigrationParameters = PropertyTasksToGenericDisctionary(this._params); - - if (this.Up) - { - migrator.MigrateUp(); - } - else if (this.Down) - { - migrator.MigrateDown(); - } - else if (this._to > -1) - { - migrator.MigrateTo(this._to); - } - else - { - migrator.MigrateToLastVersion(); - } - } + CallMigratorExecutable(this.Provider, GetConnectionString());   }   - private int CallMigratorExecutable(TransformationProvider defaultProvider) + private int CallMigratorExecutable(String provider, String connectionString)   { - // Migrator.Console.exe SqlServer "Data Source=localhost;Initial Catalog=Nexport;User Id=;Password=;Integrated Security=True" Nexport.Models.dll -up - - // Defaults to SqlServer provider - string provider = defaultProvider is MySqlTransformationProvider ? "MySql" : - defaultProvider is PostgreSQLTransformationProvider ? "Postgre" : - "SqlServer"; - String databaseConnection = defaultProvider.ConnectionString; -   string assemblyLocation = this.MigrationsAssembly.FullName;     string command = ""; @@ -198,11 +144,11 @@
  else if (this.Down)   command = "-down";   else if (this._to > -1) - command = "-to="+this._to; + command = "-to=" + this._to;   else   command = "";   - string exeArgs = String.Format(" {0} \"{1}\" \"{2}\" {3}", provider, databaseConnection, assemblyLocation, command); + string exeArgs = String.Format(" {0} \"{1}\" \"{2}\" {3}", provider, connectionString, assemblyLocation, command);     ProcessStartInfo processStartInfo = new ProcessStartInfo();   Process migratorProcess = null; @@ -227,7 +173,7 @@
  var outLine = outReader.ReadToEnd();   Console.Out.Write(outLine);   var error = errorReader.ReadToEnd(); - if(error != null && error.Length > 0) + if (error != null && error.Length > 0)   Console.Out.Write("*** {0}", error);   }   migratorProcess.WaitForExit(); @@ -236,21 +182,17 @@
  }   finally   { - if(migratorProcess != null) - migratorProcess.Close(); + if (migratorProcess != null) + migratorProcess.Close();   }   return exitCode;   }   - public void OutputRecievedHandler (object sender, DataReceivedEventArgs e){ + public void OutputRecievedHandler(object sender, DataReceivedEventArgs e) + {   Console.Out.Write(e.Data);   }   - private TransformationProvider GetProvider(ProviderTask providerTask) - { - return Migrator.CreateProvider(providerTask.ConfigFile); - } -   private Dictionary<string, string> PropertyTasksToGenericDisctionary(List<PropertyTask> propertyTasks)   {   Dictionary<string, string> result = new Dictionary<string, string>();
 
23
24
25
26
 
27
28
29
 
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 
23
24
25
 
26
27
28
29
 
73
74
75
 
76
77
78
79
 
 
 
 
 
 
80
81
82
@@ -23,7 +23,7 @@
  <OldToolsVersion>3.5</OldToolsVersion>   <UpgradeBackupLocation>   </UpgradeBackupLocation> - <TargetFrameworkVersion>v2.0</TargetFrameworkVersion> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>   <TargetFrameworkProfile />   <PublishUrl>publish\</PublishUrl>   <Install>true</Install> @@ -73,17 +73,10 @@
  <Reference Include="System.Xml" />   </ItemGroup>   <ItemGroup> - <Compile Include="Loggers\TaskLogger.cs" />   <Compile Include="MigrateTask.cs" />   <Compile Include="ProviderTask.cs" />   </ItemGroup>   <ItemGroup> - <ProjectReference Include="..\core\Migrator.csproj"> - <Project>{1FEE70A4-AAD7-4C60-BE60-3F7DC03A8C4D}</Project> - <Name>Migrator</Name> - </ProjectReference> - </ItemGroup> - <ItemGroup>   <BootstrapperPackage Include="Microsoft.Net.Client.3.5">   <Visible>False</Visible>   <ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
 
6
7
8
9
10
11
12
 
64
65
66
67
 
68
69
70
 
6
7
8
 
9
10
11
 
63
64
65
 
66
67
68
69
@@ -6,7 +6,6 @@
 using NAnt.Core.Attributes;  using NAnt.Core.Util;   -using Migrator.NAnt.Loggers;    namespace Migrator.NAnt  { @@ -64,7 +63,7 @@
    protected override void ExecuteTask()   { - +   }   }  }