From 6e94351c911370d6db800f6478a039c99c02f93d Mon Sep 17 00:00:00 2001 From: Lars Moastuen Date: Tue, 1 Mar 2016 14:09:17 +0100 Subject: [PATCH 1/4] Generalise location of test database file by using ioutil.TempFile(). --- migrate_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/migrate_test.go b/migrate_test.go index cbe72495..79b2ae72 100644 --- a/migrate_test.go +++ b/migrate_test.go @@ -2,6 +2,7 @@ package migrate import ( "database/sql" + "io/ioutil" "os" _ "github.com/mattn/go-sqlite3" @@ -9,7 +10,7 @@ import ( "gopkg.in/gorp.v1" ) -var filename = "/tmp/sql-migrate-sqlite.db" +var testDatabaseFile *os.File var sqliteMigrations = []*Migration{ &Migration{ Id: "123", @@ -31,7 +32,10 @@ type SqliteMigrateSuite struct { var _ = Suite(&SqliteMigrateSuite{}) func (s *SqliteMigrateSuite) SetUpTest(c *C) { - db, err := sql.Open("sqlite3", filename) + var err error + testDatabaseFile, err = ioutil.TempFile("", "sql-migrate-sqlite") + c.Assert(err, IsNil) + db, err := sql.Open("sqlite3", testDatabaseFile.Name()) c.Assert(err, IsNil) s.Db = db @@ -39,7 +43,7 @@ func (s *SqliteMigrateSuite) SetUpTest(c *C) { } func (s *SqliteMigrateSuite) TearDownTest(c *C) { - err := os.Remove(filename) + err := os.Remove(testDatabaseFile.Name()) c.Assert(err, IsNil) } From d33a96d815faba897332c9d70f6430a11bab2e99 Mon Sep 17 00:00:00 2001 From: Lars Moastuen Date: Tue, 1 Mar 2016 14:21:33 +0100 Subject: [PATCH 2/4] Add support for comparing timestamps with separators by falling back to string comparison when VersionInt() of two migration steps are equal. --- migrate.go | 2 +- migrate_test.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/migrate.go b/migrate.go index 861bbf67..1958c9b1 100644 --- a/migrate.go +++ b/migrate.go @@ -81,7 +81,7 @@ type Migration struct { func (m Migration) Less(other *Migration) bool { switch { - case m.isNumeric() && other.isNumeric(): + case m.isNumeric() && other.isNumeric() && m.VersionInt()-other.VersionInt() != 0: return m.VersionInt() < other.VersionInt() case m.isNumeric() && !other.isNumeric(): return true diff --git a/migrate_test.go b/migrate_test.go index 79b2ae72..253950e4 100644 --- a/migrate_test.go +++ b/migrate_test.go @@ -359,3 +359,20 @@ func (s *SqliteMigrateSuite) TestPlanMigrationWithHoles(c *C) { c.Assert(plannedMigrations[2].Migration.Id, Equals, "2") c.Assert(plannedMigrations[2].Queries[0], Equals, down) } + +func (s *SqliteMigrateSuite) TestLessTwoNumeric(c *C) { + c.Assert((Migration{Id: "1"}).Less(&Migration{Id: "2"}), Equals, true) // 1 less than 2 + c.Assert((Migration{Id: "2"}).Less(&Migration{Id: "1"}), Equals, false) // 2 not less than 1 + c.Assert((Migration{Id: "1"}).Less(&Migration{Id: "a"}), Equals, true) // 1 less than a + c.Assert((Migration{Id: "a"}).Less(&Migration{Id: "1"}), Equals, false) // a not less than 1 + c.Assert((Migration{Id: "a"}).Less(&Migration{Id: "a"}), Equals, false) // a not less than a + c.Assert((Migration{Id: "1-a"}).Less(&Migration{Id: "1-b"}), Equals, true) // 1-a less than 1-b + c.Assert((Migration{Id: "1-b"}).Less(&Migration{Id: "1-a"}), Equals, false) // 1-b not less than 1-a + // 20160126_1100 less than 20160126_1200 + c.Assert((Migration{Id: "20160126_1100"}). + Less(&Migration{Id: "20160126_1200"}), Equals, true) + // 20160126_1200 not less than 20160126_1100 + c.Assert((Migration{Id: "20160126_1200"}). + Less(&Migration{Id: "20160126_1100"}), Equals, false) + +} From 3c91f414efb2873774e29e2ce2fd480b26485a80 Mon Sep 17 00:00:00 2001 From: Lars Moastuen Date: Tue, 1 Mar 2016 14:35:34 +0100 Subject: [PATCH 3/4] Improve readability for not-equal VersionInt() check --- migrate.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrate.go b/migrate.go index 1958c9b1..1aef15c1 100644 --- a/migrate.go +++ b/migrate.go @@ -81,7 +81,7 @@ type Migration struct { func (m Migration) Less(other *Migration) bool { switch { - case m.isNumeric() && other.isNumeric() && m.VersionInt()-other.VersionInt() != 0: + case m.isNumeric() && other.isNumeric() && m.VersionInt() != other.VersionInt(): return m.VersionInt() < other.VersionInt() case m.isNumeric() && !other.isNumeric(): return true From 6831c511880ff8ec6b1d618532709c684e9d40b1 Mon Sep 17 00:00:00 2001 From: Lars Moastuen Date: Tue, 1 Mar 2016 14:40:26 +0100 Subject: [PATCH 4/4] Add more test cases for Migration.Less() --- migrate_test.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/migrate_test.go b/migrate_test.go index 253950e4..4d573747 100644 --- a/migrate_test.go +++ b/migrate_test.go @@ -360,14 +360,18 @@ func (s *SqliteMigrateSuite) TestPlanMigrationWithHoles(c *C) { c.Assert(plannedMigrations[2].Queries[0], Equals, down) } -func (s *SqliteMigrateSuite) TestLessTwoNumeric(c *C) { - c.Assert((Migration{Id: "1"}).Less(&Migration{Id: "2"}), Equals, true) // 1 less than 2 - c.Assert((Migration{Id: "2"}).Less(&Migration{Id: "1"}), Equals, false) // 2 not less than 1 - c.Assert((Migration{Id: "1"}).Less(&Migration{Id: "a"}), Equals, true) // 1 less than a - c.Assert((Migration{Id: "a"}).Less(&Migration{Id: "1"}), Equals, false) // a not less than 1 - c.Assert((Migration{Id: "a"}).Less(&Migration{Id: "a"}), Equals, false) // a not less than a - c.Assert((Migration{Id: "1-a"}).Less(&Migration{Id: "1-b"}), Equals, true) // 1-a less than 1-b - c.Assert((Migration{Id: "1-b"}).Less(&Migration{Id: "1-a"}), Equals, false) // 1-b not less than 1-a +func (s *SqliteMigrateSuite) TestLess(c *C) { + c.Assert((Migration{Id: "1"}).Less(&Migration{Id: "2"}), Equals, true) // 1 less than 2 + c.Assert((Migration{Id: "2"}).Less(&Migration{Id: "1"}), Equals, false) // 2 not less than 1 + c.Assert((Migration{Id: "1"}).Less(&Migration{Id: "a"}), Equals, true) // 1 less than a + c.Assert((Migration{Id: "a"}).Less(&Migration{Id: "1"}), Equals, false) // a not less than 1 + c.Assert((Migration{Id: "a"}).Less(&Migration{Id: "a"}), Equals, false) // a not less than a + c.Assert((Migration{Id: "1-a"}).Less(&Migration{Id: "1-b"}), Equals, true) // 1-a less than 1-b + c.Assert((Migration{Id: "1-b"}).Less(&Migration{Id: "1-a"}), Equals, false) // 1-b not less than 1-a + c.Assert((Migration{Id: "1"}).Less(&Migration{Id: "10"}), Equals, true) // 1 less than 10 + c.Assert((Migration{Id: "10"}).Less(&Migration{Id: "1"}), Equals, false) // 10 not less than 1 + c.Assert((Migration{Id: "1_foo"}).Less(&Migration{Id: "10_bar"}), Equals, true) // 1_foo not less than 1 + c.Assert((Migration{Id: "10_bar"}).Less(&Migration{Id: "1_foo"}), Equals, false) // 10 not less than 1 // 20160126_1100 less than 20160126_1200 c.Assert((Migration{Id: "20160126_1100"}). Less(&Migration{Id: "20160126_1200"}), Equals, true)