/
MigratorConsole.cs
203 lines (184 loc) · 5.63 KB
/
MigratorConsole.cs
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
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
202
203
#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.Reflection;
using Migrator.Framework;
using Migrator.Tools;
using System.Collections.Generic;
namespace Migrator.MigratorConsole
{
/// <summary>
/// Commande line utility to run the migrations
/// </summary>
/// </remarks>
public class MigratorConsole
{
private string _provider;
private string _connectionString;
private string _migrationsAssembly;
private bool _list = false;
private bool _trace = false;
private bool _dryrun = false;
private string _dumpTo;
private long _migrateTo = -1;
private string[] args;
/// <summary>
/// Builds a new console
/// </summary>
/// <param name="argv">Command line arguments</param>
public MigratorConsole(string[] argv)
{
args = argv;
ParseArguments(argv);
}
/// <summary>
/// Run the migrator's console
/// </summary>
/// <returns>-1 if error, else 0</returns>
public int Run()
{
try
{
if (_list)
List();
else if (_dumpTo != null)
Dump();
else
Migrate();
}
catch (ArgumentException aex)
{
Console.WriteLine("Invalid argument '{0}' : {1}", aex.ParamName, aex.Message);
Console.WriteLine();
PrintUsage();
return -1;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return -1;
}
return 0;
}
/// <summary>
/// Runs the migrations.
/// </summary>
public void Migrate()
{
CheckArguments();
Migrator mig = GetMigrator();
if (mig.DryRun)
mig.Logger.Log("********** Dry run! Not actually applying changes. **********");
if (_migrateTo == -1)
mig.MigrateToLastVersion();
else
mig.MigrateTo(_migrateTo);
}
/// <summary>
/// List migrations.
/// </summary>
public void List()
{
CheckArguments();
Migrator mig = GetMigrator();
List<long> appliedMigrations = mig.AppliedMigrations;
Console.WriteLine("Available migrations:");
foreach (Type t in mig.MigrationsTypes)
{
long v = MigrationLoader.GetMigrationVersion(t);
Console.WriteLine("{0} {1} {2}",
appliedMigrations.Contains(v) ? "=>" : " ",
v.ToString().PadLeft(3),
StringUtils.ToHumanName(t.Name)
);
}
}
public void Dump()
{
CheckArguments();
SchemaDumper dumper = new SchemaDumper(_provider, _connectionString);
dumper.DumpTo(_dumpTo);
}
/// <summary>
/// Show usage information and help.
/// </summary>
public void PrintUsage()
{
int tab = 17;
Version ver = Assembly.GetExecutingAssembly().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}", "version NO".PadRight(tab), "To specific version to migrate the database to");
Console.WriteLine("\t-{0}{1}", "list".PadRight(tab), "List migrations");
Console.WriteLine("\t-{0}{1}", "trace".PadRight(tab), "Show debug informations");
Console.WriteLine("\t-{0}{1}", "dump FILE".PadRight(tab), "Dump the database schema as migration code");
Console.WriteLine("\t-{0}{1}", "dryrun".PadRight(tab), "Simulation mode (don't actually apply/remove any migrations)");
Console.WriteLine();
}
#region Private helper methods
private void CheckArguments()
{
if (_connectionString == null)
throw new ArgumentException("Connection string missing", "connectionString");
if (_migrationsAssembly == null)
throw new ArgumentException("Migrations assembly missing", "migrationsAssembly");
}
private Migrator GetMigrator()
{
Assembly asm = Assembly.LoadFrom(_migrationsAssembly);
Migrator migrator = new Migrator(_provider, _connectionString, asm, _trace);
migrator.args = args;
migrator.DryRun = _dryrun;
return migrator;
}
private void ParseArguments(string[] argv)
{
for (int i = 0; i < argv.Length; i++)
{
if (argv[i].Equals("-list"))
{
_list = true;
}
else if (argv[i].Equals("-trace"))
{
_trace = true;
}
else if (argv[i].Equals("-dryrun"))
{
_dryrun = true;
}
else if (argv[i].Equals("-version"))
{
_migrateTo = long.Parse(argv[i+1]);
i++;
}
else if (argv[i].Equals("-dump"))
{
_dumpTo = argv[i+1];
i++;
}
else
{
if (i == 0) _provider = argv[i];
if (i == 1) _connectionString = argv[i];
if (i == 2) _migrationsAssembly = argv[i];
}
}
}
#endregion
}
}