fix: apply pending migrations without version arithmetic

This commit was merged in pull request #1.
This commit is contained in:
end committed 2026-10-06 00:08:57 -07:00
1 parent 8b5364d898
commit 4ca353dd7e
5 files changed
+430 -21

No files matched your search

+38 -21
View File
@@ -2,6 +2,7 @@ package migrate
import (
"context"
"errors"
"fmt"
"log/slog"
"time"
@@ -9,7 +10,11 @@ import (
"github.com/pressly/goose/v3"
)
// ApplyPendingMigrations applies pending migrations. pendingCount: 0 none, <0 auto-detect, >0 cap.
// ApplyPendingMigrations applies pending migrations and returns the number successfully applied.
// A zero pendingCount is a no-op, a negative value applies all pending migrations,
// and a positive value caps successful applications. Exhausting pending migrations
// is successful even when fewer than the requested cap remain. Versions are identifiers,
// not counts, and may contain gaps or timestamps.
func ApplyPendingMigrations(ctx context.Context, pendingCount int64) (int64, error) {
migrationsModule.RequireLoaded()
p, err := Provider()
@@ -17,29 +22,34 @@ func ApplyPendingMigrations(ctx context.Context, pendingCount int64) (int64, err
return 0, err
}
if pendingCount == 0 {
return 0, nil
}
count, _, err := applyPendingMigrations(ctx, p, migrationsModule.Logger(), pendingCount)
return count, err
}
if pendingCount < 0 {
curr, target, err := p.GetVersions(ctx)
if err != nil {
return 0, err
}
pendingCount = target - curr
}
type migrationStepper interface {
UpByOne(context.Context) (*goose.MigrationResult, error)
}
var count int64
for range pendingCount {
// The last successful version is used for summaries; it cannot be inferred from count.
func applyPendingMigrations(ctx context.Context, p migrationStepper, logger *slog.Logger, pendingCount int64) (count, lastVersion int64, err error) {
for pendingCount < 0 || count < pendingCount {
res, err := p.UpByOne(ctx)
if err := handleMigrationResults(migrationsModule.Logger(), res, err); err != nil {
return count, err
// A migration itself can fail with ErrNoNextVersion. Goose reports that
// through PartialError, which must not be mistaken for normal exhaustion.
var partial *goose.PartialError
if res == nil && errors.Is(err, goose.ErrNoNextVersion) && !errors.As(err, &partial) {
return count, lastVersion, nil
}
if res != nil && res.Error == nil {
count++
if err := handleMigrationResults(logger, res, err); err != nil {
return count, lastVersion, err
}
if res == nil {
return count, lastVersion, nil
}
count++
lastVersion = res.Source.Version
}
return count, nil
return count, lastVersion, nil
}
// MigrateToBlank rolls back all applied migrations.
@@ -102,17 +112,24 @@ func AutoMigrate(ctx context.Context) error {
migrationsModule.Logger().Info("Pending migrations detected, applying them...", fields...)
now := time.Now()
count, err := ApplyPendingMigrations(ctx, target-current)
count, lastVersion, err := applyPendingMigrations(ctx, p, migrationsModule.Logger(), -1)
if count > 0 {
current = lastVersion
}
if err != nil {
migrationsModule.Logger().Error("Couldn't apply pending migrations", "current", current+count, "target", target, "err", err)
migrationsModule.Logger().Error("Couldn't apply pending migrations", "current", current, "target", target, "appliedCount", count, "err", err)
return err
}
migrationsModule.Logger().Info("Applied pending migrations", "current", target, "appliedCount", count, "duration", time.Since(now))
migrationsModule.Logger().Info("Applied pending migrations", "current", current, "appliedCount", count, "duration", time.Since(now))
return nil
}
func handleMigrationResults(logger *slog.Logger, res *goose.MigrationResult, err error) error {
if err != nil {
var partial *goose.PartialError
if errors.As(err, &partial) && partial.Failed != nil {
_ = handleMigrationResults(logger, partial.Failed, nil)
}
return err
}
if res == nil {