Skip to content

Commit 3095413

Browse files
committed
Fix critical db.DB() method returning nil - v0.1.3
### Critical Fix - Fixed db.DB() method returning nil by implementing correct GetDBConn() interface - Corrected interface method name from GetDBConnector() to GetDBConn() - Added comprehensive test coverage for db.DB() method functionality ### Issue Resolved External projects were experiencing nil pointer panics when calling: This was caused by GORM expecting GetDBConn() interface method, not GetDBConnector(). ### Validation - All existing tests pass - New TestDBMethod specifically validates db.DB() functionality - Fixes integration issues reported by external projects This resolves compatibility issues with GORM's standard database access patterns.
1 parent b8ba01d commit 3095413

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@ All notable changes to the GORM DuckDB driver will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.1.3] - 2025-06-22
9+
10+
### Fixed
11+
12+
- **Critical**: Fixed `db.DB()` method returning `nil` by implementing correct `GetDBConn()` interface
13+
- Added comprehensive test coverage for `db.DB()` method functionality
14+
- Resolved issue where external projects couldn't access underlying `*sql.DB` instance
15+
16+
### Changed
17+
18+
- Corrected interface method name from `GetDBConnector()` to `GetDBConn()` to match GORM expectations
19+
- Enhanced test suite with specific `db.DB()` method validation
20+
21+
### Technical Details
22+
23+
The connection pool wrapper now properly implements the `GetDBConn() (*sql.DB, error)` interface that GORM's `db.DB()` method expects. This resolves the critical issue where:
24+
25+
- `db.DB()` was returning `nil` instead of the underlying `*sql.DB` instance
26+
- External projects couldn't perform database cleanup or connection management
27+
- Integration tests failed when trying to close database connections
28+
29+
This fix ensures full compatibility with GORM's standard database access patterns.
30+
831
## [0.1.2] - 2025-06-22
932

1033
### Fixed

dialector.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,16 +273,17 @@ func (p *duckdbConnPoolWrapper) QueryRowContext(ctx context.Context, query strin
273273
return p.ConnPool.QueryRowContext(ctx, query, convertedArgs...)
274274
}
275275

276-
// Implement GetDBConnector interface to allow access to underlying *sql.DB
277-
func (p *duckdbConnPoolWrapper) GetDBConnector() (*sql.DB, error) {
278-
if dbConnector, ok := p.ConnPool.(interface{ GetDBConnector() (*sql.DB, error) }); ok {
279-
return dbConnector.GetDBConnector()
280-
}
281-
276+
// GetDBConn implements the interface expected by GORM's db.DB() method
277+
func (p *duckdbConnPoolWrapper) GetDBConn() (*sql.DB, error) {
282278
// If the wrapped ConnPool is directly *sql.DB, return it
283279
if db, ok := p.ConnPool.(*sql.DB); ok {
284280
return db, nil
285281
}
286282

283+
// Check if the wrapped ConnPool implements GetDBConn
284+
if dbConn, ok := p.ConnPool.(interface{ GetDBConn() (*sql.DB, error) }); ok {
285+
return dbConn.GetDBConn()
286+
}
287+
287288
return nil, fmt.Errorf("unable to get underlying *sql.DB from connection pool")
288289
}

duckdb_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,43 @@ func TestMigration(t *testing.T) {
193193
t.Error("Expected extra column to exist after migration")
194194
}
195195
}
196+
197+
func TestDBMethod(t *testing.T) {
198+
// Test that db.DB() method works correctly
199+
db, err := gorm.Open(Open(":memory:"), &gorm.Config{})
200+
if err != nil {
201+
t.Fatalf("Failed to connect to database: %v", err)
202+
}
203+
204+
// Test getting the underlying *sql.DB
205+
sqlDB, err := db.DB()
206+
if err != nil {
207+
t.Fatalf("Failed to get *sql.DB: %v", err)
208+
}
209+
210+
if sqlDB == nil {
211+
t.Fatal("db.DB() returned nil - this should not happen")
212+
}
213+
214+
// Test ping
215+
if err := sqlDB.Ping(); err != nil {
216+
t.Fatalf("Failed to ping database: %v", err)
217+
}
218+
219+
// Test setting connection pool settings
220+
sqlDB.SetMaxIdleConns(5)
221+
sqlDB.SetMaxOpenConns(10)
222+
223+
// Test getting stats
224+
stats := sqlDB.Stats()
225+
if stats.MaxOpenConnections != 10 {
226+
t.Errorf("Expected MaxOpenConnections to be 10, got %d", stats.MaxOpenConnections)
227+
}
228+
229+
// Test close (this should work for cleanup)
230+
defer func() {
231+
if err := sqlDB.Close(); err != nil {
232+
t.Errorf("Failed to close database: %v", err)
233+
}
234+
}()
235+
}

0 commit comments

Comments
 (0)