From 5877e940b1f648fdfbe1251952c637a6dd338904 Mon Sep 17 00:00:00 2001 From: Mike Fridman Date: Fri, 19 May 2023 19:21:44 -0400 Subject: [PATCH] update sentinel error --- down.go | 10 +++------- errors.go | 5 +++-- internal/cli/output.go | 2 +- tests/e2e/postgres/locking_test.go | 8 ++++---- tests/e2e/postgres/migrations_test.go | 4 ++-- up.go | 10 ++-------- 6 files changed, 15 insertions(+), 24 deletions(-) diff --git a/down.go b/down.go index 6ccdb5df2..de6359673 100644 --- a/down.go +++ b/down.go @@ -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. ??? @@ -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 } @@ -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]) @@ -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 { diff --git a/errors.go b/errors.go index b80f7e9a3..f9dba22fc 100644 --- a/errors.go +++ b/errors.go @@ -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") diff --git a/internal/cli/output.go b/internal/cli/output.go index 038a16148..6910d3e72 100644 --- a/internal/cli/output.go +++ b/internal/cli/output.go @@ -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" diff --git a/tests/e2e/postgres/locking_test.go b/tests/e2e/postgres/locking_test.go index 5713c8bca..d629b5df2 100644 --- a/tests/e2e/postgres/locking_test.go +++ b/tests/e2e/postgres/locking_test.go @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/tests/e2e/postgres/migrations_test.go b/tests/e2e/postgres/migrations_test.go index 5a52b38ea..c6ac3b5be 100644 --- a/tests/e2e/postgres/migrations_test.go +++ b/tests/e2e/postgres/migrations_test.go @@ -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 } diff --git a/up.go b/up.go index 42c6dbf01..558f1271d 100644 --- a/up.go +++ b/up.go @@ -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) { @@ -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 } @@ -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.