add method CleanArchive to delete archived orders (#2796)
This commit is contained in:
parent
189b268892
commit
497f10d7b1
@ -126,6 +126,15 @@ func TestDB(t *testing.T) {
|
||||
},
|
||||
}, archived, cmp.Comparer(pb.Equal)))
|
||||
|
||||
// with 1 hour ttl, archived order should not be deleted
|
||||
n, err := db.Orders().CleanArchive(ctx, time.Hour)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 0, n)
|
||||
|
||||
// with 1 nanosecond ttl, archived order should be deleted
|
||||
n, err = db.Orders().CleanArchive(ctx, time.Nanosecond)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, n)
|
||||
})
|
||||
}
|
||||
|
||||
@ -172,5 +181,10 @@ func TestDB_Trivial(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Len(t, infos, 1)
|
||||
}
|
||||
{ // Ensure CleanArchive works at all
|
||||
n, err := db.Orders().CleanArchive(ctx, time.Nanosecond)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, n)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -72,6 +72,8 @@ type DB interface {
|
||||
Archive(ctx context.Context, requests ...ArchiveRequest) error
|
||||
// ListArchived returns orders that have been sent.
|
||||
ListArchived(ctx context.Context, limit int) ([]*ArchivedInfo, error)
|
||||
// CleanArchive deletes all entries older than ttl
|
||||
CleanArchive(ctx context.Context, ttl time.Duration) (int, error)
|
||||
}
|
||||
|
||||
// SenderConfig defines configuration for sending orders.
|
||||
|
@ -259,3 +259,25 @@ func (db *ordersdb) ListArchived(ctx context.Context, limit int) (_ []*orders.Ar
|
||||
|
||||
return infos, ErrInfo.Wrap(rows.Err())
|
||||
}
|
||||
|
||||
// CleanArchive deletes all entries older than ttl
|
||||
func (db *ordersdb) CleanArchive(ctx context.Context, ttl time.Duration) (_ int, err error) {
|
||||
defer mon.Task()(&ctx)(&err)
|
||||
|
||||
deleteBefore := time.Now().UTC().Add(-1 * ttl)
|
||||
result, err := db.db.Exec(`
|
||||
DELETE FROM order_archive_
|
||||
WHERE archived_at <= ?
|
||||
`, deleteBefore)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return 0, nil
|
||||
}
|
||||
return 0, ErrInfo.Wrap(err)
|
||||
}
|
||||
count, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return 0, ErrInfo.Wrap(err)
|
||||
}
|
||||
return int(count), nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user