Add RollbackUnlessCommitted()
This commit is contained in:
parent
742154be9a
commit
eaece32546
17
main.go
17
main.go
@ -523,6 +523,23 @@ func (s *DB) Rollback() *DB {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RollbackUnlessCommitted rollback a transaction if it has not yet been
|
||||||
|
// committed.
|
||||||
|
func (s *DB) RollbackUnlessCommitted() *DB {
|
||||||
|
var emptySQLTx *sql.Tx
|
||||||
|
if db, ok := s.db.(sqlTx); ok && db != nil && db != emptySQLTx {
|
||||||
|
err := db.Rollback()
|
||||||
|
// Ignore the error indicating that the transaction has already
|
||||||
|
// been committed.
|
||||||
|
if err != sql.ErrTxDone {
|
||||||
|
s.AddError(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
s.AddError(ErrInvalidTransaction)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
// NewRecord check if value's primary key is blank
|
// NewRecord check if value's primary key is blank
|
||||||
func (s *DB) NewRecord(value interface{}) bool {
|
func (s *DB) NewRecord(value interface{}) bool {
|
||||||
return s.NewScope(value).PrimaryKeyZero()
|
return s.NewScope(value).PrimaryKeyZero()
|
||||||
|
34
main_test.go
34
main_test.go
@ -394,6 +394,40 @@ func TestTransaction(t *testing.T) {
|
|||||||
if err := DB.First(&User{}, "name = ?", "transcation-2").Error; err != nil {
|
if err := DB.First(&User{}, "name = ?", "transcation-2").Error; err != nil {
|
||||||
t.Errorf("Should be able to find committed record")
|
t.Errorf("Should be able to find committed record")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tx3 := DB.Begin()
|
||||||
|
u3 := User{Name: "transcation-3"}
|
||||||
|
if err := tx3.Save(&u3).Error; err != nil {
|
||||||
|
t.Errorf("No error should raise")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx3.First(&User{}, "name = ?", "transcation-3").Error; err != nil {
|
||||||
|
t.Errorf("Should find saved record")
|
||||||
|
}
|
||||||
|
|
||||||
|
tx3.RollbackUnlessCommitted()
|
||||||
|
|
||||||
|
if err := tx.First(&User{}, "name = ?", "transcation").Error; err == nil {
|
||||||
|
t.Errorf("Should not find record after rollback")
|
||||||
|
}
|
||||||
|
|
||||||
|
tx4 := DB.Begin()
|
||||||
|
u4 := User{Name: "transcation-4"}
|
||||||
|
if err := tx4.Save(&u4).Error; err != nil {
|
||||||
|
t.Errorf("No error should raise")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx4.First(&User{}, "name = ?", "transcation-4").Error; err != nil {
|
||||||
|
t.Errorf("Should find saved record")
|
||||||
|
}
|
||||||
|
|
||||||
|
tx4.Commit()
|
||||||
|
|
||||||
|
tx4.RollbackUnlessCommitted()
|
||||||
|
|
||||||
|
if err := DB.First(&User{}, "name = ?", "transcation-4").Error; err != nil {
|
||||||
|
t.Errorf("Should be able to find committed record")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRow(t *testing.T) {
|
func TestRow(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user