Skip to content

Commit d33a96d

Browse files
author
Lars Moastuen
committed
Add support for comparing timestamps with separators by falling back to string comparison when VersionInt() of two migration steps are equal.
1 parent 6e94351 commit d33a96d

2 files changed

Lines changed: 18 additions & 1 deletion

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() != 0:
8585
return m.VersionInt() < other.VersionInt()
8686
case m.isNumeric() && !other.isNumeric():
8787
return true

migrate_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,20 @@ func (s *SqliteMigrateSuite) TestPlanMigrationWithHoles(c *C) {
359359
c.Assert(plannedMigrations[2].Migration.Id, Equals, "2")
360360
c.Assert(plannedMigrations[2].Queries[0], Equals, down)
361361
}
362+
363+
func (s *SqliteMigrateSuite) TestLessTwoNumeric(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+
// 20160126_1100 less than 20160126_1200
372+
c.Assert((Migration{Id: "20160126_1100"}).
373+
Less(&Migration{Id: "20160126_1200"}), Equals, true)
374+
// 20160126_1200 not less than 20160126_1100
375+
c.Assert((Migration{Id: "20160126_1200"}).
376+
Less(&Migration{Id: "20160126_1100"}), Equals, false)
377+
378+
}

0 commit comments

Comments
 (0)