Skip to content

Commit

Permalink
Initial import of the migrator repository. taken from the refactoring…
Browse files Browse the repository at this point in the history
… branch on marcs repository.
  • Loading branch information
nhemsley committed Sep 25, 2007
1 parent f6b8173 commit e8b84ea
Show file tree
Hide file tree
Showing 74 changed files with 11,643 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/console/Boot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#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;

namespace Migrator.MigratorConsole
{
/// <summary>
/// Console application boostrap class.
/// </summary>
public class Boot
{
[STAThread]
public static int Main(string[] argv)
{
MigratorConsole con = new MigratorConsole(argv);
return con.Run();
}
}
}
46 changes: 46 additions & 0 deletions app/console/Migrator.Console_2005.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{FBE3A83A-D0F8-4D72-AF8D-9EF772569A31}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Migrator.Console</RootNamespace>
<AssemblyName>Migrator.Console</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Migrator.Console\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Migrator.Console\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Boot.cs" />
<Compile Include="MigratorConsole.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\core\Migrator_2005.csproj">
<Project>{1FEE70A4-AAD7-4C60-BE60-3F7DC03A8C4D}</Project>
<Name>Migrator_2005</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
191 changes: 191 additions & 0 deletions app/console/MigratorConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
#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 System.IO;
using Migrator.Tools;

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 string _dumpTo;
private int _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 (_migrateTo == -1)
mig.MigrateToLastVersion();
else
mig.MigrateTo(_migrateTo);
}

/// <summary>
/// List migrations.
/// </summary>
public void List()
{
CheckArguments();

Migrator mig = GetMigrator();
int currentVersion = mig.CurrentVersion;

Console.WriteLine("Available migrations:");
foreach (Type t in mig.MigrationsTypes)
{
int v = Migrator.GetMigrationVersion(t);
Console.WriteLine("{0} {1} {2}",
v == currentVersion ? "=>" : " ",
v.ToString().PadLeft(3),
Migrator.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();
}

#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;
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("-version"))
{
_migrateTo = int.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
}
}
Loading

0 comments on commit e8b84ea

Please sign in to comment.