skip triggering the before hooks on record delete retry

This commit is contained in:
Gani Georgiev 2022-12-01 19:00:38 +02:00
parent 0fa5edb0b1
commit 44a69eb4ba
1 changed files with 20 additions and 2 deletions

View File

@ -353,7 +353,7 @@ func (dao *Dao) DeleteRecord(record *models.Record) error {
attempts := 1 attempts := 1
Retry: Retry:
err := dao.deleteRecord(record) err := dao.deleteRecord(record, attempts)
if err != nil && if err != nil &&
attempts <= maxAttempts && attempts <= maxAttempts &&
// note: we are checking the error msg so that we can handle both the cgo and noncgo errors // note: we are checking the error msg so that we can handle both the cgo and noncgo errors
@ -366,8 +366,26 @@ Retry:
return err return err
} }
func (dao *Dao) deleteRecord(record *models.Record) error { func (dao *Dao) deleteRecord(record *models.Record, attempts int) error {
return dao.RunInTransaction(func(txDao *Dao) error { return dao.RunInTransaction(func(txDao *Dao) error {
// unset transaction dao before hook on retry to avoid
// triggering the same before callbacks multiple times
if attempts > 1 {
oldBeforeCreateFunc := txDao.BeforeCreateFunc
oldBeforeUpdateFunc := txDao.BeforeUpdateFunc
oldBeforeDeleteFunc := txDao.BeforeDeleteFunc
txDao.BeforeCreateFunc = nil
txDao.BeforeUpdateFunc = nil
txDao.BeforeDeleteFunc = nil
defer func() {
if txDao != nil {
txDao.BeforeCreateFunc = oldBeforeCreateFunc
txDao.BeforeUpdateFunc = oldBeforeUpdateFunc
txDao.BeforeDeleteFunc = oldBeforeDeleteFunc
}
}()
}
// check for references // check for references
refs, err := txDao.FindCollectionReferences(record.Collection()) refs, err := txDao.FindCollectionReferences(record.Collection())
if err != nil { if err != nil {