Add RollbackUnlessCommitted() (#2126)

This commit is contained in:
Adam S Levy 2019-06-10 04:19:39 -08:00 committed by Jinzhu
parent ea12400190
commit 44d3060254
2 changed files with 51 additions and 0 deletions

17
main.go
View File

@ -542,6 +542,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()

View File

@ -419,6 +419,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 TestTransaction_NoErrorOnRollbackAfterCommit(t *testing.T) { func TestTransaction_NoErrorOnRollbackAfterCommit(t *testing.T) {