Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[First Draft] postgre: add more error information to an error message (position, line number, partial SQL) #203

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/lib/pq v1.9.0
github.com/mattn/go-sqlite3 v1.14.6
github.com/pierrec/lz4 v2.6.0+incompatible // indirect
github.com/pkg/errors v0.9.1
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/testify v1.7.0
github.com/urfave/cli/v2 v2.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pierrec/lz4 v2.6.0+incompatible h1:Ix9yFKn1nSPBLFl/yZknTp8TU5G4Ps0JDmguYK6iH1A=
github.com/pierrec/lz4 v2.6.0+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
37 changes: 36 additions & 1 deletion pkg/dbmate/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dbmate

import (
"database/sql"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -11,9 +10,13 @@ import (
"path/filepath"
"regexp"
"sort"
"strconv"
"time"

"github.com/amacneil/dbmate/pkg/dbutil"

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

// DefaultMigrationsDir specifies default directory to find migration files
Expand Down Expand Up @@ -360,6 +363,38 @@ func (db *DB) migrate(drv Driver) error {
// run actual migration
result, err := tx.Exec(up.Contents)
if err != nil {
if pqErr, ok := err.(*pq.Error); ok {
pos, posErr := strconv.Atoi(pqErr.Position)
if posErr != nil {
// if unable to parse position as number
// pq must be giving us a bad number string or number
// is too big to fit in an int
return errors.Wrapf(err, "pos: %v", pqErr.Position)
}
// compute line number manually as pq driver does not provide it
column := 0
lineCount := 0
itColumn := 0
for _, c := range up.Contents[:pos] {
itColumn++
if c == '\n' {
column = itColumn
itColumn = 0
lineCount++
}
}
const fuzzyRange = 50
minPos := pos - fuzzyRange
if minPos < 0 {
minPos = 0
}
maxPos := pos + fuzzyRange
if maxPos >= len(up.Contents) {
maxPos = len(up.Contents) - 1
}
areaOfFailure := up.Contents[minPos:maxPos]
return errors.Wrapf(err, "position: %v, line: %v, column: %v, sql: %s\n", pqErr.Position, lineCount, column, areaOfFailure)
}
return err
} else if db.Verbose {
db.printVerbose(result)
Expand Down