Commit b18237b

unknown <mo@.(none)>
2009-10-10 02:57:46
got the database gateway skipping scripts that have already run.
1 parent bd98514
Changed files (3)
product/application/data/SqlFile.cs
@@ -6,11 +6,15 @@ namespace simple.migrations.Data
     {
         public virtual string path { get; set; }
 
-        public virtual int version() { 
-            throw new NotImplementedException();}
+        public virtual int version()
+        {
+            throw new NotImplementedException();
+        }
 
-        public virtual string name() {
-            throw new NotImplementedException();}
+        public virtual string name()
+        {
+            throw new NotImplementedException();
+        }
 
         static public implicit operator SqlFile(string file_path)
         {
@@ -41,5 +45,10 @@ namespace simple.migrations.Data
         {
             return path;
         }
+
+        public virtual bool is_greater_than(int version)
+        {
+            throw new NotImplementedException();
+        }
     }
 }
\ No newline at end of file
product/application/data/SqlServerDatabaseGateway.cs
@@ -1,3 +1,7 @@
+using System;
+using System.Data;
+using System.Globalization;
+
 namespace simple.migrations.Data
 {
     public class SqlServerDatabaseGateway : DatabaseGateway
@@ -13,7 +17,13 @@ namespace simple.migrations.Data
         {
             using (var command = command_factory.create())
             {
-                command.run(file);
+                foreach (DataRow row in command.run("select * from migration_scripts").Rows)
+                {
+                    var version = (int) Convert.ChangeType(row["version"], typeof (int), CultureInfo.InvariantCulture);
+                    if (!file.is_greater_than(version)) return;
+
+                    command.run(file);
+                }
             }
         }
     }
product/application.tests/data/SqlServerDatabaseGatewaySpecs.cs
@@ -1,4 +1,3 @@
-using System;
 using System.Data;
 using developwithpassion.bdd.contexts;
 using developwithpassion.bdd.harnesses.mbunit;
@@ -16,60 +15,59 @@ namespace tests.data
             context c = () =>
             {
                 command_factory = the_dependency<DatabaseCommandFactory>();
+                first_script = an<SqlFile>();
+                second_script = an<SqlFile>();
+                command = an<DatabaseCommand>();
+
+                var table = new DataTable();
+                var row = table.NewRow();
+                table.Columns.Add("version");
+                row["version"] = "0001";
+                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);
+                first_script.is_told_to(x => x.is_greater_than(1)).it_will_return(false);
+                second_script.is_told_to(x => x.is_greater_than(1)).it_will_return(true);
             };
 
             static protected DatabaseCommandFactory command_factory;
+            static protected SqlFile first_script;
+            static protected SqlFile second_script;
+            static protected DatabaseCommand command;
         }
 
         [Concern(typeof (SqlServerDatabaseGateway))]
         public class when_attempting_to_run_a_migration_script_that_has_already_been_run_against_the_database : concern
         {
-            because b = () => {};
+            because b = () =>
+            {
+                sut.run(first_script);
+            };
 
-            it should_skip_that_script = () => {};
+            it should_skip_that_script = () =>
+            {
+                command.never_received(x => x.run(first_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);
+                sut.run(second_script);
             };
 
             it should_execute_the_sql_from_each_script_against_the_database = () =>
             {
-                command.received(x => x.run(sql_file));
+                command.received(x => x.run(second_script));
             };
 
             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