Skip to content

Commit

Permalink
Issue 52
Browse files Browse the repository at this point in the history
Add a dry run option for the runners that won't actually apply the changes but rather shows what would have happened.
Thanks to evonzee for the patch (although it was changed a bit)
  • Loading branch information
geofflane committed Sep 5, 2008
1 parent e2976aa commit 575829d
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 29 deletions.
30 changes: 27 additions & 3 deletions doc/example/example-msbuild.proj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MigratorTasksPath>$(MSBuildProjectDirectory)/../bin</MigratorTasksPath>
<MigratorTasksPath>$(MSBuildProjectDirectory)\..\..\build</MigratorTasksPath>
</PropertyGroup>

<!-- Migration-related targets, properties, etc. -->
Expand All @@ -14,7 +14,31 @@

<Migrate Provider="MySql"
Connectionstring="Data Source=localhost;Database=test;User Id=root;Password=;"
Directory="migrations"
Migrations="$(MSBuildProjectDirectory)\migrations\migrations.dll"
To="$(SchemaVersion)"/>
</Target>
</Project>

<Target Name="Migrate-DryRun">
<CreateProperty Value="-1" Condition="'$(SchemaVersion)'==''">
<Output TaskParameter="Value" PropertyName="SchemaVersion"/>
</CreateProperty>

<Migrate Provider="MySql"
Connectionstring="Data Source=localhost;Database=test;User Id=root;Password=;"
Migrations="$(MSBuildProjectDirectory)\migrations\migrations.dll"
To="$(SchemaVersion)"
DryRun="true"/>
</Target>

<Target Name="Migrate-Dump">
<CreateProperty Value="-1" Condition="'$(SchemaVersion)'==''">
<Output TaskParameter="Value" PropertyName="SchemaVersion"/>
</CreateProperty>

<Migrate Provider="MySql"
Connectionstring="Data Source=localhost;Database=test;User Id=root;Password=;"
Migrations="$(MSBuildProjectDirectory)\migrations\migrations.dll"
To="$(SchemaVersion)"
ScriptFile="MSBuild-migrations.sql"/>
</Target>
</Project>
22 changes: 21 additions & 1 deletion doc/example/example-nant.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<project name="migrator" xmlns="http://nant.sf.net/release/0.85/nant.xsd">
<loadtasks assembly="../bin/Migrator.NAnt.dll" />
<loadtasks assembly="../../build/Migrator.NAnt.dll" />
<target name="migrate" description="Migrate the database">
<property name="version" value="-1" overwrite="false" />
<migrate
Expand All @@ -9,4 +9,24 @@
directory="migrations"
to="${version}" />
</target>

<target name="migrate-dryrun" description="Migrate the database">
<property name="version" value="-1" overwrite="false" />
<migrate
provider="MySql"
connectionstring="Data Source=localhost;Database=test;User Id=root;Password=;"
directory="migrations"
to="${version}"
dryrun="true"/>
</target>

<target name="migrate-dump" description="Migrate the database">
<property name="version" value="-1" overwrite="false" />
<migrate
provider="MySql"
connectionstring="Data Source=localhost;Database=test;User Id=root;Password=;"
directory="migrations"
to="${version}"
scriptFile="nant-migrations.sql"/>
</target>
</project>
10 changes: 10 additions & 0 deletions src/Migrator.Console/MigratorConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class MigratorConsole
private string _migrationsAssembly;
private bool _list = false;
private bool _trace = false;
private bool _dryrun = false;
private string _dumpTo;
private int _migrateTo = -1;
private string[] args;
Expand Down Expand Up @@ -80,6 +81,9 @@ 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
Expand Down Expand Up @@ -137,6 +141,7 @@ public void PrintUsage()
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();
}

Expand All @@ -155,6 +160,7 @@ private Migrator GetMigrator()

Migrator migrator = new Migrator(_provider, _connectionString, asm, _trace);
migrator.args = args;
migrator.DryRun = _dryrun;
return migrator;
}

