Skip to content

Commit 1911909

Browse files
authored
Merge pull request #25 from larsmoa/master
Support timestamp 'versions' by falling back to string comparison when to VersionInts() are equal
2 parents 51aa4ca + 6831c51 commit 1911909

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

migrate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ type Migration struct {
8181

8282
func (m Migration) Less(other *Migration) bool {
8383
switch {
84-
case m.isNumeric() && other.isNumeric():
84+
case m.isNumeric() && other.isNumeric() && m.VersionInt() != other.VersionInt():
8585
return m.VersionInt() < other.VersionInt()
8686
case m.isNumeric() && !other.isNumeric():
8787
return true

migrate_test.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package migrate
22

33
import (
44
"database/sql"
5+
"io/ioutil"
56
"os"
67

78
_ "github.com/mattn/go-sqlite3"
89
. "gopkg.in/check.v1"
910
"gopkg.in/gorp.v1"
1011
)
1112

12-
var filename = "/tmp/sql-migrate-sqlite.db"
13+
var testDatabaseFile *os.File
1314
var sqliteMigrations = []*Migration{
1415
&Migration{
1516
Id: "123",
@@ -31,15 +32,18 @@ type SqliteMigrateSuite struct {
3132
var _ = Suite(&SqliteMigrateSuite{})
3233

3334
func (s *SqliteMigrateSuite) SetUpTest(c *C) {
34-
db, err := sql.Open("sqlite3", filename)
35+
var err error
36+
testDatabaseFile, err = ioutil.TempFile("", "sql-migrate-sqlite")
37+
c.Assert(err, IsNil)
38+
db, err := sql.Open("sqlite3", testDatabaseFile.Name())
3539
c.Assert(err, IsNil)
3640

3741
s.Db = db
3842
s.DbMap = &gorp.DbMap{Db: db, Dialect: &gorp.SqliteDialect{}}
3943
}
4044

4145
func (s *SqliteMigrateSuite) TearDownTest(c *C) {
42-
err := os.Remove(filename)
46+
err := os.Remove(testDatabaseFile.Name())
4347
c.Assert(err, IsNil)
4448
}
4549

@@ -355,3 +359,24 @@ func (s *SqliteMigrateSuite) TestPlanMigrationWithHoles(c *C) {
355359
c.Assert(plannedMigrations[2].Migration.Id, Equals, "2")
356360
c.Assert(plannedMigrations[2].Queries[0], Equals, down)
357361
}
362+
363+
func (s *SqliteMigrateSuite) TestLess(c *C) {
364+
c.Assert((Migration{Id: "1"}).Less(&Migration{Id: "2"}), Equals, true) // 1 less than 2
365+
c.Assert((Migration{Id: "2"}).Less(&Migration{Id: "1"}), Equals, false) // 2 not less than 1
366+
c.Assert((Migration{Id: "1"}).Less(&Migration{Id: "a"}), Equals, true) // 1 less than a
367+
c.Assert((Migration{Id: "a"}).Less(&Migration{Id: "1"}), Equals, false) // a not less than 1
368+
c.Assert((Migration{Id: "a"}).Less(&Migration{Id: "a"}), Equals, false) // a not less than a
369+
c.Assert((Migration{Id: "1-a"}).Less(&Migration{Id: "1-b"}), Equals, true) // 1-a less than 1-b
370+
c.Assert((Migration{Id: "1-b"}).Less(&Migration{Id: "1-a"}), Equals, false) // 1-b not less than 1-a
371+
c.Assert((Migration{Id: "1"}).Less(&Migration{Id: "10"}), Equals, true) // 1 less than 10
372+
c.Assert((Migration{Id: "10"}).Less(&Migration{Id: "1"}), Equals, false) // 10 not less than 1
373+
c.Assert((Migration{Id: "1_foo"}).Less(&Migration{Id: "10_bar"}), Equals, true) // 1_foo not less than 1
374+
c.Assert((Migration{Id: "10_bar"}).Less(&Migration{Id: "1_foo"}), Equals, false) // 10 not less than 1
375+
// 20160126_1100 less than 20160126_1200
376+
c.Assert((Migration{Id: "20160126_1100"}).
377+
Less(&Migration{Id: "20160126_1200"}), Equals, true)
378+
// 20160126_1200 not less than 20160126_1100
379+
c.Assert((Migration{Id: "20160126_1200"}).
380+
Less(&Migration{Id: "20160126_1100"}), Equals, false)
381+
382+
}

0 commit comments

Comments
 (0)