prune method

Future<int> prune({
  1. DateTime? now,
})

Removes history entries older than DVHistory.keep (DV-HISTORY-004), returning how many. Never touches the records themselves.

Implementation

Future<int> prune({DateTime? now}) async {
  final Duration? keep = historyPolicy?.keep;
  if (keep == null) return 0;
  final String cutoff = _stamp((now ?? DateTime.now()).subtract(keep));
  final int removed = await database.execute(
    'DELETE FROM $historyTable WHERE occurred_at < ?',
    <Object?>[cutoff],
  );
  if (removed > 0) {
    // A change log that shrinks is evidence leaving the system, so it
    // leaves a line saying how much and under which declaration.
    DVObservability.log(
      'Removed $removed history entries from $table older than the '
      'declared retention of ${keep.inDays} days.',
      level: DVLogLevel.info,
      code: 'DV-HISTORY-004',
      context: <String, Object?>{
        'table': table,
        'removed': removed,
        'keepDays': keep.inDays,
        'before': cutoff,
      },
    );
  }
  return removed;
}