Expand All @@ -170,6 +176,10 @@ private void ParseArguments(string[] argv)
{
_trace = true;
}
else if (argv[i].Equals("-dryrun"))
{
_dryrun = true;
}
else if (argv[i].Equals("-version"))
{
_migrateTo = int.Parse(argv[i+1]);
Expand Down
11 changes: 11 additions & 0 deletions src/Migrator.MSBuild/MigrateTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class Migrate : Task
private string _connectionString;
private ITaskItem[] _migrationsAssembly;
private bool _trace;
private bool _dryrun;
private string _scriptFile;

private string _directory;
Expand Down Expand Up @@ -109,6 +110,12 @@ public bool Trace
get { return _trace; }
}

public bool DryRun
{
set { _dryrun = value; }
get { return _dryrun; }
}

/// <summary>
/// Gets value indicating whether to script the changes made to the database
/// to the file indicated by <see cref="ScriptFile"/>.
Expand Down Expand Up @@ -152,6 +159,7 @@ public override bool Execute()
private void Execute(Assembly asm)
{
Migrator mig = new Migrator(Provider, ConnectionString, asm, Trace, new TaskLogger(this));
mig.DryRun = DryRun;
if (ScriptChanges)
{
using (StreamWriter writer = new StreamWriter(ScriptFile))
Expand All @@ -168,6 +176,9 @@ private void Execute(Assembly asm)

private void RunMigration(Migrator mig)
{
if (mig.DryRun)
mig.Logger.Log("********** Dry run! Not actually applying changes. **********");

if (_to == -1)
mig.MigrateToLastVersion();
else
Expand Down
12 changes: 12 additions & 0 deletions src/Migrator.NAnt/MigrateTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class MigrateTask : Task
private string _connectionString;
private FileInfo _migrationsAssembly;
private bool _trace;
private bool _dryrun;
private string _scriptFile;

private string _directory;
Expand Down Expand Up @@ -100,6 +101,13 @@ public bool Trace
set { _trace = value; }
get { return _trace; }
}

[TaskAttribute("dryrun")]
public bool DryRun
{
set { _dryrun = value; }
get { return _dryrun; }
}

/// <summary>
/// Gets value indicating whether to script the changes made to the database
Expand Down Expand Up @@ -140,6 +148,7 @@ protected override void ExecuteTask()
private void Execute(Assembly asm)
{
Migrator mig = new Migrator(Provider, ConnectionString, asm, Trace, new TaskLogger(this));
mig.DryRun = DryRun;
if (ScriptChanges)
{
using (StreamWriter writer = new StreamWriter(ScriptFile))
Expand All @@ -156,6 +165,9 @@ private void Execute(Assembly asm)

private void RunMigration(Migrator mig)
{
if (mig.DryRun)
mig.Logger.Log("********** Dry run! Not actually applying changes. **********");

if (_to == -1)
mig.MigrateToLastVersion();
else
Expand Down
7 changes: 7 additions & 0 deletions src/Migrator/BaseMigrate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public abstract class BaseMigrate
protected List<long> _availableMigrations;
protected List<long> _original;
protected long _current;
protected bool _dryrun;

protected BaseMigrate(List<long> availableMigrations, ITransformationProvider provider, ILogger logger)
{
Expand All @@ -35,6 +36,12 @@ public virtual long Current
protected set { _current = value; }
}

public virtual bool DryRun
{
get { return _dryrun; }
set { _dryrun = value; }
}

public abstract long Previous { get; }
public abstract long Next { get; }

Expand Down
6 changes: 4 additions & 2 deletions src/Migrator/Compile/ScriptEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
using Migrator.Framework;

namespace Migrator.Compile
{
Expand Down Expand Up @@ -75,8 +76,9 @@ private CompilerParameters SetupCompilerParams()

private string FullAssemblyPath(string assemblyName)
{
string assemblyLocation = Assembly.GetExecutingAssembly().Location;
return Path.Combine(Directory.GetParent(assemblyLocation).FullName, "Migrator.Framework.dll");
string assemblyLocation = Assembly.GetAssembly(typeof(MigrationAttribute)).Location;
Console.Out.WriteLine("Assembly Path: {0}", assemblyLocation);
return Path.Combine(Directory.GetParent(assemblyLocation).FullName, assemblyName);
}
}
}
53 changes: 32 additions & 21 deletions src/Migrator/MigrateAnywhere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ public class MigrateAnywhere : BaseMigrate
public MigrateAnywhere(List<long> availableMigrations, ITransformationProvider provider, ILogger logger)
: base(availableMigrations, provider, logger)
{
_current = 0;
if (provider.AppliedMigrations.Count > 0)
{
_current = provider.AppliedMigrations[provider.AppliedMigrations.Count - 1];
}
_goForward = false;
_current = 0;
if (provider.AppliedMigrations.Count > 0) {
_current = provider.AppliedMigrations[provider.AppliedMigrations.Count - 1];
}
_goForward = false;
}

public override long Next
Expand Down Expand Up @@ -62,27 +61,39 @@ public override bool Continue(long version)
public override void Migrate(IMigration migration)
{
_provider.BeginTransaction();
MigrationAttribute attr =
(MigrationAttribute) Attribute.GetCustomAttribute(migration.GetType(), typeof (MigrationAttribute));

if (_provider.AppliedMigrations.Contains(attr.Version))
{
// we're removing this one
_logger.MigrateDown(Current, migration.Name);
migration.Down();
_provider.MigrationUnApplied(attr.Version);
_provider.Commit();
migration.AfterDown();
MigrationAttribute attr = (MigrationAttribute)Attribute.GetCustomAttribute(migration.GetType(), typeof(MigrationAttribute));

if (_provider.AppliedMigrations.Contains(attr.Version)) {
RemoveMigration(migration, attr);
} else {
ApplyMigration(migration, attr);
}
else
}

private void ApplyMigration(IMigration migration, MigrationAttribute attr)
{
// we're adding this one
_logger.MigrateUp(Current, migration.Name);
if(! DryRun)
{
// we're adding this one
_logger.MigrateUp(Current, migration.Name);
migration.Up();
_provider.MigrationApplied(attr.Version);
_provider.Commit();
migration.AfterUp();
}
}

private void RemoveMigration(IMigration migration, MigrationAttribute attr)
{
// we're removing this one
_logger.MigrateDown(Current, migration.Name);
if (! DryRun)
{
migration.Down();
_provider.MigrationUnApplied(attr.Version);
_provider.Commit();
migration.AfterDown();
}
}
}
}
}
12 changes: 11 additions & 1 deletion src/Migrator/Migrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class Migrator
private readonly MigrationLoader _migrationLoader;

private ILogger _logger = new Logger(false);
protected bool _dryrun;
private string[] _args;

public string[] args
Expand Down Expand Up @@ -76,7 +77,8 @@ public List<Type> MigrationsTypes
}

/// <summary>
/// Run all migrations up to the latest.
/// Run all migrations up to the latest. Make no changes to database if
/// dryrun is true.
/// </summary>
public void MigrateToLastVersion()
{
Expand Down Expand Up @@ -104,6 +106,12 @@ public ILogger Logger
}
}

public virtual bool DryRun
{
get { return _dryrun; }
set { _dryrun = value; }
}

/// <summary>
/// Migrate the database to a specific version.
/// Runs all migration between the actual version and the
Expand All @@ -112,6 +120,7 @@ public ILogger Logger
/// the <c>Up()</c> method will be invoked.
/// If <c>version</c> lower then the current version,
/// the <c>Down()</c> method of previous migration will be invoked.
/// If <c>dryrun</c> is set, don't write any changes to the database.
/// </summary>
/// <param name="version">The version that must became the current one</param>
public void MigrateTo(long version)
Expand All @@ -125,6 +134,7 @@ public void MigrateTo(long version)

bool firstRun = true;
BaseMigrate migrate = BaseMigrate.GetInstance(_migrationLoader.GetAvailableMigrations(), _provider, _logger);
migrate.DryRun = DryRun;
Logger.Started(migrate.AppliedVersions, version);

while (migrate.Continue(version))
Expand Down
2 changes: 1 addition & 1 deletion src/config/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("0.7.0.*")]
[assembly: AssemblyVersion("0.8.0.*")]

0 comments on commit 575829d

Please sign in to comment.