From 999b6d88f8edede65c5c180eb8bb426c561e4787 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Sun, 28 Apr 2019 18:37:44 +0900 Subject: [PATCH 1/2] add nil check for getting read/write queries --- database/sql/tx.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/database/sql/tx.go b/database/sql/tx.go index 0520331..53aec5f 100644 --- a/database/sql/tx.go +++ b/database/sql/tx.go @@ -75,11 +75,17 @@ func (proxy *Tx) AfterCommitCallback(success func() error, failure func(bool, [] // WriteQueries informations of executed INSERT/UPDATE/DELETE query func (proxy *Tx) WriteQueries() []*connection.QueryLog { + if proxy.tx == nil { + return []*connection.QueryLog{} + } return proxy.tx.WriteQueries } // ReadQueries informations of executed SELECT query func (proxy *Tx) ReadQueries() []*connection.QueryLog { + if proxy.tx == nil { + return []*connection.QueryLog{} + } return proxy.tx.ReadQueries } From 239f5753a857fe6ebf42dc4f59279e2e3ce4a90f Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Sun, 28 Apr 2019 18:51:58 +0900 Subject: [PATCH 2/2] add test code for coverage --- database/sql/sql_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/database/sql/sql_test.go b/database/sql/sql_test.go index db39e68..88f024f 100644 --- a/database/sql/sql_test.go +++ b/database/sql/sql_test.go @@ -607,6 +607,12 @@ func TestTransaction(t *testing.T) { defer db.Close() tx, err := db.Begin() checkErr(t, err) + if readQueries := tx.ReadQueries(); len(readQueries) > 0 { + t.Fatal("invalid read queries") + } + if writeQueries := tx.WriteQueries(); len(writeQueries) > 0 { + t.Fatal("invalid write queries") + } ctx, cancel := context.WithCancel(context.Background()) defer cancel() t.Run("prepare context", func(t *testing.T) {