Merge branch 'master' into feature/use_index
This commit is contained in:
commit
52c4497f71
4
main.go
4
main.go
@ -538,7 +538,9 @@ func (s *DB) Commit() *DB {
|
|||||||
func (s *DB) Rollback() *DB {
|
func (s *DB) Rollback() *DB {
|
||||||
var emptySQLTx *sql.Tx
|
var emptySQLTx *sql.Tx
|
||||||
if db, ok := s.db.(sqlTx); ok && db != nil && db != emptySQLTx {
|
if db, ok := s.db.(sqlTx); ok && db != nil && db != emptySQLTx {
|
||||||
s.AddError(db.Rollback())
|
if err := db.Rollback(); err != nil && err != sql.ErrTxDone {
|
||||||
|
s.AddError(err)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
s.AddError(ErrInvalidTransaction)
|
s.AddError(ErrInvalidTransaction)
|
||||||
}
|
}
|
||||||
|
16
main_test.go
16
main_test.go
@ -421,6 +421,22 @@ func TestTransaction(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTransaction_NoErrorOnRollbackAfterCommit(t *testing.T) {
|
||||||
|
tx := DB.Begin()
|
||||||
|
u := User{Name: "transcation"}
|
||||||
|
if err := tx.Save(&u).Error; err != nil {
|
||||||
|
t.Errorf("No error should raise")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Commit().Error; err != nil {
|
||||||
|
t.Errorf("Commit should not raise error")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Rollback().Error; err != nil {
|
||||||
|
t.Errorf("Rollback should not raise error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRow(t *testing.T) {
|
func TestRow(t *testing.T) {
|
||||||
user1 := User{Name: "RowUser1", Age: 1, Birthday: parseTime("2000-1-1")}
|
user1 := User{Name: "RowUser1", Age: 1, Birthday: parseTime("2000-1-1")}
|
||||||
user2 := User{Name: "RowUser2", Age: 10, Birthday: parseTime("2010-1-1")}
|
user2 := User{Name: "RowUser2", Age: 10, Birthday: parseTime("2010-1-1")}
|
||||||
|
2
scope.go
2
scope.go
@ -402,7 +402,7 @@ func (scope *Scope) InstanceGet(name string) (interface{}, bool) {
|
|||||||
// Begin start a transaction
|
// Begin start a transaction
|
||||||
func (scope *Scope) Begin() *Scope {
|
func (scope *Scope) Begin() *Scope {
|
||||||
if db, ok := scope.SQLDB().(sqlDb); ok {
|
if db, ok := scope.SQLDB().(sqlDb); ok {
|
||||||
if tx, err := db.Begin(); err == nil {
|
if tx, err := db.Begin(); scope.Err(err) == nil {
|
||||||
scope.db.db = interface{}(tx).(SQLCommon)
|
scope.db.db = interface{}(tx).(SQLCommon)
|
||||||
scope.InstanceSet("gorm:started_transaction", true)
|
scope.InstanceSet("gorm:started_transaction", true)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user