Skip to content

feat: add check command to check pending migrations #1219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions internal/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
errInvalidSequenceWidth = errors.New("Digits must be positive")
errIncompatibleSeqAndFormat = errors.New("The seq and format options are mutually exclusive")
errInvalidTimeFormat = errors.New("Time format may not be empty")
errPendingMigrations = errors.New("There are pending migrations")
)

func nextSeqVersion(matches []string, seqDigits int) (string, error) {
Expand Down Expand Up @@ -222,6 +223,20 @@ func versionCmd(m *migrate.Migrate) error {
return nil
}

func checkCmd(m *migrate.Migrate) error {
pending, err := m.Check()
if err != nil {
return err
}

log.Printf("%v pending migration(s)\n", pending)
if pending > 0 {
return errPendingMigrations
}

return nil
}

// numDownMigrationsFromArgs returns an int for number of migrations to apply
// and a bool indicating if we need a confirm before applying
func numDownMigrationsFromArgs(applyAll bool, args []string) (int, bool, error) {
Expand Down
10 changes: 10 additions & 0 deletions internal/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Commands:
%s
%s
version Print current migration version
check Check if database has pending migrations

Source drivers: `+strings.Join(source.List(), ", ")+`
Database drivers: `+strings.Join(database.List(), ", ")+"\n", createUsage, gotoUsage, upUsage, downUsage, dropUsage, forceUsage)
Expand Down Expand Up @@ -371,6 +372,15 @@ Database drivers: `+strings.Join(database.List(), ", ")+"\n", createUsage, gotoU
log.fatalErr(err)
}

case "check":
if migraterErr != nil {
log.fatalErr(migraterErr)
}

if err := checkCmd(migrater); err != nil {
log.fatalErr(err)
}

default:
printUsageAndExit()
}
Expand Down
38 changes: 38 additions & 0 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,44 @@ func (m *Migrate) Version() (version uint, dirty bool, err error) {
return suint(v), d, nil
}

// Check returns number of pending migrations.
func (m *Migrate) Check() (pending int, err error) {
// Disable prefetch as we'll only be checking and not applying migrations.
m.PrefetchMigrations = 0

curVersion, dirty, err := m.databaseDrv.Version()
if err != nil {
return 0, err
}

if dirty {
return 0, ErrDirty{curVersion}
}

pending = 0

ret := make(chan interface{}, m.PrefetchMigrations)
go m.readUp(curVersion, -1, ret)

for r := range ret {
if m.stop() {
break
}

switch r := r.(type) {
case error:
if r != ErrNoChange {
return 0, r
}

case *Migration:
pending += 1
}
}

return pending, nil
}

// read reads either up or down migrations from source `from` to `to`.
// Each migration is then written to the ret channel.
// If an error occurs during reading, that error is written to the ret channel, too.
Expand Down
37 changes: 37 additions & 0 deletions migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,43 @@ func TestVersion(t *testing.T) {
}
}

func TestCheck(t *testing.T) {
m, _ := New("stub://", "stub://")
m.sourceDrv.(*sStub.Stub).Migrations = sourceStubMigrations

pending, err := m.Check()
if err != nil {
t.Fatal(err)
}
if pending != 5 {
t.Fatalf("expected 5 pending, got %v", pending)
}

if err := m.Steps(3); err != nil {
t.Fatal(err)
}

pending, err = m.Check()
if err != nil {
t.Fatal(err)
}
if pending != 2 {
t.Fatalf("expected 2 pending, got %v", pending)
}

if err := m.Up(); err != nil {
t.Fatal(err)
}

pending, err = m.Check()
if err != nil {
t.Fatal(err)
}
if pending != 0 {
t.Fatalf("expected 0 pending, got %v", pending)
}
}

func TestRun(t *testing.T) {
m, _ := New("stub://", "stub://")

Expand Down
Loading