Commit bd98514
Changed files (7)
product
application
application.tests
product/application/data/DatabaseCommand.cs
@@ -0,0 +1,11 @@
+using System;
+using System.Data;
+
+namespace simple.migrations.Data
+{
+ public interface DatabaseCommand : IDisposable
+ {
+ DataTable run(string sql);
+ void run(SqlFile sql);
+ }
+}
\ No newline at end of file
product/application/data/DatabaseCommandFactory.cs
@@ -0,0 +1,7 @@
+namespace simple.migrations.Data
+{
+ public interface DatabaseCommandFactory
+ {
+ DatabaseCommand create();
+ }
+}
\ No newline at end of file
product/application/data/SqlFile.cs
@@ -1,9 +1,17 @@
+using System;
+
namespace simple.migrations.Data
{
public class SqlFile
{
public virtual string path { get; set; }
+ public virtual int version() {
+ throw new NotImplementedException();}
+
+ public virtual string name() {
+ throw new NotImplementedException();}
+
static public implicit operator SqlFile(string file_path)
{
return new SqlFile {path = file_path.ToLower()};
product/application/data/SqlServerDatabaseGateway.cs
@@ -0,0 +1,20 @@
+namespace simple.migrations.Data
+{
+ public class SqlServerDatabaseGateway : DatabaseGateway
+ {
+ DatabaseCommandFactory command_factory;
+
+ public SqlServerDatabaseGateway(DatabaseCommandFactory command_factory)
+ {
+ this.command_factory = command_factory;
+ }
+
+ public void run(SqlFile file)
+ {
+ using (var command = command_factory.create())
+ {
+ command.run(file);
+ }
+ }
+ }
+}
\ No newline at end of file
product/application/application.csproj
@@ -52,9 +52,12 @@
<Compile Include="ConsoleArguments.cs" />
<Compile Include="ConsoleArgumentsCommandRegistry.cs" />
<Compile Include="ConsoleCommand.cs" />
+ <Compile Include="data\DatabaseCommand.cs" />
+ <Compile Include="data\DatabaseCommandFactory.cs" />
<Compile Include="data\DatabaseGateway.cs" />
<Compile Include="data\DatabaseGatewayFactory.cs" />
<Compile Include="data\SqlFile.cs" />
+ <Compile Include="data\SqlServerDatabaseGateway.cs" />
<Compile Include="HelpCommand.cs" />
<Compile Include="io\FileSystem.cs" />
<Compile Include="io\WindowsFileSystem.cs" />
product/application.tests/data/SqlServerDatabaseGatewaySpecs.cs
@@ -0,0 +1,75 @@
+using System;
+using System.Data;
+using developwithpassion.bdd.contexts;
+using developwithpassion.bdd.harnesses.mbunit;
+using developwithpassion.bdd.mocking.rhino;
+using developwithpassion.bdddoc.core;
+using simple.migrations.Data;
+using tests.helpers;
+
+namespace tests.data
+{
+ public class SqlServerDatabaseGatewaySpecs
+ {
+ public abstract class concern : observations_for_a_sut_with_a_contract<DatabaseGateway, SqlServerDatabaseGateway>
+ {
+ context c = () =>
+ {
+ command_factory = the_dependency<DatabaseCommandFactory>();
+ };
+
+ static protected DatabaseCommandFactory command_factory;
+ }
+
+ [Concern(typeof (SqlServerDatabaseGateway))]
+ public class when_attempting_to_run_a_migration_script_that_has_already_been_run_against_the_database : concern
+ {
+ because b = () => {};
+
+ it should_skip_that_script = () => {};
+ }
+
+ [Concern(typeof (SqlServerDatabaseGateway))]
+ public class when_running_a_bunch_of_migrations_scripts_against_a_database : concern
+ {
+ context c = () =>
+ {
+ sql_file = an<SqlFile>();
+ command = an<DatabaseCommand>();
+
+ var table = new DataTable();
+ var row = table.NewRow();
+ table.Columns.Add("version");
+ table.Columns.Add("script_name");
+ table.Columns.Add("run_on");
+ row["version"] = "0001";
+ row["script_name"] = "0001_hello_world.sql";
+ row["run_on"] = DateTime.Now;
+ table.Rows.Add(row);
+
+ command_factory.is_told_to(x => x.create()).it_will_return(command);
+ command.is_told_to(x => x.run("select * from migration_scripts")).it_will_return(table);
+ sql_file.is_told_to(x => x.version()).it_will_return(2);
+ sql_file.is_told_to(x => x.name()).it_will_return("0002_another_script.sql");
+ };
+
+ because b = () =>
+ {
+ sut.run(sql_file);
+ };
+
+ it should_execute_the_sql_from_each_script_against_the_database = () =>
+ {
+ command.received(x => x.run(sql_file));
+ };
+
+ it should_close_the_connection_to_the_database_when_finished = () =>
+ {
+ command.received(x => x.Dispose());
+ };
+
+ static SqlFile sql_file;
+ static DatabaseCommand command;
+ }
+ }
+}
\ No newline at end of file
product/application.tests/application.tests.csproj
@@ -72,6 +72,7 @@
<Compile Include="CommandRegistrySpecs.cs" />
<Compile Include="ConsoleArgumentsSpecs.cs" />
<Compile Include="ConsoleSpecs.cs" />
+ <Compile Include="data\SqlServerDatabaseGatewaySpecs.cs" />
<Compile Include="EmptyTestFixture.cs" />
<Compile Include="helpers\RhinoStubbingExtensions.cs" />
<Compile Include="io\WindowsFileSystemSpecs.cs" />
@@ -95,9 +96,6 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
- <ItemGroup>
- <Folder Include="data\" />
- </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.