Skip to content

Commit

Permalink
fix: better behavior when saving non-existent instance (#462)
Browse files Browse the repository at this point in the history
Signed-off-by: Carson Farmer <carson.farmer@gmail.com>
  • Loading branch information
carsonfarmer authored Nov 2, 2020
1 parent 8be7022 commit de1407d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
25 changes: 9 additions & 16 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,23 +553,16 @@ func (t *Txn) createSaveActions(identity thread.PubKey, updated ...[]byte) ([]co
key := baseKey.ChildString(t.collection.name).ChildString(id.String())
previous, err := t.collection.db.datastore.Get(key)
if err == ds.ErrNotFound {
a := core.Action{
Type: core.Create,
InstanceID: id,
CollectionName: t.collection.name,
Previous: nil,
Current: next,
}
t.actions = append(t.actions, a)
// Early out and make this a create event. Assumes valid _id as specified above
continue
}
if err != nil {
return nil, err
}
previous, err = t.collection.filterRead(identity, previous)
if err != nil {
// Default to an empty doc, downstream reducer will take care of patching, etc
previous = []byte("{}")
} else if err != nil {
return nil, err
} else {
// No errors, carry on
previous, err = t.collection.filterRead(identity, previous)
if err != nil {
return nil, err
}
}

actions = append(actions, core.Action{
Expand Down
29 changes: 28 additions & 1 deletion db/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,34 @@ func TestSaveInstance(t *testing.T) {

p := util.JSONFromInstance(Person{Name: "Alice", Age: 42})
if err := m.Save(p); err != nil {
t.Fatal("should have saved non-existent instasnce")
t.Fatal("should have saved non-existent instance")
}

// Create valid, predefined ID
ID := core.NewInstanceID()
// Update an instance **twice** in the same transaction, **without** a create op
err = m.WriteTxn(func(txn *Txn) error {
// Save non-existent instance
p := Person{Name: "Bob", Age: 37, ID: ID}
if err := txn.Save(util.JSONFromInstance(p)); err != nil {
return err
}
// Now update it in the **same** transaction
p.Age = 44
if err := txn.Save(util.JSONFromInstance(p)); err != nil {
return err
}
return nil
})
checkErr(t, err)

// Make sure bob is in there
instance, err := m.FindByID(ID)
checkErr(t, err)
person := &Person{}
util.InstanceFromJSON(instance, person)
if person.ID != ID || person.Age != 44 || person.Name != "Bob" {
t.Fatalf(errInvalidInstanceState)
}
})
}
Expand Down
7 changes: 3 additions & 4 deletions jsonpatcher/jsonpatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

jsonpatch "github.com/evanphx/json-patch"
cbornode "github.com/ipfs/go-ipld-cbor"
"github.com/ipfs/go-ipld-format"
format "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log"
"github.com/multiformats/go-multihash"
ds "github.com/textileio/go-datastore"
Expand Down Expand Up @@ -149,9 +149,8 @@ func (jp *jsonPatcher) Reduce(events []core.Event, datastore ds.TxnDatastore, ba
case save:
value, err := txn.Get(key)
if errors.Is(err, ds.ErrNotFound) {
return nil, errSavingNonExistentInstance
}
if err != nil {
value = []byte("{}")
} else if err != nil {
return nil, err
}
patchedValue, err := jsonpatch.MergePatch(value, je.Patch.JSONPatch)
Expand Down

0 comments on commit de1407d

Please sign in to comment.