Skip to content

Commit

Permalink
all tests really passing now
Browse files Browse the repository at this point in the history
  • Loading branch information
schambers committed Aug 27, 2010
1 parent 9833af9 commit 85622d6
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,43 @@ namespace FluentMigrator.Runner.Processors.SqlServer
{
public class SqlServerProcessor : ProcessorBase
{
public virtual SqlConnection Connection { get; set; }
public SqlTransaction Transaction { get; private set; }

public virtual SqlConnection Connection { get; set; }
public SqlTransaction Transaction { get; private set; }
public bool WasCommitted { get; private set; }

public SqlServerProcessor(SqlConnection connection, IMigrationGenerator generator, IAnnouncer announcer, IMigrationProcessorOptions options)
: base(generator, announcer, options)
{
Connection = connection;
connection.Open();
Transaction = connection.BeginTransaction();
}

}

public override bool SchemaExists(string schemaName)
{
return Exists("SELECT * FROM SYS.SCHEMAS WHERE NAME = '{0}'", schemaName);
}

}

public override bool TableExists(string tableName)
{
return Exists("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{0}'", tableName);
}

}

public override bool ColumnExists(string tableName, string columnName)
{
return Exists("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{0}' AND COLUMN_NAME = '{1}'", tableName, columnName);
}

}

public override bool ConstraintExists(string tableName, string constraintName)
{
return Exists("SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_CATALOG = DB_NAME() AND TABLE_NAME = '{0}' AND CONSTRAINT_NAME = '{1}'", tableName, constraintName);
}

}

public override void Execute(string template, params object[] args)
{
Process(String.Format(template, args));
}

}

public override bool Exists(string template, params object[] args)
{
if (Connection.State != ConnectionState.Open)
Expand All @@ -73,13 +74,13 @@ public override bool Exists(string template, params object[] args)
{
return reader.Read();
}
}

}

public override DataSet ReadTableData(string tableName)
{
return Read("SELECT * FROM {0}", tableName);
}

}

public override DataSet Read(string template, params object[] args)
{
if (Connection.State != ConnectionState.Open) Connection.Open();
Expand All @@ -91,18 +92,19 @@ public override DataSet Read(string template, params object[] args)
adapter.Fill(ds);
return ds;
}
}

}

public override void BeginTransaction()
{
Announcer.Say( "Beginning Transaction" );
Transaction = Connection.BeginTransaction();
}

}

