-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #932 from go-kivik/purge
Basic purge support & more stub methods
- Loading branch information
Showing
12 changed files
with
292 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// http:https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
|
||
package sqlite | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/go-kivik/kivik/v4" | ||
"github.com/go-kivik/kivik/v4/driver" | ||
) | ||
|
||
func (d *db) Purge(ctx context.Context, request map[string][]string) (*driver.PurgeResult, error) { | ||
tx, err := d.db.Begin() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer tx.Rollback() | ||
|
||
stmts := newStmtCache() | ||
result := &driver.PurgeResult{} | ||
|
||
for docID, revs := range request { | ||
for _, rev := range revs { | ||
r, err := parseRev(rev) | ||
if err != nil { | ||
return nil, err | ||
} | ||
_, err = d.isLeafRev(ctx, tx, docID, r.rev, r.id) | ||
if err != nil { | ||
switch kivik.HTTPStatus(err) { | ||
case http.StatusNotFound, http.StatusConflict: | ||
// Non-leaf rev, do nothing | ||
continue | ||
default: | ||
return nil, err | ||
} | ||
} | ||
|
||
stmt, err := stmts.prepare(ctx, tx, d.query(` | ||
DELETE FROM {{ .Revs }} | ||
WHERE id = $1 AND rev = $2 AND rev_id = $3 | ||
`)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
_, err = stmt.ExecContext(ctx, docID, r.rev, r.id) | ||
if err != nil { | ||
return nil, fmt.Errorf("exec failed: %w", err) | ||
} | ||
if result.Purged == nil { | ||
result.Purged = map[string][]string{} | ||
} | ||
result.Purged[docID] = append(result.Purged[docID], rev) | ||
} | ||
} | ||
|
||
return result, tx.Commit() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// http:https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
|
||
//go:build !js | ||
// +build !js | ||
|
||
package sqlite | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"gitlab.com/flimzy/testy" | ||
|
||
"github.com/go-kivik/kivik/v4" | ||
"github.com/go-kivik/kivik/v4/driver" | ||
) | ||
|
||
func TestDBPurge(t *testing.T) { | ||
t.Parallel() | ||
type test struct { | ||
db *testDB | ||
arg map[string][]string | ||
want *driver.PurgeResult | ||
wantErr string | ||
wantStatus int | ||
wantRevs []leaf | ||
} | ||
tests := testy.NewTable() | ||
tests.Add("nothing to purge", test{ | ||
arg: map[string][]string{ | ||
"foo": {"1-1234", "2-5678"}, | ||
}, | ||
want: &driver.PurgeResult{}, | ||
}) | ||
tests.Add("success", func(t *testing.T) interface{} { | ||
d := newDB(t) | ||
rev := d.tPut("foo", map[string]string{"foo": "bar"}) | ||
|
||
return test{ | ||
db: d, | ||
arg: map[string][]string{ | ||
"foo": {rev}, | ||
}, | ||
want: &driver.PurgeResult{ | ||
Purged: map[string][]string{ | ||
"foo": {rev}, | ||
}, | ||
}, | ||
wantRevs: nil, | ||
} | ||
}) | ||
tests.Add("malformed rev", test{ | ||
arg: map[string][]string{ | ||
"foo": {"abc"}, | ||
}, | ||
wantErr: "invalid rev format", | ||
wantStatus: http.StatusBadRequest, | ||
}) | ||
tests.Add("attempt to purge non-leaf rev does nothing", func(t *testing.T) interface{} { | ||
d := newDB(t) | ||
rev := d.tPut("foo", map[string]string{"foo": "bar"}) | ||
_ = d.tPut("foo", map[string]string{"foo": "baz"}, kivik.Rev(rev)) | ||
|
||
return test{ | ||
db: d, | ||
arg: map[string][]string{ | ||
"foo": {rev}, | ||
}, | ||
want: &driver.PurgeResult{}, | ||
wantRevs: []leaf{ | ||
{ID: "foo", Rev: 1}, | ||
{ID: "foo", Rev: 2, ParentRev: &[]int{1}[0]}, | ||
}, | ||
} | ||
}) | ||
tests.Add("deleting conflict leaves non-conflicting leaf", func(t *testing.T) interface{} { | ||
d := newDB(t) | ||
_ = d.tPut("foo", map[string]interface{}{ | ||
"version": "one", | ||
"_revisions": map[string]interface{}{ | ||
"start": 3, | ||
"ids": []string{"ccc", "bbb", "aaa"}, | ||
}, | ||
}, kivik.Param("new_edits", false)) | ||
_ = d.tPut("foo", map[string]interface{}{ | ||
"version": "two", | ||
"_revisions": map[string]interface{}{ | ||
"start": 3, | ||
"ids": []string{"rrr", "qqq", "aaa"}, | ||
}, | ||
}, kivik.Param("new_edits", false)) | ||
|
||
return test{ | ||
db: d, | ||
arg: map[string][]string{ | ||
"foo": {"3-ccc"}, | ||
}, | ||
want: &driver.PurgeResult{ | ||
Purged: map[string][]string{ | ||
"foo": {"3-ccc"}, | ||
}, | ||
}, | ||
wantRevs: []leaf{ | ||
{ID: "foo", Rev: 1, RevID: "aaa"}, | ||
{ID: "foo", Rev: 2, RevID: "bbb", ParentRev: &[]int{1}[0], ParentRevID: &[]string{"aaa"}[0]}, | ||
{ID: "foo", Rev: 2, RevID: "qqq", ParentRev: &[]int{1}[0], ParentRevID: &[]string{"aaa"}[0]}, | ||
{ID: "foo", Rev: 3, RevID: "rrr", ParentRev: &[]int{2}[0], ParentRevID: &[]string{"qqq"}[0]}, | ||
}, | ||
} | ||
}) | ||
|
||
/* | ||
TODO: | ||
- What happens when purging a leaf, and its parent at the same time? | ||
- what is purge seq? | ||
- refactor: bulk delete, bulk lookup | ||
*/ | ||
|
||
tests.Run(t, func(t *testing.T, tt test) { | ||
t.Parallel() | ||
db := tt.db | ||
if db == nil { | ||
db = newDB(t) | ||
} | ||
got, err := db.Purge(context.Background(), tt.arg) | ||
if !testy.ErrorMatches(tt.wantErr, err) { | ||
t.Errorf("Unexpected error: %s", err) | ||
} | ||
if status := kivik.HTTPStatus(err); status != tt.wantStatus { | ||
t.Errorf("Unexpected status: %d", status) | ||
} | ||
if err != nil { | ||
return | ||
} | ||
if d := cmp.Diff(got, tt.want); d != "" { | ||
t.Errorf("Unexpected result:\n%s", d) | ||
} | ||
leaves := readRevisions(t, db.underlying()) | ||
for i, r := range tt.wantRevs { | ||
// allow tests to omit RevID | ||
if r.RevID == "" { | ||
leaves[i].RevID = "" | ||
} | ||
if r.ParentRevID == nil { | ||
leaves[i].ParentRevID = nil | ||
} | ||
} | ||
if d := cmp.Diff(tt.wantRevs, leaves); d != "" { | ||
t.Errorf("Unexpected leaves: %s", d) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.