recordErasure method

Future<String> recordErasure(
  1. DVRecordTable table,
  2. Object key, {
  3. int? version,
})

Purges every value the log holds for one record and captures what a removal left of it, returning the change id: published at once, or staged and published after commit inside DV.transaction.

This is what a row removed or anonymized beside DVRecordTable -- by an erasure, a retention sweep or a replayed erasure -- owes the log. A record that is gone is captured as a delete one version past the newest the log or version, the version the caller removed, knows of; a destination applies changes by version, and a delete at a version it already holds is one it rightly ignores.

Implementation

Future<String> recordErasure(
  DVRecordTable table,
  Object key, {
  int? version,
}) async {
  await _ensureReady();
  final String recordKey = jsonEncode(_jsonSafe(key));
  final DVFilter thisRecord = DVFilter.all(<DVFilter>[
    DVFilter.equals('model', table.table),
    DVFilter.equals('record_key', recordKey),
  ]);
  final List<Map<String, Object?>> newest = await _records.find(
    logTable,
    where: thisRecord,
    orderBy: const <DVSort>[DVSort('record_version', descending: true)],
    limit: 1,
    fields: const <String>['record_version'],
  );
  await _records.update(
    logTable,
    <String, Object?>{'row_values': null, 'purged': 1},
    where: thisRecord,
  );
  final DVRecord? current = await table.read(key, withDeleted: true);
  final bool live = current != null && current.deletedAt == null;
  final int known = math.max(
    version ?? 0,
    newest.isEmpty ? 0 : _asInt(newest.first['record_version']),
  );
  return record(
    table: table,
    operation: live ? DVCaptureOp.update : DVCaptureOp.delete,
    key: key,
    version: current?.version ?? known + 1,
    values: live ? current.values : const <String, Object?>{},
    erased: true,
  );
}