From d8ad40173a05a9441dc5b8bc4ed2377edbc2b524 Mon Sep 17 00:00:00 2001 From: "Chris (ChrisJr404)" <11917633+ChrisJr404@users.noreply.github.com> Date: Tue, 25 Aug 2026 11:48:09 -0400 Subject: [PATCH] Don't error when target version has no migrations left to apply When a version is passed to ExecVersion/PlanMigrationToVersion and the database is already at that version, there are no migrations left to apply, but planning returned a misleading "unknown migration with version id" error instead of a successful no-op. Skip the target lookup when nothing is left to apply so re-running against the current version succeeds. --- migrate.go | 4 +++- migrate_test.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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",