Skip to content

Commit

Permalink
Properly delete losing leaf revs
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Feb 19, 2024
1 parent 1b295e9 commit 9d497d8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
18 changes: 10 additions & 8 deletions x/sqlite/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,23 @@ func (d *db) Delete(ctx context.Context, docID string, options driver.Options) (
}
defer tx.Rollback()

var curRev revision
var found bool
err = tx.QueryRowContext(ctx, fmt.Sprintf(`
SELECT rev, rev_id
FROM %q
WHERE id = $1
ORDER BY rev DESC, rev_id DESC
LIMIT 1
`, d.name), data.ID).Scan(&curRev.rev, &curRev.id)
SELECT child.id IS NULL
FROM %[2]q AS rev
LEFT JOIN %[2]q AS child ON rev.id = child.id AND rev.rev = child.parent_rev AND rev.rev_id = child.parent_rev_id
JOIN %[1]q AS doc ON rev.id = doc.id AND rev.rev = doc.rev AND rev.rev_id = doc.rev_id
WHERE rev.id = $1
AND rev.rev = $2
AND rev.rev_id = $3
`, d.name, d.name+"_revs"), data.ID, delRev.rev, delRev.id).Scan(&found)
switch {
case errors.Is(err, sql.ErrNoRows):
return "", &internal.Error{Status: http.StatusNotFound, Message: "not found"}
case err != nil:
return "", err

Check warning on line 65 in x/sqlite/delete.go

View check run for this annotation

Codecov / codecov/patch

x/sqlite/delete.go#L64-L65

Added lines #L64 - L65 were not covered by tests
}
if curRev.String() != optRev {
if !found {
return "", &internal.Error{Status: http.StatusConflict, Message: "conflict"}
}
var r revision
Expand Down
4 changes: 2 additions & 2 deletions x/sqlite/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestDBDelete(t *testing.T) {
wantErr: "conflict",
},
{
name: "delete losing rev for conflict",
name: "delete losing rev for conflict should succeed",
setup: func(t *testing.T, db driver.DB) {
_, err := db.Put(context.Background(), "foo", map[string]string{
"cat": "meow",
Expand All @@ -139,7 +139,7 @@ func TestDBDelete(t *testing.T) {
},
id: "foo",
options: kivik.Rev("1-aaa"),
wantRev: "2-xxxxx",
wantRev: "2-df2a4fe30cde39c357c8d1105748d1b9",
},
{
name: "invalid rev format",
Expand Down

0 comments on commit 9d497d8

Please sign in to comment.