Skip to content

Commit f3d569f

Browse files
authored
Remove pkg/errors in favor of std lib (#373)
1 parent d04c088 commit f3d569f

File tree

10 files changed

+50
-58
lines changed

10 files changed

+50
-58
lines changed

create.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"path/filepath"
88
"text/template"
99
"time"
10-
11-
"github.com/pkg/errors"
1210
)
1311

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

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

6765
f, err := os.Create(path)
6866
if err != nil {
69-
return errors.Wrap(err, "failed to create migration file")
67+
return fmt.Errorf("failed to create migration file: %w", err)
7068
}
7169
defer f.Close()
7270

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

8179
log.Printf("Created new file: %s\n", f.Name())

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ require (
99
github.com/go-sql-driver/mysql v1.6.0
1010
github.com/lib/pq v1.10.6
1111
github.com/ory/dockertest/v3 v3.9.1
12-
github.com/pkg/errors v0.9.1
1312
github.com/ziutek/mymysql v1.5.4
1413
modernc.org/sqlite v1.17.3
1514
)
@@ -39,6 +38,7 @@ require (
3938
github.com/opencontainers/runc v1.1.2 // indirect
4039
github.com/paulmach/orb v0.7.1 // indirect
4140
github.com/pierrec/lz4/v4 v4.1.15 // indirect
41+
github.com/pkg/errors v0.9.1 // indirect
4242
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
4343
github.com/shopspring/decimal v1.3.1 // indirect
4444
github.com/sirupsen/logrus v1.8.1 // indirect

migrate.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package goose
22

33
import (
44
"database/sql"
5+
"errors"
56
"fmt"
67
"io/fs"
78
"path"
89
"runtime"
910
"sort"
1011
"time"
11-
12-
"github.com/pkg/errors"
1312
)
1413

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

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

287286
return 0, ErrNoNextVersion

migration.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package goose
22

33
import (
44
"database/sql"
5+
"errors"
56
"fmt"
67
"path/filepath"
78
"strconv"
89
"strings"
910
"time"
10-
11-
"github.com/pkg/errors"
1211
)
1312

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

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

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

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

7776
case ".go":
7877
if !m.Registered {
79-
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)
78+
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)
8079
}
8180
tx, err := db.Begin()
8281
if err != nil {
83-
return errors.Wrap(err, "ERROR failed to begin transaction")
82+
return fmt.Errorf("ERROR failed to begin transaction: %w", err)
8483
}
8584

8685
fn := m.UpFn
@@ -92,25 +91,25 @@ func (m *Migration) run(db *sql.DB, direction bool) error {
9291
// Run Go migration function.
9392
if err := fn(tx); err != nil {
9493
tx.Rollback()
95-
return errors.Wrapf(err, "ERROR %v: failed to run Go migration function %T", filepath.Base(m.Source), fn)
94+
return fmt.Errorf("ERROR %v: failed to run Go migration function %T: %w", filepath.Base(m.Source), fn, err)
9695
}
9796
}
9897
if !m.noVersioning {
9998
if direction {
10099
if _, err := tx.Exec(GetDialect().insertVersionSQL(), m.Version, direction); err != nil {
101100
tx.Rollback()
102-
return errors.Wrap(err, "ERROR failed to execute transaction")
101+
return fmt.Errorf("ERROR failed to execute transaction: %w", err)
103102
}
104103
} else {
105104
if _, err := tx.Exec(GetDialect().deleteVersionSQL(), m.Version); err != nil {
106105
tx.Rollback()
107-
return errors.Wrap(err, "ERROR failed to execute transaction")
106+
return fmt.Errorf("ERROR failed to execute transaction: %w", err)
108107
}
109108
}
110109
}
111110

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

116115
if fn != nil {

migration_sql.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package goose
22

33
import (
44
"database/sql"
5+
"fmt"
56
"regexp"
67
"time"
7-
8-
"github.com/pkg/errors"
98
)
109

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

2524
tx, err := db.Begin()
2625
if err != nil {
27-
return errors.Wrap(err, "failed to begin transaction")
26+
return fmt.Errorf("failed to begin transaction: %w", err)
2827
}
2928

3029
for _, query := range statements {
3130
verboseInfo("Executing statement: %s\n", clearStatement(query))
3231
if err = execQuery(tx.Exec, query); err != nil {
3332
verboseInfo("Rollback transaction")
3433
tx.Rollback()
35-
return errors.Wrapf(err, "failed to execute SQL query %q", clearStatement(query))
34+
return fmt.Errorf("failed to execute SQL query %q: %w", clearStatement(query), err)
3635
}
3736
}
3837

@@ -41,20 +40,20 @@ func runSQLMigration(db *sql.DB, statements []string, useTx bool, v int64, direc
4140
if err := execQuery(tx.Exec, GetDialect().insertVersionSQL(), v, direction); err != nil {
4241
verboseInfo("Rollback transaction")
4342
tx.Rollback()
44-
return errors.Wrap(err, "failed to insert new goose version")
43+
return fmt.Errorf("failed to insert new goose version: %w", err)
4544
}
4645
} else {
4746
if err := execQuery(tx.Exec, GetDialect().deleteVersionSQL(), v); err != nil {
4847
verboseInfo("Rollback transaction")
4948
tx.Rollback()
50-
return errors.Wrap(err, "failed to delete goose version")
49+
return fmt.Errorf("failed to delete goose version: %w", err)
5150
}
5251
}
5352
}
5453

