Skip to content

Commit

Permalink
updating context
Browse files Browse the repository at this point in the history
  • Loading branch information
diogogmt committed May 21, 2020
1 parent 87fc0d1 commit 24e5e23
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 35 deletions.
22 changes: 16 additions & 6 deletions down.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"
)

// Down rolls back a single migration from the current version.
func Down(ctx context.Context, db *sql.DB, dir string) error {
// DownCtx rolls back a single migration from the current version.
func DownCtx(ctx context.Context, db *sql.DB, dir string) error {
currentVersion, err := GetDBVersion(db)
if err != nil {
return err
Expand All @@ -23,11 +23,16 @@ func Down(ctx context.Context, db *sql.DB, dir string) error {
return fmt.Errorf("no migration %v", currentVersion)
}

return current.Down(ctx, db)
return current.DownCtx(ctx, db)
}

// DownTo rolls back migrations to a specific version.
func DownTo(ctx context.Context, db *sql.DB, dir string, version int64) error {
// Down rolls back a single migration from the current version.
func Down(db *sql.DB, dir string) error {
return DownCtx(context.Background(), db, dir)
}

// DownToCtx rolls back migrations to a specific version.
func DownToCtx(ctx context.Context, db *sql.DB, dir string, version int64) error {
migrations, err := CollectMigrations(dir, minVersion, maxVersion)
if err != nil {
return err
Expand All @@ -50,8 +55,13 @@ func DownTo(ctx context.Context, db *sql.DB, dir string, version int64) error {
return nil
}

if err = current.Down(ctx, db); err != nil {
if err = current.DownCtx(ctx, db); err != nil {
return err
}
}
}

// DownTo rolls back migrations to a specific version.
func DownTo(db *sql.DB, dir string, version int64) error {
return DownToCtx(context.Background(), db, dir, version)
}
24 changes: 12 additions & 12 deletions goose.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
const VERSION = "v2.7.0-rc3"

var (
minVersion = int64(0)
maxVersion = int64((1 << 63) - 1)
timestampFormat = "20060102150405"
verbose = false
minVersion = int64(0)
maxVersion = int64((1 << 63) - 1)
timestampFormat = "20060102150405"
verbose = false
)

// SetVerbose set the goose verbosity mode
Expand All @@ -23,14 +23,14 @@ func SetVerbose(v bool) {

// Run runs a goose command.
func Run(command string, db *sql.DB, dir string, args ...string) error {
return RunWithCtx(context.Background(), command, db, dir, args...)
return RunCtx(context.Background(), command, db, dir, args...)
}

// Run runs a goose command propagating a context down the call stack.
func RunWithCtx(ctx context.Context, command string, db *sql.DB, dir string, args ...string) error {
func RunCtx(ctx context.Context, command string, db *sql.DB, dir string, args ...string) error {
switch command {
case "up":
if err := Up(ctx, db, dir); err != nil {
if err := UpCtx(ctx, db, dir); err != nil {
return err
}
case "up-by-one":
Expand All @@ -46,7 +46,7 @@ func RunWithCtx(ctx context.Context, command string, db *sql.DB, dir string, arg
if err != nil {
return fmt.Errorf("version must be a number (got '%s')", args[0])
}
if err := UpTo(ctx, db, dir, version); err != nil {
if err := UpToCtx(ctx, db, dir, version); err != nil {
return err
}
case "create":
Expand All @@ -62,7 +62,7 @@ func RunWithCtx(ctx context.Context, command string, db *sql.DB, dir string, arg
return err
}
case "down":
if err := Down(ctx, db, dir); err != nil {
if err := DownCtx(ctx, db, dir); err != nil {
return err
}
case "down-to":
Expand All @@ -74,19 +74,19 @@ func RunWithCtx(ctx context.Context, command string, db *sql.DB, dir string, arg
if err != nil {
return fmt.Errorf("version must be a number (got '%s')", args[0])
}
if err := DownTo(ctx, db, dir, version); err != nil {
if err := DownToCtx(ctx, db, dir, version); err != nil {
return err
}
case "fix":
if err := Fix(dir); err != nil {
return err
}
case "redo":
if err := Redo(ctx, db, dir); err != nil {
if err := RedoCtx(ctx, db, dir); err != nil {
return err
}
case "reset":
if err := Reset(ctx, db, dir); err != nil {
if err := ResetCtx(ctx, db, dir); err != nil {
return err
}
case "status":
Expand Down
18 changes: 14 additions & 4 deletions migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,32 @@ func (m *Migration) String() string {
return fmt.Sprintf(m.Source)
}

// Up runs an up migration.
func (m *Migration) Up(ctx context.Context, db *sql.DB) error {
// UpCtx runs an up migration.
func (m *Migration) UpCtx(ctx context.Context, db *sql.DB) error {
if err := m.run(ctx, db, true); err != nil {
return err
}
return nil
}

// Down runs a down migration.
func (m *Migration) Down(ctx context.Context, db *sql.DB) error {
// Up runs an up migration.
func (m *Migration) Up(db *sql.DB) error {
return m.UpCtx(context.Background(), db)
}

// DownCtx runs a down migration.
func (m *Migration) DownCtx(ctx context.Context, db *sql.DB) error {
if err := m.run(ctx, db, false); err != nil {
return err
}
return nil
}

// Down runs a down migration.
func (m *Migration) Down(db *sql.DB) error {
return m.DownCtx(context.Background(), db)
}

func (m *Migration) run(ctx context.Context, db *sql.DB, direction bool) error {
switch filepath.Ext(m.Source) {
case ".sql":
Expand Down
11 changes: 8 additions & 3 deletions redo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// Redo rolls back the most recently applied migration, then runs it again.
func Redo(ctx context.Context, db *sql.DB, dir string) error {
func RedoCtx(ctx context.Context, db *sql.DB, dir string) error {
currentVersion, err := GetDBVersion(db)
if err != nil {
return err
Expand All @@ -22,13 +22,18 @@ func Redo(ctx context.Context, db *sql.DB, dir string) error {
return err
}

if err := current.Down(ctx, db); err != nil {
if err := current.DownCtx(ctx, db); err != nil {
return err
}

if err := current.Up(ctx, db); err != nil {
if err := current.UpCtx(ctx, db); err != nil {
return err
}

return nil
}

// Redo rolls back the most recently applied migration, then runs it again.
func Redo(db *sql.DB, dir string) error {
return RedoCtx(context.Background(), db, dir)
}
9 changes: 7 additions & 2 deletions reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// Reset rolls back all migrations
func Reset(ctx context.Context, db *sql.DB, dir string) error {
func ResetCtx(ctx context.Context, db *sql.DB, dir string) error {
migrations, err := CollectMigrations(dir, minVersion, maxVersion)
if err != nil {
return errors.Wrap(err, "failed to collect migrations")
Expand All @@ -24,14 +24,19 @@ func Reset(ctx context.Context, db *sql.DB, dir string) error {
if !statuses[migration.Version] {
continue
}
if err = migration.Down(ctx, db); err != nil {
if err = migration.DownCtx(ctx, db); err != nil {
return errors.Wrap(err, "failed to db-down")
}
}

return nil
}

// Reset rolls back all migrations
func Reset(ctx context.Context, db *sql.DB, dir string) error {
return ResetCtx(context.Background(), db, dir)
}

func dbMigrationsStatus(db *sql.DB) (map[int64]bool, error) {
rows, err := GetDialect().dbVersionQuery(db)
if err != nil {
Expand Down
31 changes: 23 additions & 8 deletions up.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"database/sql"
)

// UpTo migrates up to a specific version.
func UpTo(ctx context.Context, db *sql.DB, dir string, version int64) error {
// UpToCtx migrates up to a specific version.
func UpToCtx(ctx context.Context, db *sql.DB, dir string, version int64) error {
migrations, err := CollectMigrations(dir, minVersion, version)
if err != nil {
return err
Expand All @@ -27,19 +27,29 @@ func UpTo(ctx context.Context, db *sql.DB, dir string, version int64) error {
return err
}

if err = next.Up(ctx, db); err != nil {
if err = next.UpCtx(ctx, db); err != nil {
return err
}
}
}

// Up applies all available migrations.
func Up(ctx context.Context, db *sql.DB, dir string) error {
// UpTo migrates up to a specific version.
func UpTo(ctx context.Context, db *sql.DB, dir string, version int64) error {
return UpToCtx(context.Background(), db, dir, version)
}

// UpCtx applies all available migrations.
func UpCtx(ctx context.Context, db *sql.DB, dir string) error {
return UpTo(ctx, db, dir, maxVersion)
}

// UpByOne migrates up by a single version.
func UpByOne(ctx context.Context, db *sql.DB, dir string) error {
// Up applies all available migrations.
func Up(db *sql.DB, dir string) error {
return UpCtx(context.Background(), db, dir)
}

// UpByOneCtx migrates up by a single version.
func UpByOneCtx(ctx context.Context, db *sql.DB, dir string) error {
migrations, err := CollectMigrations(dir, minVersion, maxVersion)
if err != nil {
return err
Expand All @@ -58,9 +68,14 @@ func UpByOne(ctx context.Context, db *sql.DB, dir string) error {
return err
}

if err = next.Up(ctx, db); err != nil {
if err = next.UpCtx(ctx, db); err != nil {
return err
}

return nil
}

// UpByOne migrates up by a single version.
func UpByOne(ctx context.Context, db *sql.DB, dir string) error {
return UpByOneCtx(context.Background(), db, dir)
}

0 comments on commit 24e5e23

Please sign in to comment.