Skip to content
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: 5 additions & 0 deletions sequel.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,11 @@ func (t *Tx) rebindModel(query string) string {
return query
}

// Tx returns the embedded *sql.Tx.
func (t *Tx) Tx() *sql.Tx {
return t.tx.Tx
}

// Commit commits the transaction.
func (t *Tx) Commit() error {
err := t.tx.Commit()
Expand Down
16 changes: 16 additions & 0 deletions sequel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,22 @@ func TestTxQueries(t *testing.T) {
assert.NoError(t, tx.Commit())
})

t.Run("sql tx", func(t *testing.T) {
tx, err := db.Begin(ctx)
require.NoError(t, err)
defer func() {
assert.NoError(t, tx.Rollback())
}()

// A write through the *sql.Tx is visible inside the same transaction.
_, err = tx.Tx().ExecContext(ctx, "UPDATE person_test SET name = $1 WHERE id = $2", "Foo Bar", p1.GetID())
require.NoError(t, err)

var name string
require.NoError(t, tx.QueryRow("SELECT name FROM person_test WHERE id = $1", p1.GetID()).Scan(&name))
assert.Equal(t, "Foo Bar", name)
})

t.Run("insert error", func(t *testing.T) {
tx, err := db.Begin(ctx)
require.NoError(t, err)
Expand Down
Loading