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 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 {
|
func (s *DB) DB() *sql.DB {
|
||||||
return s.db.(*sql.DB)
|
return s.db.(*sql.DB)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the underlying sql.Tx instance.
|
// Return the underlying sql.DB or sql.Tx instance.
|
||||||
//
|
// Use of this method is discouraged. It's mainly intended to allow
|
||||||
// If called outside of a transaction, it will panic.
|
// coexistence with legacy non-GORM code.
|
||||||
// Use DB() instead in this case.
|
func (s *DB) CommonDB() sqlCommon {
|
||||||
func (s *DB) Tx() *sql.Tx {
|
return s.db
|
||||||
return s.db.(*sql.Tx)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DB) Callback() *callback {
|
func (s *DB) Callback() *callback {
|
||||||
|
|
|
@ -1542,9 +1542,8 @@ func TestTransaction(t *testing.T) {
|
||||||
t.Errorf("Should find saved record, but got", err)
|
t.Errorf("Should find saved record, but got", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sql_tx := tx.Tx() // This shouldn't panic.
|
if sql_tx, ok := tx.CommonDB().(*sql.Tx); !ok || sql_tx == nil {
|
||||||
if sql_tx == nil {
|
t.Errorf("Should return the underlying sql.Tx")
|
||||||
t.Errorf("Should return the underlying sql.Tx, but got nil")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
|
|
Loading…
Reference in New Issue