From c4edeb65ec41569caac728826c1a415b95c79aa9 Mon Sep 17 00:00:00 2001 From: Insomnius Date: Thu, 1 Dec 2022 15:45:34 +0700 Subject: [PATCH] Adding method to eject real database --- db/adapter.go | 5 +++++ db/adapter_test.go | 10 ++++++++++ db/db.go | 3 +++ 3 files changed, 18 insertions(+) diff --git a/db/adapter.go b/db/adapter.go index a1db514..7da97d4 100644 --- a/db/adapter.go +++ b/db/adapter.go @@ -105,3 +105,8 @@ func runWithSQLAnalyzer(ktx kontext.Context, executionLevel, function string, f } return nil } + +// Eject sql.DB out of db adapter +func (a *Adapter) Eject() *sql.DB { + return a.db +} diff --git a/db/adapter_test.go b/db/adapter_test.go index 9a1b741..abe2389 100644 --- a/db/adapter_test.go +++ b/db/adapter_test.go @@ -43,6 +43,16 @@ func TestAdapter(t *testing.T) { }) }) + t.Run("Eject", func(t *testing.T) { + t.Run("When ejected it return *sql.DB instance", func(t *testing.T) { + sqldb, _, _ := sqlmock.New(sqlmock.MonitorPingsOption(true)) + defer sqldb.Close() + + sql := db.Adapt(sqldb) + assert.Equal(t, sqldb, sql.Eject()) + }) + }) + t.Run("QueryRowContext", func(t *testing.T) { t.Run("When querying done it will return db.Row and scan the value", func(t *testing.T) { diff --git a/db/db.go b/db/db.go index b9f9d33..1d4d12a 100644 --- a/db/db.go +++ b/db/db.go @@ -1,6 +1,8 @@ package db import ( + "database/sql" + "github.com/kodefluence/monorepo/exception" "github.com/kodefluence/monorepo/kontext" ) @@ -12,6 +14,7 @@ type DB interface { Ping(ktx kontext.Context) exception.Exception Transactionable TX + Eject() *sql.DB } // Transactionable is wrapper to create transaction process