Skip to content

Commit

Permalink
Remove pkg/errors in favor of std lib (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman authored Jun 23, 2022
1 parent d04c088 commit f3d569f
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 58 deletions.
8 changes: 3 additions & 5 deletions create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"path/filepath"
"text/template"
"time"

"github.com/pkg/errors"
)

type tmplVars struct {
Expand Down Expand Up @@ -61,12 +59,12 @@ func CreateWithTemplate(db *sql.DB, dir string, tmpl *template.Template, name, m

path := filepath.Join(dir, filename)
if _, err := os.Stat(path); !os.IsNotExist(err) {
return errors.Wrap(err, "failed to create migration file")
return fmt.Errorf("failed to create migration file: %w", err)
}

f, err := os.Create(path)
if err != nil {
return errors.Wrap(err, "failed to create migration file")
return fmt.Errorf("failed to create migration file: %w", err)
}
defer f.Close()

Expand All @@ -75,7 +73,7 @@ func CreateWithTemplate(db *sql.DB, dir string, tmpl *template.Template, name, m
CamelName: camelCase(name),
}
if err := tmpl.Execute(f, vars); err != nil {
return errors.Wrap(err, "failed to execute tmpl")
return fmt.Errorf("failed to execute tmpl: %w", err)
}

log.Printf("Created new file: %s\n", f.Name())
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/go-sql-driver/mysql v1.6.0
github.com/lib/pq v1.10.6
github.com/ory/dockertest/v3 v3.9.1
github.com/pkg/errors v0.9.1
github.com/ziutek/mymysql v1.5.4
modernc.org/sqlite v1.17.3
)
Expand Down Expand Up @@ -39,6 +38,7 @@ require (
github.com/opencontainers/runc v1.1.2 // indirect
github.com/paulmach/orb v0.7.1 // indirect
github.com/pierrec/lz4/v4 v4.1.15 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
Expand Down
7 changes: 3 additions & 4 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package goose

import (
"database/sql"
"errors"
"fmt"
"io/fs"
"path"
"runtime"
"sort"
"time"

"github.com/pkg/errors"
)

var (
Expand Down Expand Up @@ -256,7 +255,7 @@ func EnsureDBVersion(db *sql.DB) (int64, error) {
for rows.Next() {
var row MigrationRecord
if err = rows.Scan(&row.VersionID, &row.IsApplied); err != nil {
return 0, errors.Wrap(err, "failed to scan row")
return 0, fmt.Errorf("failed to scan row: %w", err)
}

// have we already marked this version to be skipped?
Expand All @@ -281,7 +280,7 @@ func EnsureDBVersion(db *sql.DB) (int64, error) {
toSkip = append(toSkip, row.VersionID)
}
if err := rows.Err(); err != nil {
return 0, errors.Wrap(err, "failed to get next row")
return 0, fmt.Errorf("failed to get next row: %w", err)
}

return 0, ErrNoNextVersion
Expand Down
21 changes: 10 additions & 11 deletions migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package goose

import (
"database/sql"
"errors"
"fmt"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
)

// MigrationRecord struct.
Expand Down Expand Up @@ -55,17 +54,17 @@ func (m *Migration) run(db *sql.DB, direction bool) error {
case ".sql":
f, err := baseFS.Open(m.Source)
if err != nil {
return errors.Wrapf(err, "ERROR %v: failed to open SQL migration file", filepath.Base(m.Source))
return fmt.Errorf("ERROR %v: failed to open SQL migration file: %w", filepath.Base(m.Source), err)
}
defer f.Close()

statements, useTx, err := parseSQLMigration(f, direction)
if err != nil {
return errors.Wrapf(err, "ERROR %v: failed to parse SQL migration file", filepath.Base(m.Source))
return fmt.Errorf("ERROR %v: failed to parse SQL migration file: %w", filepath.Base(m.Source), err)
}

if err := runSQLMigration(db, statements, useTx, m.Version, direction, m.noVersioning); err != nil {
return errors.Wrapf(err, "ERROR %v: failed to run SQL migration", filepath.Base(m.Source))
return fmt.Errorf("ERROR %v: failed to run SQL migration: %w", filepath.Base(m.Source), err)
}

if len(statements) > 0 {
Expand All @@ -76,11 +75,11 @@ func (m *Migration) run(db *sql.DB, direction bool) error {

case ".go":
if !m.Registered {
return errors.Errorf("ERROR %v: failed to run Go migration: Go functions must be registered and built into a custom binary (see https://github.com/pressly/goose/tree/master/examples/go-migrations)", m.Source)
return fmt.Errorf("ERROR %v: failed to run Go migration: Go functions must be registered and built into a custom binary (see https://github.com/pressly/goose/tree/master/examples/go-migrations)", m.Source)
}
tx, err := db.Begin()
if err != nil {
return errors.Wrap(err, "ERROR failed to begin transaction")
return fmt.Errorf("ERROR failed to begin transaction: %w", err)
}

fn := m.UpFn
Expand All @@ -92,25 +91,25 @@ func (m *Migration) run(db *sql.DB, direction bool) error {
// Run Go migration function.
if err := fn(tx); err != nil {
tx.Rollback()
return errors.Wrapf(err, "ERROR %v: failed to run Go migration function %T", filepath.Base(m.Source), fn)
return fmt.Errorf("ERROR %v: failed to run Go migration function %T: %w", filepath.Base(m.Source), fn, err)
}
}
if !m.noVersioning {
if direction {
if _, err := tx.Exec(GetDialect().insertVersionSQL(), m.Version, direction); err != nil {
tx.Rollback()
return errors.Wrap(err, "ERROR failed to execute transaction")
return fmt.Errorf("ERROR failed to execute transaction: %w", err)
}
} else {
if _, err := tx.Exec(GetDialect().deleteVersionSQL(), m.Version); err != nil {
tx.Rollback()
return errors.Wrap(err, "ERROR failed to execute transaction")
return fmt.Errorf("ERROR failed to execute transaction: %w", err)
}
}
}

if err := tx.Commit(); err != nil {
return errors.Wrap(err, "ERROR failed to commit transaction")
return fmt.Errorf("ERROR failed to commit transaction: %w", err)
}

if fn != nil {
Expand Down
19 changes: 9 additions & 10 deletions migration_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package goose

import (
"database/sql"
"fmt"
"regexp"
"time"

"github.com/pkg/errors"
)

// Run a migration specified in raw SQL.
Expand All @@ -24,15 +23,15 @@ func runSQLMigration(db *sql.DB, statements []string, useTx bool, v int64, direc

tx, err := db.Begin()
if err != nil {
return errors.Wrap(err, "failed to begin transaction")
return fmt.Errorf("failed to begin transaction: %w", err)
}

for _, query := range statements {
verboseInfo("Executing statement: %s\n", clearStatement(query))
if err = execQuery(tx.Exec, query); err != nil {
verboseInfo("Rollback transaction")
tx.Rollback()
return errors.Wrapf(err, "failed to execute SQL query %q", clearStatement(query))
return fmt.Errorf("failed to execute SQL query %q: %w", clearStatement(query), err)
}
}

Expand All @@ -41,20 +40,20 @@ func runSQLMigration(db *sql.DB, statements []string, useTx bool, v int64, direc
if err := execQuery(tx.Exec, GetDialect().insertVersionSQL(), v, direction); err != nil {
verboseInfo("Rollback transaction")
tx.Rollback()
return errors.Wrap(err, "failed to insert new goose version")
return fmt.Errorf("failed to insert new goose version: %w", err)
}
} else {
if err := execQuery(tx.Exec, GetDialect().deleteVersionSQL(), v); err != nil {
verboseInfo("Rollback transaction")
tx.Rollback()
return errors.Wrap(err, "failed to delete goose version")
return fmt.Errorf("failed to delete goose version: %w", err)
}
}
}

verboseInfo("Commit transaction")
if err := tx.Commit(); err != nil {
return errors.Wrap(err, "failed to commit transaction")
return fmt.Errorf("failed to commit transaction: %w", err)
}

return nil
Expand All @@ -64,17 +63,17 @@ func runSQLMigration(db *sql.DB, statements []string, useTx bool, v int64, direc
for _, query := range statements {
verboseInfo("Executing statement: %s", clearStatement(query))
if err := execQuery(db.Exec, query); err != nil {
return errors.Wrapf(err, "failed to execute SQL query %q", clearStatement(query))
return fmt.Errorf("failed to execute SQL query %q: %w", clearStatement(query), err)
}
}
if !noVersioning {
if direction {
if err := execQuery(db.Exec, GetDialect().insertVersionSQL(), v, direction); err != nil {
return errors.Wrap(err, "failed to insert new goose version")
return fmt.Errorf("failed to insert new goose version: %w", err)
}
} else {
if err := execQuery(db.Exec, GetDialect().deleteVersionSQL(), v); err != nil {
return errors.Wrap(err, "failed to delete goose version")
return fmt.Errorf("failed to delete goose version: %w", err)
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package goose

import (
"database/sql"
"fmt"
"sort"

"github.com/pkg/errors"
)

// Reset rolls back all migrations
Expand All @@ -15,15 +14,15 @@ func Reset(db *sql.DB, dir string, opts ...OptionsFunc) error {
}
migrations, err := CollectMigrations(dir, minVersion, maxVersion)
if err != nil {
return errors.Wrap(err, "failed to collect migrations")
return fmt.Errorf("failed to collect migrations: %w", err)
}
if option.noVersioning {
return DownTo(db, dir, minVersion, opts...)
}

statuses, err := dbMigrationsStatus(db)
if err != nil {
return errors.Wrap(err, "failed to get status of migrations")
return fmt.Errorf("failed to get status of migrations: %w", err)
}
sort.Sort(sort.Reverse(migrations))

Expand All @@ -32,7 +31,7 @@ func Reset(db *sql.DB, dir string, opts ...OptionsFunc) error {
continue
}
if err = migration.Down(db); err != nil {
return errors.Wrap(err, "failed to db-down")
return fmt.Errorf("failed to db-down: %w", err)
}
}

Expand All @@ -54,7 +53,7 @@ func dbMigrationsStatus(db *sql.DB) (map[int64]bool, error) {
for rows.Next() {
var row MigrationRecord
if err = rows.Scan(&row.VersionID, &row.IsApplied); err != nil {
return nil, errors.Wrap(err, "failed to scan row")
return nil, fmt.Errorf("failed to scan row: %w", err)
}

if _, ok := result[row.VersionID]; ok {
Expand Down
17 changes: 9 additions & 8 deletions sql_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package goose
import (
"bufio"
"bytes"
"fmt"
"io"
"regexp"
"strings"
"sync"

"github.com/pkg/errors"
"errors"
)

type parserState int
Expand Down Expand Up @@ -79,7 +80,7 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,
case start:
stateMachine.Set(gooseUp)
default:
return nil, false, errors.Errorf("duplicate '-- +goose Up' annotations; stateMachine=%v, see https://github.com/pressly/goose#sql-migrations", stateMachine)
return nil, false, fmt.Errorf("duplicate '-- +goose Up' annotations; stateMachine=%v, see https://github.com/pressly/goose#sql-migrations", stateMachine)
}
continue

Expand All @@ -88,7 +89,7 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,
case gooseUp, gooseStatementEndUp:
stateMachine.Set(gooseDown)
default:
return nil, false, errors.Errorf("must start with '-- +goose Up' annotation, stateMachine=%v, see https://github.com/pressly/goose#sql-migrations", stateMachine)
return nil, false, fmt.Errorf("must start with '-- +goose Up' annotation, stateMachine=%v, see https://github.com/pressly/goose#sql-migrations", stateMachine)
}
continue

Expand All @@ -99,7 +100,7 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,
case gooseDown, gooseStatementEndDown:
stateMachine.Set(gooseStatementBeginDown)
default:
return nil, false, errors.Errorf("'-- +goose StatementBegin' must be defined after '-- +goose Up' or '-- +goose Down' annotation, stateMachine=%v, see https://github.com/pressly/goose#sql-migrations", stateMachine)
return nil, false, fmt.Errorf("'-- +goose StatementBegin' must be defined after '-- +goose Up' or '-- +goose Down' annotation, stateMachine=%v, see https://github.com/pressly/goose#sql-migrations", stateMachine)
}
continue

Expand Down Expand Up @@ -132,7 +133,7 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,

// Write SQL line to a buffer.
if _, err := buf.WriteString(line + "\n"); err != nil {
return nil, false, errors.Wrap(err, "failed to write to buf")
return nil, false, fmt.Errorf("failed to write to buf: %w", err)
}

// Read SQL body one by line, if we're in the right direction.
Expand All @@ -154,7 +155,7 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,
continue
}
default:
return nil, false, errors.Errorf("failed to parse migration: unexpected state %q on line %q, see https://github.com/pressly/goose#sql-migrations", stateMachine, line)
return nil, false, fmt.Errorf("failed to parse migration: unexpected state %q on line %q, see https://github.com/pressly/goose#sql-migrations", stateMachine, line)
}

switch stateMachine.Get() {
Expand Down Expand Up @@ -183,7 +184,7 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,
}
}
if err := scanner.Err(); err != nil {
return nil, false, errors.Wrap(err, "failed to scan migration")
return nil, false, fmt.Errorf("failed to scan migration: %w", err)
}
// EOF

Expand All @@ -195,7 +196,7 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,
}

if bufferRemaining := strings.TrimSpace(buf.String()); len(bufferRemaining) > 0 {
return nil, false, errors.Errorf("failed to parse migration: state %q, direction: %v: unexpected unfinished SQL query: %q: missing semicolon?", stateMachine, direction, bufferRemaining)
return nil, false, fmt.Errorf("failed to parse migration: state %q, direction: %v: unexpected unfinished SQL query: %q: missing semicolon?", stateMachine, direction, bufferRemaining)
}

return stmts, useTx, nil
Expand Down
7 changes: 3 additions & 4 deletions sql_parser_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package goose

import (
"fmt"
"os"
"strings"
"testing"

"github.com/pkg/errors"
)

func TestSemicolons(t *testing.T) {
Expand Down Expand Up @@ -58,7 +57,7 @@ func TestSplitStatements(t *testing.T) {
// up
stmts, _, err := parseSQLMigration(strings.NewReader(test.sql), true)
if err != nil {
t.Error(errors.Wrapf(err, "tt[%v] unexpected error", i))
t.Error(fmt.Errorf("tt[%v] unexpected error: %w", i, err))
}
if len(stmts) != test.up {
t.Errorf("tt[%v] incorrect number of up stmts. got %v (%+v), want %v", i, len(stmts), stmts, test.up)
Expand All @@ -67,7 +66,7 @@ func TestSplitStatements(t *testing.T) {
// down
stmts, _, err = parseSQLMigration(strings.NewReader(test.sql), false)
if err != nil {
t.Error(errors.Wrapf(err, "tt[%v] unexpected error", i))
t.Error(fmt.Errorf("tt[%v] unexpected error: %w", i, err))
}
if len(stmts) != test.down {
t.Errorf("tt[%v] incorrect number of down stmts. got %v (%+v), want %v", i, len(stmts), stmts, test.down)
Expand Down
Loading

0 comments on commit f3d569f

Please sign in to comment.