Skip to content

Commit

Permalink
update sentinel error
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed May 19, 2023
1 parent 5db7047 commit 5877e94
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 24 deletions.
10 changes: 3 additions & 7 deletions down.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"github.com/pressly/goose/v4/internal/sqlparser"
)

// Down rolls back the most recently applied migration.
// Down rolls back the most recently applied migration. If there are no migrations to apply, this
// method returns ErrNoMigrations.
//
// If using out-of-order migrations, this method will roll back the most recently applied migration
// that was applied out-of-order. ???
Expand All @@ -19,7 +20,7 @@ func (p *Provider) Down(ctx context.Context) (*MigrationResult, error) {
return nil, err
}
if len(res) == 0 {
return nil, ErrNothingToApply
return nil, ErrNoMigration
}
return res[0], nil
}
Expand Down Expand Up @@ -52,9 +53,6 @@ func (p *Provider) down(ctx context.Context, downByOne bool, version int64) (_ [
}

if p.opt.NoVersioning {
if downByOne && len(p.migrations) == 0 {
return nil, ErrNothingToApply
}
var downMigrations []*migration.Migration
if downByOne {
downMigrations = append(downMigrations, p.migrations[len(p.migrations)-1])
Expand All @@ -72,8 +70,6 @@ func (p *Provider) down(ctx context.Context, downByOne bool, version int64) (_ [
return nil, nil
}

// This is the sequential path.

var downMigrations []*migration.Migration
for _, dbMigration := range dbMigrations {
if dbMigration.Version <= version {
Expand Down
5 changes: 3 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ var (
// ErrVersionNotFound when a migration version is not found.
ErrVersionNotFound = errors.New("version not found")

// ErrNothingToApply when there are no migrations to apply.
ErrNothingToApply = errors.New("no migrations to apply")
// ErrNoMigration when there are no migrations to apply. It is returned by (*Provider).Down and
// (*Provider).UpByOne.
ErrNoMigration = errors.New("no migration to apply")

// ErrAlreadyApplied when a migration has already been applied.
ErrAlreadyApplied = errors.New("already applied")
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func printResult(
useJSON bool,
) error {
switch {
case err == nil, errors.Is(err, goose.ErrNothingToApply):
case err == nil, errors.Is(err, goose.ErrNoMigration):
printMigrations(os.Stdout, migrationResults)
if count := len(migrationResults); count > 0 {
msg := "migration"
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/postgres/locking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestLockModeAdvisorySession(t *testing.T) {
for {
result, err := provider1.UpByOne(context.Background())
if err != nil {
if errors.Is(err, goose.ErrNothingToApply) {
if errors.Is(err, goose.ErrNoMigration) {
return nil
}
return err
Expand All @@ -103,7 +103,7 @@ func TestLockModeAdvisorySession(t *testing.T) {
for {
result, err := provider2.UpByOne(context.Background())
if err != nil {
if errors.Is(err, goose.ErrNothingToApply) {
if errors.Is(err, goose.ErrNoMigration) {
return nil
}
return err
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestLockModeAdvisorySession(t *testing.T) {
for {
result, err := provider1.Down(context.Background())
if err != nil {
if errors.Is(err, goose.ErrNothingToApply) {
if errors.Is(err, goose.ErrNoMigration) {
return nil
}
return err
Expand All @@ -202,7 +202,7 @@ func TestLockModeAdvisorySession(t *testing.T) {
for {
result, err := provider2.Down(context.Background())
if err != nil {
if errors.Is(err, goose.ErrNothingToApply) {
if errors.Is(err, goose.ErrNoMigration) {
return nil
}
return err
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/postgres/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ func TestMigrateUpByOne(t *testing.T) {
result, err := te.provider.UpByOne(ctx)
counter++
if counter > len(migrations) {
if !errors.Is(err, goose.ErrNothingToApply) {
t.Fatalf("incorrect error: got:%v want:%v", err, goose.ErrNothingToApply)
if !errors.Is(err, goose.ErrNoMigration) {
t.Fatalf("incorrect error: got:%v want:%v", err, goose.ErrNoMigration)
}
break
}
Expand Down
10 changes: 2 additions & 8 deletions up.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (p *Provider) Up(ctx context.Context) ([]*MigrationResult, error) {
}

// UpByOne applies the next available migration. If there are no migrations to apply, this method
// returns ErrNoNextVersion.
// returns ErrNoMigrations.
//
// It is safe for concurrent use.
func (p *Provider) UpByOne(ctx context.Context) (*MigrationResult, error) {
Expand All @@ -32,7 +32,7 @@ func (p *Provider) UpByOne(ctx context.Context) (*MigrationResult, error) {
return nil, err
}
if len(res) == 0 {
return nil, ErrNothingToApply
return nil, ErrNoMigration
}
return res[0], nil
}
Expand Down Expand Up @@ -122,12 +122,6 @@ func (p *Provider) up(ctx context.Context, upByOne bool, version int64) (_ []*Mi
migrationsToApply = append(migrationsToApply, m)
}
}
if len(migrationsToApply) == 0 {
if upByOne {
return nil, ErrNothingToApply
}
return nil, nil
}

// feat(mf): this is where can (optionally) group multiple migrations to be run in a single
// transaction. The default is to apply each migration sequentially on its own.
Expand Down

0 comments on commit 5877e94

Please sign in to comment.