Browse Source

dbtest: properly close test connection (#7598)

Joe Chen 11 months ago
parent
commit
31a0964e12
1 changed files with 12 additions and 2 deletions
  1. 12 2
      internal/dbtest/dbtest.go

+ 12 - 2
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()
 		}