Skip to content
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

adds a 'status' command that checks for pending migrations. #30

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
1 change: 1 addition & 0 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Commands:
create - create a new migration in %s with the provided name
migrate - run any migrations that haven't been run yet
rollback - roll back the previous run batch of migrations
status - check if there are pending migrations
help - print this help text

Examples:
Expand Down
33 changes: 21 additions & 12 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,11 @@ func Register(name string, up, down func(orm.DB) error, opts MigrationOptions) {
}

func migrate(db *pg.DB) (err error) {
// sort the registered migrations by name (which will sort by the
// timestamp in their names)
sort.Slice(migrations, func(i, j int) bool {
return migrations[i].Name < migrations[j].Name
})

// look at the migrations table to see the already run migrations
completed, err := getCompletedMigrations(db)
uncompleted, err := getUncompletedMigrations(db)
if err != nil {
return err
}

// diff the completed migrations from the registered migrations to find
// the migrations we still need to run
uncompleted := filterMigrations(migrations, completed, false)

// if there are no migrations that need to be run, exit early
if len(uncompleted) == 0 {
fmt.Println("Migrations already up to date")
Expand Down Expand Up @@ -91,6 +80,26 @@ func migrate(db *pg.DB) (err error) {
return nil
}

func getUncompletedMigrations(db orm.DB) ([]migration, error) {
// sort the registered migrations by name (which will sort by the
// timestamp in their names)
sort.Slice(migrations, func(i, j int) bool {
return migrations[i].Name < migrations[j].Name
})

// look at the migrations table to see the already run migrations
completed, err := getCompletedMigrations(db)
if err != nil {
return nil, err
}

// diff the completed migrations from the registered migrations to find
// the migrations we still need to run
uncompleted := filterMigrations(migrations, completed, false)

return uncompleted, nil
}

func getCompletedMigrations(db orm.DB) ([]migration, error) {
var completed []migration

Expand Down
7 changes: 7 additions & 0 deletions migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ func Run(db *pg.DB, directory string, args []string) error {
}

return rollback(db)
case "status":
err := ensureMigrationTables(db)
if err != nil {
return err
}

return migrationStatus(db)
default:
help(directory)
return nil
Expand Down
34 changes: 34 additions & 0 deletions status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package migrations

import (
"fmt"

"github.com/go-pg/pg/v10"
)

func migrationStatus(db *pg.DB) error {
uncompleted, err := getUncompletedMigrations(db)
if err != nil {
return nil
}

if len(uncompleted) == 0 {
fmt.Println("Migrations already up to date")
return nil
}

return ErrPendingMigrations{len(uncompleted)}
}

// ErrPendingMigrations is returned by the 'status' command when there is at
// least one pending migration
type ErrPendingMigrations struct {
N int
}

func (p ErrPendingMigrations) Error() string {
if p.N == 1 {
return "1 migration is pending"
}
return fmt.Sprintf("%d migrations are pending", p.N)
}
58 changes: 58 additions & 0 deletions status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package migrations

import (
"os"
"testing"

"github.com/go-pg/pg/v10"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMigrationStatus(t *testing.T) {
db := pg.Connect(&pg.Options{
Addr: "localhost:5432",
User: os.Getenv("TEST_DATABASE_USER"),
Database: os.Getenv("TEST_DATABASE_NAME"),
})

err := ensureMigrationTables(db)
require.Nil(t, err)

defer clearMigrations(t, db)
defer resetMigrations(t)

t.Run("Returns nil if migrations are up to date", func(tt *testing.T) {
clearMigrations(tt, db)
resetMigrations(tt)

migrations = []migration{
{Name: "123", Up: noopMigration, Down: noopMigration},
}

_, err := db.Model(&migrations[0]).Insert()
assert.Nil(tt, err)

pendingErr := migrationStatus(db)
assert.Nil(tt, pendingErr)
})

t.Run("Returns an error if migrations are not up to date", func(tt *testing.T) {
clearMigrations(tt, db)
resetMigrations(tt)

migrations = []migration{
{Name: "123", Up: noopMigration, Down: noopMigration},
}

pendingErr := migrationStatus(db)
assert.EqualError(tt, pendingErr, "1 migration is pending")

migrations = append(migrations, migration{
Name: "123", Up: noopMigration, Down: noopMigration,
})

pendingErr = migrationStatus(db)
assert.EqualError(tt, pendingErr, "2 migrations are pending")
})
}