Added database.change_batch_retention, deleting old change batches
Each save that records a batch also deletes, in the same transaction, the batches older than database.change_batch_retention (30 days by default; 0 or less keeps them for ever), with their records of opportunities. A client still holding a deleted batch gets 410 Gone from ?changed_after= and reloads the whole list, as for any unknown batch.
This commit is contained in:
1 parent
1988d07a29
commit
65e5bc6b26
10 files changed
+153
-19
No files matched your search
@@ -2,6 +2,7 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -53,3 +54,71 @@ func TestChangeBatchAfterClockWentBack(t *testing.T) {
|
||||
t.Errorf("changed after future: %v, %q", got, hash)
|
||||
}
|
||||
}
|
||||
|
||||
// Recording a batch deletes the batches older than the retention, with their
|
||||
// records of opportunities; with no retention, none are deleted.
|
||||
func TestChangeBatchRetention(t *testing.T) {
|
||||
for _, retention := range []time.Duration{time.Hour, 0} {
|
||||
db := openTestDB(t)
|
||||
s := &Store{
|
||||
db: db,
|
||||
changeBatchRetention: retention,
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := s.SaveOpportunity(
|
||||
ctx,
|
||||
&types.Opportunity{
|
||||
UUID: "ocds-1",
|
||||
LatestReleaseDate: time.Now(),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Backdate every batch so far by two hours.
|
||||
db.MustExec(
|
||||
`UPDATE opportunity_change_batches SET created_at = ?`,
|
||||
time.Now().UTC().Add(-2*time.Hour),
|
||||
)
|
||||
old, err := s.LatestChangeBatch(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = s.SetMarks(ctx, types.OpportunityMarks{
|
||||
UUID: "ocds-1",
|
||||
Skipped: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var batches, records int
|
||||
db.Get(&batches, `SELECT COUNT(*) FROM opportunity_change_batches`)
|
||||
db.Get(
|
||||
&records,
|
||||
`SELECT COUNT(*) FROM opportunity_change_batch_opportunities`,
|
||||
)
|
||||
_, _, err = s.ListChangedOpportunities(ctx, old)
|
||||
|
||||
if retention > 0 {
|
||||
if batches != 1 || records != 1 ||
|
||||
!errors.Is(err, ErrUnknownBatch) {
|
||||
t.Errorf(
|
||||
"retention %v: %d batches, %d records, err %v; "+
|
||||
"want the old batch gone",
|
||||
retention, batches, records, err,
|
||||
)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if batches != 2 || records != 2 || err != nil {
|
||||
t.Errorf(
|
||||
"no retention: %d batches, %d records, err %v; want both kept",
|
||||
batches, records, err,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user