public override void CommitTransaction()
{
Announcer.Say("Commiting Transaction");
Transaction.Commit();
WasCommitted = true;
if (Connection.State != ConnectionState.Closed)
{
Connection.Close();
Expand All @@ -113,6 +115,7 @@ public override void RollbackTransaction()
{
Announcer.Say("Rolling back transaction");
Transaction.Rollback();
WasCommitted = true;
if (Connection.State != ConnectionState.Closed)
{
Connection.Close();
Expand Down
1 change: 0 additions & 1 deletion src/FluentMigrator.Tests/FluentMigrator.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@
<Compile Include="Integration\Migrations\Interleaved\Pass3\2_UserEmail.cs" />
<Compile Include="Integration\Migrations\Invalid\InvalidMigration.cs" />
<Compile Include="Integration\Migrations\Invalid\InvalidMigration2.cs" />
<Compile Include="Integration\MigrationVersionRunnerTests.cs" />
<Compile Include="Integration\Processors\FileProcessorTests.cs" />
<Compile Include="Integration\VersionMigrationTests.cs" />
<Compile Include="Unit\AnnouncerTests.cs" />
Expand Down
5 changes: 5 additions & 0 deletions src/FluentMigrator.Tests/Integration/IntegrationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ protected static void ExecuteWithSqlServer(Action<IMigrationProcessor> test, Int
var connection = new SqlConnection(serverOptions.ConnectionString);
var processor = new SqlServerProcessor(connection, new SqlServer2000Generator(), new TextWriterAnnouncer(System.Console.Out), new ProcessorOptions());
test(processor);

if (!processor.WasCommitted)
{
processor.RollbackTransaction();
}
}

protected static void ExecuteWithSqlite(Action<IMigrationProcessor> test, IntegrationTestOptions.DatabaseServerOptions serverOptions)
Expand Down
106 changes: 103 additions & 3 deletions src/FluentMigrator.Tests/Integration/MigrationRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
#endregion

using System;
using System.Data.SqlClient;
using System.Reflection;
using FluentMigrator.Expressions;
using FluentMigrator.Runner;
using FluentMigrator.Runner.Announcers;
using FluentMigrator.Runner.Generators;
using FluentMigrator.Runner.Initialization;
using FluentMigrator.Runner.Processors;
using FluentMigrator.Runner.Processors.SqlServer;
using FluentMigrator.Tests.Integration.Migrations;
using FluentMigrator.Tests.Integration.Migrations.Invalid;
using Moq;
using NUnit.Framework;
using NUnit.Should;
Expand Down Expand Up @@ -58,7 +64,7 @@ public void CanRunMigration()
processor.TableExists("TestTable").ShouldBeTrue();
// This is a hack until MigrationVersionRunner and MigrationRunner are refactored and merged together
processor.CommitTransaction();
//processor.CommitTransaction();
runner.Down(new TestCreateAndDropTableMigration());
processor.TableExists("TestTable").ShouldBeFalse();
Expand Down Expand Up @@ -97,7 +103,7 @@ public void CanApplyForeignKeyConvention()
runner.Up(new TestForeignKeyNamingConvention());
// This is a hack until MigrationVersionRunner and MigrationRunner are refactored and merged together
processor.CommitTransaction();
//processor.CommitTransaction();
processor.ConstraintExists( "Users", "FK_Users_GroupId_Groups_GroupId" ).ShouldBeTrue();
runner.Down( new TestForeignKeyNamingConvention() );
Expand All @@ -118,8 +124,102 @@ public void CanApplyIndexConvention()
runner.Down(new TestIndexNamingConvention());
processor.TableExists("Users").ShouldBeFalse();
processor.CommitTransaction();
//processor.CommitTransaction();
});
}

[Test]
public void CanLoadMigrations()
{
ExecuteWithSupportedProcessors( processor =>
{
var runnerContext = new RunnerContext( new TextWriterAnnouncer( System.Console.Out ) )
{
Namespace = typeof( TestMigration ).Namespace,
};
var runner = new MigrationRunner( typeof( MigrationRunnerTests ).Assembly, runnerContext, processor );
//runner.Processor.CommitTransaction();
runner.MigrationLoader.Migrations.ShouldNotBeNull();
} );
}

[Test]
public void CanLoadVersion()
{
ExecuteWithSupportedProcessors( processor =>
{
var runnerContext = new RunnerContext( new TextWriterAnnouncer( System.Console.Out ) )
{
Namespace = typeof( TestMigration ).Namespace,
};
var runner = new MigrationRunner( typeof( TestMigration ).Assembly, runnerContext, processor );
//runner.Processor.CommitTransaction();
runner.VersionLoader.VersionInfo.ShouldNotBeNull();
} );
}

[Test]
public void CanRunMigrations()
{
ExecuteWithSupportedProcessors( processor =>
{
Assembly asm = typeof( MigrationRunnerTests ).Assembly;
var runnerContext = new RunnerContext( new TextWriterAnnouncer( System.Console.Out ) )
{
Namespace = "FluentMigrator.Tests.Integration.Migrations"
};
var runner = new MigrationRunner( asm, runnerContext, processor );
runner.MigrateUp();
runner.VersionLoader.VersionInfo.HasAppliedMigration( 1 ).ShouldBeTrue();
runner.VersionLoader.VersionInfo.HasAppliedMigration( 2 ).ShouldBeTrue();
runner.VersionLoader.VersionInfo.Latest().ShouldBe( 2 );
});
}

[Test]
public void CanMigrateASpecificVersion()
{
ExecuteWithSupportedProcessors( processor =>
{
Assembly asm = typeof( MigrationRunnerTests ).Assembly;
var runnerContext = new RunnerContext( new TextWriterAnnouncer( System.Console.Out ) );
runnerContext.Namespace = "FluentMigrator.Tests.Integration.Migrations";
var runner = new MigrationRunner( asm, runnerContext, processor );
runner.MigrateUp( 1 );
runner.VersionLoader.VersionInfo.HasAppliedMigration( 1 ).ShouldBeTrue();
processor.TableExists( "Users" ).ShouldBeTrue();
});
}

[Test]
public void CanMigrateASpecificVersionDown()
{
ExecuteWithSupportedProcessors( processor =>
{
Assembly asm = typeof( MigrationRunnerTests ).Assembly;
var runnerContext = new RunnerContext( new TextWriterAnnouncer( System.Console.Out ) );
runnerContext.Namespace = "FluentMigrator.Tests.Integration.Migrations";
var runner = new MigrationRunner( asm, runnerContext, processor );
runner.MigrateUp( 1 );
runner.VersionLoader.VersionInfo.HasAppliedMigration( 1 ).ShouldBeTrue();
processor.TableExists( "Users" ).ShouldBeTrue();
runner.MigrateDown( 1 );
runner.VersionLoader.VersionInfo.HasAppliedMigration( 1 ).ShouldBeFalse();
processor.TableExists( "Users" ).ShouldBeFalse();
} );
}
}

Expand Down
Loading

0 comments on commit 85622d6

Please sign in to comment.