From 297e93360795d97bdcebeb20db1192b18e51e5b0 Mon Sep 17 00:00:00 2001 From: Mahmood Rahmani Date: Wed, 13 Mar 2019 09:53:54 -0400 Subject: [PATCH 1/3] Support driver.ConnBeginTx (#1) --- sqlhooks.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/sqlhooks.go b/sqlhooks.go index 2964249..c8c3144 100644 --- a/sqlhooks.go +++ b/sqlhooks.go @@ -160,6 +160,13 @@ func (conn *ExecerContext) Exec(query string, args []driver.Value) (driver.Resul return nil, errors.New("Exec was called when ExecContext was implemented") } +func (conn *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) { + if ciCtx, is := conn.Conn.(driver.ConnBeginTx); is { + return ciCtx.BeginTx(ctx, opts) + } + return nil, errors.New("sqlhooks: driver does not support non-default isolation level") +} + // QueryerContext implements a database/sql.driver.QueryerContext type QueryerContext struct { *Conn From 345b1ec84db59974eac3957423ae3a28a1fb1f48 Mon Sep 17 00:00:00 2001 From: Mahmood Rahmani Date: Thu, 14 Mar 2019 12:08:41 -0400 Subject: [PATCH 2/3] Added a test for sqlhooks transaction support (#2) --- sqlhooks_postgres_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sqlhooks_postgres_test.go b/sqlhooks_postgres_test.go index 6a6560b..bf5d92e 100644 --- a/sqlhooks_postgres_test.go +++ b/sqlhooks_postgres_test.go @@ -1,6 +1,7 @@ package sqlhooks import ( + "context" "database/sql" "os" "testing" @@ -53,5 +54,14 @@ func TestPostgres(t *testing.T) { s.db.QueryRow("SELECT COUNT(*) FROM users").Scan(&count), ) assert.Equal(t, 5, count) + + { // Should execute the query successfully when a transaction with non default isolation level is used. + var count int + tx, err := s.db.BeginTx(context.Background(), &sql.TxOptions{Isolation: sql.LevelSerializable}) + if assert.NoError(t, err) { + require.NoError(t, tx.QueryRow("SELECT COUNT(*) FROM users").Scan(&count)) + assert.Equal(t, 5, count) + } + } }) } From 21d73709abd78fd7e5f489075d069480f2c65b4e Mon Sep 17 00:00:00 2001 From: Mahmood Rahmani Date: Fri, 29 Mar 2019 10:22:01 -0400 Subject: [PATCH 3/3] better error message --- sqlhooks.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlhooks.go b/sqlhooks.go index c8c3144..a55d826 100644 --- a/sqlhooks.go +++ b/sqlhooks.go @@ -164,7 +164,7 @@ func (conn *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx if ciCtx, is := conn.Conn.(driver.ConnBeginTx); is { return ciCtx.BeginTx(ctx, opts) } - return nil, errors.New("sqlhooks: driver does not support non-default isolation level") + return nil, errors.New("driver does not implement driver.ConnBeginTx") } // QueryerContext implements a database/sql.driver.QueryerContext