forked from mirror/gorm
Add DB.CommonDB() instead of DB.Tx(), as discussed in the PR thread.
This commit is contained in:
parent
a336f51444
commit
42448cb5d6
15
main.go
15
main.go
|
@ -28,20 +28,15 @@ func Open(driver, source string) (DB, error) {
|
|||
return db, err
|
||||
}
|
||||
|
||||
// Return the underlying sql.DB instance.
|
||||
//
|
||||
// If called inside a transaction, it will panic.
|
||||
// Use Tx() instead in this case.
|
||||
func (s *DB) DB() *sql.DB {
|
||||
return s.db.(*sql.DB)
|
||||
}
|
||||
|
||||
// Return the underlying sql.Tx instance.
|
||||
//
|
||||
// If called outside of a transaction, it will panic.
|
||||
// Use DB() instead in this case.
|
||||
func (s *DB) Tx() *sql.Tx {
|
||||
return s.db.(*sql.Tx)
|
||||
// Return the underlying sql.DB or sql.Tx instance.
|
||||
// Use of this method is discouraged. It's mainly intended to allow
|
||||
// coexistence with legacy non-GORM code.
|
||||
func (s *DB) CommonDB() sqlCommon {
|
||||
return s.db
|
||||
}
|
||||
|
||||
func (s *DB) Callback() *callback {
|
||||
|
|
|
@ -1542,10 +1542,9 @@ func TestTransaction(t *testing.T) {
|
|||
t.Errorf("Should find saved record, but got", err)
|
||||
}
|
||||
|
||||
sql_tx := tx.Tx() // This shouldn't panic.
|
||||
if sql_tx == nil {
|
||||
t.Errorf("Should return the underlying sql.Tx, but got nil")
|
||||
}
|
||||
if sql_tx, ok := tx.CommonDB().(*sql.Tx); !ok || sql_tx == nil {
|
||||
t.Errorf("Should return the underlying sql.Tx")
|
||||
}
|
||||
|
||||
tx.Rollback()
|
||||
|
||||
|
|
Loading…
Reference in New Issue