5554
verboseInfo("Commit transaction")
5655
if err := tx.Commit(); err != nil {
57-
return errors.Wrap(err, "failed to commit transaction")
56+
return fmt.Errorf("failed to commit transaction: %w", err)
5857
}
5958

6059
return nil
@@ -64,17 +63,17 @@ func runSQLMigration(db *sql.DB, statements []string, useTx bool, v int64, direc
6463
for _, query := range statements {
6564
verboseInfo("Executing statement: %s", clearStatement(query))
6665
if err := execQuery(db.Exec, query); err != nil {
67-
return errors.Wrapf(err, "failed to execute SQL query %q", clearStatement(query))
66+
return fmt.Errorf("failed to execute SQL query %q: %w", clearStatement(query), err)
6867
}
6968
}
7069
if !noVersioning {
7170
if direction {
7271
if err := execQuery(db.Exec, GetDialect().insertVersionSQL(), v, direction); err != nil {
73-
return errors.Wrap(err, "failed to insert new goose version")
72+
return fmt.Errorf("failed to insert new goose version: %w", err)
7473
}
7574
} else {
7675
if err := execQuery(db.Exec, GetDialect().deleteVersionSQL(), v); err != nil {
77-
return errors.Wrap(err, "failed to delete goose version")
76+
return fmt.Errorf("failed to delete goose version: %w", err)
7877
}
7978
}
8079
}

reset.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package goose
22

33
import (
44
"database/sql"
5+
"fmt"
56
"sort"
6-
7-
"github.com/pkg/errors"
87
)
98

109
// Reset rolls back all migrations
@@ -15,15 +14,15 @@ func Reset(db *sql.DB, dir string, opts ...OptionsFunc) error {
1514
}
1615
migrations, err := CollectMigrations(dir, minVersion, maxVersion)
1716
if err != nil {
18-
return errors.Wrap(err, "failed to collect migrations")
17+
return fmt.Errorf("failed to collect migrations: %w", err)
1918
}
2019
if option.noVersioning {
2120
return DownTo(db, dir, minVersion, opts...)
2221
}
2322

2423
statuses, err := dbMigrationsStatus(db)
2524
if err != nil {
26-
return errors.Wrap(err, "failed to get status of migrations")
25+
return fmt.Errorf("failed to get status of migrations: %w", err)
2726
}
2827
sort.Sort(sort.Reverse(migrations))
2928

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

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

6059
if _, ok := result[row.VersionID]; ok {

sql_parser.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package goose
33
import (
44
"bufio"
55
"bytes"
6+
"fmt"
67
"io"
78
"regexp"
89
"strings"
910
"sync"
1011

11-
"github.com/pkg/errors"
12+
"errors"
1213
)
1314

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

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

@@ -99,7 +100,7 @@ func parseSQLMigration(r io.Reader, direction bool) (stmts []string, useTx bool,
99100
case gooseDown, gooseStatementEndDown:
100101
stateMachine.Set(gooseStatementBeginDown)
101102
default:
102-
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)
103+
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)
103104
}
104105
continue
105106

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

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

138139
// 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,
154155
continue
155156
}
156157
default:
157-
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)
158+
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)
158159
}
159160

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

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

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

201202
return stmts, useTx, nil

sql_parser_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package goose
22

33
import (
4+
"fmt"
45
"os"
56
"strings"
67
"testing"
7-
8-
"github.com/pkg/errors"
98
)
109

1110
func TestSemicolons(t *testing.T) {
@@ -58,7 +57,7 @@ func TestSplitStatements(t *testing.T) {
5857
// up
5958
stmts, _, err := parseSQLMigration(strings.NewReader(test.sql), true)
6059
if err != nil {
61-
t.Error(errors.Wrapf(err, "tt[%v] unexpected error", i))
60+
t.Error(fmt.Errorf("tt[%v] unexpected error: %w", i, err))
6261
}
6362
if len(stmts) != test.up {
6463
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) {
6766
// down
6867
stmts, _, err = parseSQLMigration(strings.NewReader(test.sql), false)
6968
if err != nil {
70-
t.Error(errors.Wrapf(err, "tt[%v] unexpected error", i))
69+
t.Error(fmt.Errorf("tt[%v] unexpected error: %w", i, err))
7170
}
7271
if len(stmts) != test.down {
7372
t.Errorf("tt[%v] incorrect number of down stmts. got %v (%+v), want %v", i, len(stmts), stmts, test.down)

0 commit comments

Comments
 (0)