VOL-1468 : Ensure hash is kept and latest is set

Change-Id: I6ea53b7da41963c9ecce4df2c6d9a320c79930fc
diff --git a/db/model/node.go b/db/model/node.go
index 691ea25..77ca565 100644
--- a/db/model/node.go
+++ b/db/model/node.go
@@ -705,7 +705,7 @@
 
 	if field.IsContainer {
 		if path == "" {
-			log.Errorf("cannot remove without a key")
+			log.Errorw("cannot-remove-without-key", log.Fields{"name": name, "key": path})
 		} else if field.Key != "" {
 			partition := strings.SplitN(path, "/", 2)
 			key := partition[0]
@@ -714,9 +714,11 @@
 			} else {
 				path = partition[1]
 			}
+
 			keyValue := field.KeyFromStr(key)
 			children = make([]Revision, len(rev.GetChildren(name)))
 			copy(children, rev.GetChildren(name))
+
 			if path != "" {
 				idx, childRev := n.findRevByKey(children, field.Key, keyValue)
 				childNode := childRev.GetNode()
@@ -730,26 +732,32 @@
 				n.makeLatest(branch, rev, nil)
 				return nil
 			}
-			idx, childRev := n.findRevByKey(children, field.Key, keyValue)
-			if n.GetProxy() != nil {
-				data := childRev.GetData()
-				n.GetProxy().InvokeCallbacks(PRE_REMOVE, false, data)
-				postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, data, nil})
-			} else {
-				postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, childRev.GetData(), nil})
-			}
-			childRev.Drop(txid, true)
-			children = append(children[:idx], children[idx+1:]...)
-			rev.SetChildren(name, children)
-			branch.GetLatest().Drop(txid, false)
-			n.makeLatest(branch, rev, postAnnouncement)
-			return rev
 
+			if idx, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil {
+				if n.GetProxy() != nil {
+					data := childRev.GetData()
+					n.GetProxy().InvokeCallbacks(PRE_REMOVE, false, data)
+					postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, data, nil})
+				} else {
+					postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, childRev.GetData(), nil})
+				}
+
+				childRev.Drop(txid, true)
+				children = append(children[:idx], children[idx+1:]...)
+				rev.SetChildren(name, children)
+
+				branch.GetLatest().Drop(txid, false)
+				n.makeLatest(branch, rev, postAnnouncement)
+
+				return rev
+			} else {
+				log.Errorw("failed-to-find-revision", log.Fields{"name": name, "key": keyValue.(string)})
+			}
 		}
-		log.Errorf("cannot add to non-keyed container")
+		log.Errorw("cannot-add-to-non-keyed-container", log.Fields{"name": name, "path": path, "fieldKey": field.Key})
 
 	} else {
-		log.Errorf("cannot add to non-container field")
+		log.Errorw("cannot-add-to-non-container-field", log.Fields{"name": name, "path": path})
 	}
 
 	return nil
diff --git a/db/model/persisted_revision.go b/db/model/persisted_revision.go
index 39bef5e..fa35eca 100644
--- a/db/model/persisted_revision.go
+++ b/db/model/persisted_revision.go
@@ -141,9 +141,11 @@
 						rev := branch.GetLatest()
 						updatedRev := rev.UpdateData(data.Interface(), branch)
 
-						// The changeAnnouncement field should remain 'nil' to prevent
-						// update callbacks from being executed.
-						rev.GetNode().makeLatest(branch, updatedRev, nil)
+						// ensure that we keep the previous hash value
+						updatedRev.SetHash(rev.GetHash())
+
+						// Save revision
+						branch.SetLatest(updatedRev)
 					}
 				}