Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' into dependabot/go_modules/master/github.com/bi…
Browse files Browse the repository at this point in the history
…tcoin-sv/go-broadcast-client-0.16.0
  • Loading branch information
arkadiuszos4chain authored Jan 17, 2024
2 parents 100c378 + f02b85e commit bc79472
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 46 deletions.
12 changes: 6 additions & 6 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 18 additions & 18 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 36 additions & 22 deletions model_save.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

// Save will save the model(s) into the Datastore
func Save(ctx context.Context, model ModelInterface) (err error) {

// Check for a client
c := model.Client()
if c == nil {
Expand All @@ -26,16 +25,9 @@ func Save(ctx context.Context, model ModelInterface) (err error) {
// @siggi: we need this to be in a callback context for Mongo
// NOTE: a DB error is not being returned from here
return ds.NewTx(ctx, func(tx *datastore.Transaction) (err error) {

// Fire the before hooks (parent model)
if model.IsNew() {
if err = model.BeforeCreating(ctx); err != nil {
return
}
} else {
if err = model.BeforeUpdating(ctx); err != nil {
return
}
parentBeforeHook := _beforeHook(model)
if err = parentBeforeHook(ctx); err != nil {
return _closeTxWithError(tx, err)
}

// Set the record's timestamps
Expand All @@ -47,16 +39,11 @@ func Save(ctx context.Context, model ModelInterface) (err error) {
// Add any child models (fire before hooks)
if children := model.ChildModels(); len(children) > 0 {
for _, child := range children {
if child.IsNew() {
if err = child.BeforeCreating(ctx); err != nil {
return
}
} else {
if err = child.BeforeUpdating(ctx); err != nil {
return
}
}

childBeforeHook := _beforeHook(child)
if err = childBeforeHook(ctx); err != nil {
return _closeTxWithError(tx, err)
}
// Set the record's timestamps
child.SetRecordTime(child.IsNew())
}
Expand All @@ -76,7 +63,7 @@ func Save(ctx context.Context, model ModelInterface) (err error) {
if err = modelsToSave[index].Client().Datastore().SaveModel(
ctx, modelsToSave[index], tx, modelsToSave[index].IsNew(), false,
); err != nil {
return
return _closeTxWithError(tx, err)
}
}

Expand Down Expand Up @@ -104,7 +91,6 @@ func Save(ctx context.Context, model ModelInterface) (err error) {
err = errors.Wrap(err, afterErr.Error())
}
}
// modelToSave.NotNew() // NOTE: moved to above from here
}

return
Expand All @@ -129,3 +115,31 @@ func saveToCache(ctx context.Context, keys []string, model ModelInterface, ttl t
}
return nil
}

// _closeTxWithError will close the transaction with the given error
// It's crucial to run this rollback to prevent hanging db connections.
func _closeTxWithError(tx *datastore.Transaction, baseError error) error {
if tx == nil {
if baseError != nil {
return baseError
}
return errors.New("transaction is nil during rollback")
}
if err := tx.Rollback(); err != nil {
if baseError != nil {
return errors.Wrap(baseError, err.Error())
}
return err
}
if baseError != nil {
return baseError
}
return errors.New("closing transaction with error")
}

func _beforeHook(model ModelInterface) func(context.Context) error {
if model.IsNew() {
return model.BeforeCreating
}
return model.BeforeUpdating
}

0 comments on commit bc79472

Please sign in to comment.