Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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():
return m.VersionInt() < other.VersionInt()
case m.isNumeric() && !other.isNumeric():
return true
Expand Down
31 changes: 28 additions & 3 deletions migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package migrate

import (
"database/sql"
"io/ioutil"
"os"

_ "github.com/mattn/go-sqlite3"
. "gopkg.in/check.v1"
"gopkg.in/gorp.v1"
)

var filename = "/tmp/sql-migrate-sqlite.db"
var testDatabaseFile *os.File
var sqliteMigrations = []*Migration{
&Migration{
Id: "123",
Expand All @@ -31,15 +32,18 @@ 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
s.DbMap = &gorp.DbMap{Db: db, Dialect: &gorp.SqliteDialect{}}
}

func (s *SqliteMigrateSuite) TearDownTest(c *C) {
err := os.Remove(filename)
err := os.Remove(testDatabaseFile.Name())
c.Assert(err, IsNil)
}

Expand Down Expand Up @@ -355,3 +359,24 @@ 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) 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)
// 20160126_1200 not less than 20160126_1100
c.Assert((Migration{Id: "20160126_1200"}).
Less(&Migration{Id: "20160126_1100"}), Equals, false)

}