Skip to content

Commit

Permalink
Fix panic (#18)
Browse files Browse the repository at this point in the history
* reproduce panice when calling get one in transaction

* pass scan.API while creating tx Querier
  • Loading branch information
LukasJenicek authored Jun 28, 2024
1 parent c15c341 commit 565f9af
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pgkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type DB struct {
}

func (d *DB) TxQuery(tx pgx.Tx) *Querier {
return &Querier{tx: tx, SQL: d.SQL, pool: d.Conn}
return &Querier{tx: tx, SQL: d.SQL, pool: d.Conn, Scan: d.Query.Scan}
}

type Config struct {
Expand Down
17 changes: 17 additions & 0 deletions tests/pgkit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,23 @@ func TestSugarInsertAndSelectRecords(t *testing.T) {
assert.Equal(t, "joe", accounts[0].Name)
}

func TestGetOneInTransaction(t *testing.T) {
ctx := context.Background()
q2 := DB.SQL.Select("*").From("accounts")

var account Account
tx, err := DB.Conn.BeginTx(ctx, pgx.TxOptions{})
assert.NoError(t, err)

defer tx.Rollback(ctx)

err = DB.TxQuery(tx).GetOne(ctx, q2, &account)
assert.NoError(t, err)

err = tx.Commit(ctx)
assert.NoError(t, err)
}

func TestSugarInsertAndSelectRecordsReturningID(t *testing.T) {
truncateTable(t, "accounts")

Expand Down

0 comments on commit 565f9af

Please sign in to comment.