Skip to content

Commit

Permalink
Select winning rev after deleting conflicting leaf in AllDocs
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Feb 19, 2024
1 parent 74bfefb commit 34ce793
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
18 changes: 11 additions & 7 deletions x/sqlite/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ func (d *db) AllDocs(ctx context.Context, options driver.Options) (driver.Rows,
query := fmt.Sprintf(`
WITH RankedRevisions AS (
SELECT
id AS id,
rev || '-' || rev_id AS rev,
IIF($1, doc, NULL) AS doc,
deleted AS deleted,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY rev DESC, rev_id DESC) AS rank
FROM %[1]q
rev.id AS id,
rev.rev || '-' || rev.rev_id AS rev,
IIF($1, doc.doc, NULL) AS doc,
doc.deleted AS deleted,
ROW_NUMBER() OVER (PARTITION BY rev.id ORDER BY rev.rev DESC, rev.rev_id DESC) AS rank
FROM %[1]q AS rev
LEFT JOIN %[1]q AS child ON rev.id = child.id AND rev.rev = child.parent_rev AND rev.rev_id = child.parent_rev_id
JOIN %[2]q AS doc ON rev.id = doc.id AND rev.rev = doc.rev AND rev.rev_id = doc.rev_id
WHERE child.id IS NULL
AND NOT doc.deleted
)
SELECT
id,
Expand All @@ -46,7 +50,7 @@ func (d *db) AllDocs(ctx context.Context, options driver.Options) (driver.Rows,
FROM RankedRevisions
WHERE rank = 1
AND NOT deleted
`, d.name)
`, d.name+"_revs", d.name)
results, err := d.db.QueryContext(ctx, query, optIncludeDocs) //nolint:rowserrcheck // Err checked in Next
if err != nil {
return nil, err

Check warning on line 56 in x/sqlite/views.go

View check run for this annotation

Codecov / codecov/patch

x/sqlite/views.go#L56

Added line #L56 was not covered by tests
Expand Down
30 changes: 30 additions & 0 deletions x/sqlite/views_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,36 @@ func TestDBAllDocs(t *testing.T) {
},
want: nil,
},
{
name: "conflicting document, select winning rev",
setup: func(t *testing.T, db driver.DB) {
_, err := db.Put(context.Background(), "foo", map[string]string{
"cat": "meow",
"_rev": "1-xxx",
}, kivik.Param("new_edits", false))
if err != nil {
t.Fatal(err)
}
_, err = db.Put(context.Background(), "foo", map[string]string{
"cat": "purr",
"_rev": "1-aaa",
}, kivik.Param("new_edits", false))
if err != nil {
t.Fatal(err)
}
_, err = db.Delete(context.Background(), "foo", kivik.Rev("1-aaa"))
if err != nil {
t.Fatal(err)
}
},
want: []rowResult{
{
ID: "foo",
Rev: "1-xxx",
Value: `{"value":{"rev":"1-xxx"}}` + "\n",
},
},
},
/*
TODO:
- deleted doc
Expand Down

0 comments on commit 34ce793

Please sign in to comment.