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

[bugfix] return ErrNotFound correctly #952

Merged
merged 3 commits into from
Nov 24, 2020
Merged
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
5 changes: 4 additions & 1 deletion db/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package db

import (
"errors"
"fmt"

"github.com/jackc/pgx/v4"
)

var (
ErrNotFound = errors.New("not found")
ErrNotFound = fmt.Errorf("not found: %w", pgx.ErrNoRows)
ErrAlreadyExists = errors.New("already exists")
ErrSerialization = errors.New("serialization error")
ErrNotASlice = errors.New("results must be a pointer to a slice")
Expand Down
4 changes: 3 additions & 1 deletion db/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func (d *dbTx) Get(dest interface{}, query string, args ...interface{}) error {
"took": time.Since(start),
})
err := pgxscan.Get(context.Background(), d.tx, dest, query, args...)
if errors.Is(err, pgx.ErrNoRows) {
if pgxscan.NotFound(err) {
// Don't wrap this err: it might come from a different version of pgx and then
// !errors.Is(err, pgx.ErrNoRows).
log.Trace("SQL query returned no results")
return ErrNotFound
}
Expand Down
90 changes: 90 additions & 0 deletions db/tx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package db_test

import (
"errors"
"testing"

"github.com/treeverse/lakefs/db"
"github.com/treeverse/lakefs/db/params"
)

func getDB(t *testing.T) db.Database {
t.Helper()
ret, err := db.ConnectDB(params.Database{Driver: "pgx", ConnectionString: databaseURI})
if err != nil {
t.Fatal("failed to get DB")
}
return ret
}

func TestGetPrimitive(t *testing.T) {
d := getDB(t)

t.Run("success", func(t *testing.T) {
ret, err := d.Transact(func(tx db.Tx) (interface{}, error) {
var i int64
err := tx.GetPrimitive(&i, "SELECT 17")
return i, err
})

if err != nil {
t.Errorf("failed to SELECT 17: %s", err)
}
i := ret.(int64)
if i != 17 {
t.Errorf("got %d not 17 from SELECT 17", i)
}
})

t.Run("failure", func(t *testing.T) {
_, err := d.Transact(func(tx db.Tx) (interface{}, error) {
var i int64
err := tx.GetPrimitive(&i, "SELECT 17 WHERE 2=1")
return i, err
})

if !errors.Is(err, db.ErrNotFound) {
t.Errorf("got %s wanted not found", err)
}
})
}

func TestGet(t *testing.T) {
type R struct {
A int64
B string
}

d := getDB(t)

t.Run("success", func(t *testing.T) {
ret, err := d.Transact(func(tx db.Tx) (interface{}, error) {
var r R
err := tx.Get(&r, "SELECT 17 A, 'foo' B")
return &r, err
})

if err != nil {
t.Errorf("failed to SELECT 17 and 'foo': %s", err)
}
r := ret.(*R)
if r.A != 17 {
t.Errorf("got %+v with A != 17 from SELECT 17 and 'foo'", r)
}
if r.B != "foo" {
t.Errorf("got %+v with B != 'foo' from SELECT 17 and 'foo'", r)
}
})

t.Run("failure", func(t *testing.T) {
_, err := d.Transact(func(tx db.Tx) (interface{}, error) {
var r R
err := tx.Get(&r, "SELECT 17 A, 'foo' B WHERE 2=1")
return &r, err
})

if !errors.Is(err, db.ErrNotFound) {
t.Errorf("got %s wanted not found", err)
}
})
}