From 31a0964e124e7e370fd8091f4a367e52e35a692c Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Sun, 12 Nov 2023 19:09:50 -0500 Subject: [PATCH] dbtest: properly close test connection (#7598) --- internal/dbtest/dbtest.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/dbtest/dbtest.go b/internal/dbtest/dbtest.go index 4e6b7e6a3..5fab7b136 100644 --- a/internal/dbtest/dbtest.go +++ b/internal/dbtest/dbtest.go @@ -55,7 +55,12 @@ func NewDB(t *testing.T, suite string, tables ...any) *gorm.DB { dbOpts.Name = dbName - cleanup = func(_ *gorm.DB) { + cleanup = func(db *gorm.DB) { + testDB, err := db.DB() + if err == nil { + _ = testDB.Close() + } + _, _ = sqlDB.Exec(fmt.Sprintf("DROP DATABASE `%s`", dbName)) _ = sqlDB.Close() } @@ -86,7 +91,12 @@ func NewDB(t *testing.T, suite string, tables ...any) *gorm.DB { dbOpts.Name = dbName - cleanup = func(_ *gorm.DB) { + cleanup = func(db *gorm.DB) { + testDB, err := db.DB() + if err == nil { + _ = testDB.Close() + } + _, _ = sqlDB.Exec(fmt.Sprintf(`DROP DATABASE %q`, dbName)) _ = sqlDB.Close() }