From f3d569f61bc677350e34ae36f825ca103e526857 Mon Sep 17 00:00:00 2001 From: Michael Fridman Date: Wed, 22 Jun 2022 21:36:07 -0400 Subject: [PATCH] Remove pkg/errors in favor of std lib (#373) --- create.go | 8 +++----- go.mod | 2 +- migrate.go | 7 +++---- migration.go | 21 ++++++++++----------- migration_sql.go | 19 +++++++++---------- reset.go | 11 +++++------ sql_parser.go | 17 +++++++++-------- sql_parser_test.go | 7 +++---- status.go | 11 +++++------ version.go | 5 ++--- 10 files changed, 50 insertions(+), 58 deletions(-) diff --git a/create.go b/create.go index fe386bd47..1a8bb74c0 100644 --- a/create.go +++ b/create.go @@ -7,8 +7,6 @@ import ( "path/filepath" "text/template" "time" - - "github.com/pkg/errors" ) type tmplVars struct { @@ -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() @@ -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()) diff --git a/go.mod b/go.mod index 8dc165586..05a43d47b 100644 --- a/go.mod +++ b/go.mod @@ -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 ) @@ -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 diff --git a/migrate.go b/migrate.go index 3986746ca..91da07dcc 100644 --- a/migrate.go +++ b/migrate.go @@ -2,14 +2,13 @@ package goose import ( "database/sql" + "errors" "fmt" "io/fs" "path" "runtime" "sort" "time" - - "github.com/pkg/errors" ) var ( @@ -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? @@ -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 diff --git a/migration.go b/migration.go index 9ceb76d9e..775dc779c 100644 --- a/migration.go +++ b/migration.go @@ -2,13 +2,12 @@ package goose import ( "database/sql" + "errors" "fmt" "path/filepath" "strconv" "strings" "time" - - "github.com/pkg/errors" ) // MigrationRecord struct. @@ -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 { @@ -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 @@ -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 { diff --git a/migration_sql.go b/migration_sql.go index 4db05140f..359ebf6be 100644 --- a/migration_sql.go +++ b/migration_sql.go @@ -2,10 +2,9 @@ package goose import ( "database/sql" + "fmt" "regexp" "time" - - "github.com/pkg/errors" ) // Run a migration specified in raw SQL. @@ -24,7 +23,7 @@ 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 { @@ -32,7 +31,7 @@ func runSQLMigration(db *sql.DB, statements []string, useTx bool, v int64, direc 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) } } @@ -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 @@ -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) } } } diff --git a/reset.go b/reset.go index 3605a64a9..258841fad 100644 --- a/reset.go +++ b/reset.go @@ -2,9 +2,8 @@ package goose import ( "database/sql" + "fmt" "sort" - - "github.com/pkg/errors" ) // Reset rolls back all migrations @@ -15,7 +14,7 @@ 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...) @@ -23,7 +22,7 @@ func Reset(db *sql.DB, dir string, opts ...OptionsFunc) error { 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)) @@ -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) } } @@ -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 { diff --git a/sql_parser.go b/sql_parser.go index a3f002fc9..91bc84da0 100644 --- a/sql_parser.go +++ b/sql_parser.go @@ -3,12 +3,13 @@ package goose import ( "bufio" "bytes" + "fmt" "io" "regexp" "strings" "sync" - "github.com/pkg/errors" + "errors" ) type parserState int @@ -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 @@ -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 @@ -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 @@ -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. @@ -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() { @@ -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 @@ -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 diff --git a/sql_parser_test.go b/sql_parser_test.go index facc55d2b..6420ebb03 100644 --- a/sql_parser_test.go +++ b/sql_parser_test.go @@ -1,11 +1,10 @@ package goose import ( + "fmt" "os" "strings" "testing" - - "github.com/pkg/errors" ) func TestSemicolons(t *testing.T) { @@ -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) @@ -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) diff --git a/status.go b/status.go index 7378f01d5..f53f1bece 100644 --- a/status.go +++ b/status.go @@ -2,10 +2,9 @@ package goose import ( "database/sql" + "fmt" "path/filepath" "time" - - "github.com/pkg/errors" ) // Status prints the status of all migrations. @@ -16,7 +15,7 @@ func Status(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 { log.Println(" Applied At Migration") @@ -29,14 +28,14 @@ func Status(db *sql.DB, dir string, opts ...OptionsFunc) error { // must ensure that the version table exists if we're running on a pristine DB if _, err := EnsureDBVersion(db); err != nil { - return errors.Wrap(err, "failed to ensure DB version") + return fmt.Errorf("failed to ensure DB version: %w", err) } log.Println(" Applied At Migration") log.Println(" =======================================") for _, migration := range migrations { if err := printMigrationStatus(db, migration.Version, filepath.Base(migration.Source)); err != nil { - return errors.Wrap(err, "failed to print status") + return fmt.Errorf("failed to print status: %w", err) } } @@ -50,7 +49,7 @@ func printMigrationStatus(db *sql.DB, version int64, script string) error { err := db.QueryRow(q, version).Scan(&row.TStamp, &row.IsApplied) if err != nil && err != sql.ErrNoRows { - return errors.Wrap(err, "failed to query the latest migration") + return fmt.Errorf("failed to query the latest migration: %w", err) } var appliedAt string diff --git a/version.go b/version.go index f6628ac51..47765f728 100644 --- a/version.go +++ b/version.go @@ -2,8 +2,7 @@ package goose import ( "database/sql" - - "github.com/pkg/errors" + "fmt" ) // Version prints the current version of the database. @@ -16,7 +15,7 @@ func Version(db *sql.DB, dir string, opts ...OptionsFunc) error { var current int64 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 len(migrations) > 0 { current = migrations[len(migrations)-1].Version