Skip to content

Commit

Permalink
Better handling for document.update TX
Browse files Browse the repository at this point in the history
  • Loading branch information
sauls8t committed Oct 30, 2019
1 parent 811e239 commit 2d105f2
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions domain/document/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,18 @@ func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {

d.RefID = documentID

ctx.Transaction, err = h.Runtime.Db.Beginx()
if err != nil {
var ok bool
ctx.Transaction, ok = h.Runtime.StartTx(sql.LevelReadUncommitted)
if !ok {
h.Runtime.Log.Info("unable to start transaction " + method)
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
}

// If space changed for document, remove document categories.
oldDoc, err := h.Store.Document.Get(ctx, documentID)
if err != nil {
ctx.Transaction.Rollback()
h.Runtime.Rollback(ctx.Transaction)
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
Expand All @@ -251,7 +252,7 @@ func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
h.Store.Category.RemoveDocumentCategories(ctx, d.RefID)
err = h.Store.Document.MoveActivity(ctx, documentID, oldDoc.SpaceID, d.SpaceID)
if err != nil {
ctx.Transaction.Rollback()
h.Runtime.Rollback(ctx.Transaction)
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
Expand All @@ -260,7 +261,7 @@ func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {

err = h.Store.Document.Update(ctx, d)
if err != nil {
ctx.Transaction.Rollback()
h.Runtime.Rollback(ctx.Transaction)
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
Expand All @@ -272,7 +273,7 @@ func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
if len(d.GroupID) > 0 {
err = h.Store.Document.UpdateGroup(ctx, d)
if err != nil {
ctx.Transaction.Rollback()
h.Runtime.Rollback(ctx.Transaction)
response.WriteServerError(w, method, err)
h.Runtime.Log.Error(method, err)
return
Expand Down Expand Up @@ -309,12 +310,13 @@ func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
}
}

ctx.Transaction.Commit()
h.Runtime.Commit(ctx.Transaction)

h.Store.Space.SetStats(ctx, d.SpaceID)
if oldDoc.SpaceID != d.SpaceID {
h.Store.Space.SetStats(ctx, oldDoc.SpaceID)
}

h.Store.Audit.Record(ctx, audit.EventTypeDocumentUpdate)

// Live document indexed for search.
Expand Down

0 comments on commit 2d105f2

Please sign in to comment.