Skip to content

Commit

Permalink
Merge pull request #885 from go-kivik/revConflicts
Browse files Browse the repository at this point in the history
Handle _revisions + _rev + query param conflicts properly
  • Loading branch information
flimzy authored Feb 16, 2024
2 parents d888232 + 5a485fa commit 89860aa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
12 changes: 8 additions & 4 deletions x/sqlite/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,20 @@ func (d *db) Put(ctx context.Context, docID string, doc interface{}, options dri
}
options.Apply(opts)
optsRev, _ := opts["rev"].(string)
data, err := prepareDoc(docID, doc)
if err != nil {
return "", err
}

if data.Revisions.Start != 0 {
docRev = data.Revisions.leaf().String()
}
if optsRev != "" && docRev != "" && optsRev != docRev {
return "", &internal.Error{Status: http.StatusBadRequest, Message: "Document rev and option have different values"}
}
if docRev == "" && optsRev != "" {
docRev = optsRev
}
data, err := prepareDoc(docID, doc)
if err != nil {
return "", err
}

tx, err := d.db.BeginTx(ctx, nil)
if err != nil {
Expand Down
40 changes: 38 additions & 2 deletions x/sqlite/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,45 @@ func TestDBPut(t *testing.T) {
},
},
},
{
name: "new_edits=false, with _revisions and _rev, _revisions wins",
docID: "foo",
doc: map[string]interface{}{
"_revisions": map[string]interface{}{
"ids": []string{"ghi"},
"start": 1,
},
"_rev": "1-abc",
"foo": "bar",
},
options: kivik.Param("new_edits", false),
wantRev: "1-ghi",
wantRevs: []leaf{
{
ID: "foo",
Rev: 1,
RevID: "ghi",
},
},
},
{
name: "new_edits=false, with _revisions and query rev, conflict",
docID: "foo",
doc: map[string]interface{}{
"_revisions": map[string]interface{}{
"ids": []string{"ghi"},
"start": 1,
},
"foo": "bar",
},
options: kivik.Params(map[string]interface{}{
"new_edits": false,
"rev": "1-abc",
}),
wantStatus: http.StatusConflict,
wantErr: "Document rev and option have different values",
},
/*
- _revisions and _rev conflict
- _revisions and rev query parameter conflict
- _revisions replay
- _revisions partial replay (some revs already exist)
- _revisions with some revs and docs already exist
Expand Down

0 comments on commit 89860aa

Please sign in to comment.