diff --git a/migrate.go b/migrate.go index 2152d8e4..1f7672ed 100644 --- a/migrate.go +++ b/migrate.go @@ -680,7 +680,9 @@ func (ms MigrationSet) planMigrationCommon(db *sql.DB, dialect string, m Migrati toApply := ToApply(migrations, record.Id, dir) toApplyCount := len(toApply) - if version >= 0 { + // When a target version is requested but there are no migrations left to + // apply, we are already at that version, so there is nothing to do. + if version >= 0 && len(toApply) > 0 { targetIndex := 0 for targetIndex < len(toApply) { tempVersion := toApply[targetIndex].VersionInt() diff --git a/migrate_test.go b/migrate_test.go index 3d6e80e4..66b5fb71 100644 --- a/migrate_test.go +++ b/migrate_test.go @@ -229,6 +229,22 @@ func (s *SqliteMigrateSuite) TestMigrateVersionIntFailedWithInvalidVerion(c *C) c.Assert(err, NotNil) } +func (s *SqliteMigrateSuite) TestMigrateVersionIntAlreadyAtVersion(c *C) { + migrations := &FileMigrationSource{ + Dir: "test-migrations", + } + + // Migrate up to the latest version. + n, err := ExecVersion(s.Db, "sqlite3", migrations, Up, 2) + c.Assert(err, IsNil) + c.Assert(n, Equals, 2) + + // Requesting the same version again has nothing to apply and must not error. + n, err = ExecVersion(s.Db, "sqlite3", migrations, Up, 2) + c.Assert(err, IsNil) + c.Assert(n, Equals, 0) +} + func (s *SqliteMigrateSuite) TestMigrateDown(c *C) { migrations := &FileMigrationSource{ Dir: "test-migrations",