Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2e40786bcd | ||
|
|
38bcf01ae3 |
@@ -91,25 +91,6 @@ class BankController extends ChangeNotifier {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Drop tombstones that match any of [txs] (fingerprint-aware).
|
|
||||||
void clearRemoteDeletesMatching(Iterable<BankTransaction> txs) {
|
|
||||||
final list = txs.toList(growable: false);
|
|
||||||
if (list.isEmpty) return;
|
|
||||||
final next = [
|
|
||||||
for (final tomb in _pendingRemoteDeletes)
|
|
||||||
if (!list.any(tomb.matchesForDelete)) tomb,
|
|
||||||
];
|
|
||||||
if (next.length == _pendingRemoteDeletes.length) return;
|
|
||||||
_pendingRemoteDeletes = next;
|
|
||||||
unawaited(_persistPendingDeletes());
|
|
||||||
}
|
|
||||||
|
|
||||||
void clearAllPendingRemoteDeletes() {
|
|
||||||
if (_pendingRemoteDeletes.isEmpty) return;
|
|
||||||
_pendingRemoteDeletes = const [];
|
|
||||||
unawaited(_persistPendingDeletes());
|
|
||||||
}
|
|
||||||
|
|
||||||
void requeueRemoteDeletes(Iterable<BankTransaction> txs) {
|
void requeueRemoteDeletes(Iterable<BankTransaction> txs) {
|
||||||
for (final tx in txs) {
|
for (final tx in txs) {
|
||||||
_queueRemoteDelete(tx, notify: false);
|
_queueRemoteDelete(tx, notify: false);
|
||||||
@@ -120,11 +101,54 @@ class BankController extends ChangeNotifier {
|
|||||||
void _queueRemoteDelete(BankTransaction tx, {bool notify = true}) {
|
void _queueRemoteDelete(BankTransaction tx, {bool notify = true}) {
|
||||||
if (tx.id.isEmpty) return;
|
if (tx.id.isEmpty) return;
|
||||||
if (_isTombstoned(tx)) return;
|
if (_isTombstoned(tx)) return;
|
||||||
_pendingRemoteDeletes = [..._pendingRemoteDeletes, tx];
|
_pendingRemoteDeletes = [
|
||||||
|
..._pendingRemoteDeletes,
|
||||||
|
tx.copyWith(isManual: true),
|
||||||
|
];
|
||||||
unawaited(_persistPendingDeletes());
|
unawaited(_persistPendingDeletes());
|
||||||
if (notify) onPendingRemoteDeletesChanged?.call();
|
if (notify) onPendingRemoteDeletesChanged?.call();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Merge delete tombstones from another device and strip matching local rows.
|
||||||
|
Future<void> applyRemoteDeletes(
|
||||||
|
List<BankTransaction> remote, {
|
||||||
|
bool notify = true,
|
||||||
|
}) async {
|
||||||
|
var tombsChanged = false;
|
||||||
|
for (final tomb in remote) {
|
||||||
|
if (_isTombstoned(tomb)) continue;
|
||||||
|
_pendingRemoteDeletes = [
|
||||||
|
..._pendingRemoteDeletes,
|
||||||
|
tomb.copyWith(isManual: true),
|
||||||
|
];
|
||||||
|
tombsChanged = true;
|
||||||
|
}
|
||||||
|
if (_pendingRemoteDeletes.isEmpty) return;
|
||||||
|
|
||||||
|
final beforeRemote = _remoteTransactions.length;
|
||||||
|
final beforeImported = _importedTransactions.length;
|
||||||
|
_remoteTransactions = [
|
||||||
|
for (final tx in _remoteTransactions)
|
||||||
|
if (!_isTombstoned(tx)) tx,
|
||||||
|
];
|
||||||
|
_importedTransactions = [
|
||||||
|
for (final tx in _importedTransactions)
|
||||||
|
if (!_isTombstoned(tx)) tx,
|
||||||
|
];
|
||||||
|
final stripped = beforeRemote != _remoteTransactions.length ||
|
||||||
|
beforeImported != _importedTransactions.length;
|
||||||
|
|
||||||
|
if (tombsChanged || stripped) {
|
||||||
|
_invalidateMerged();
|
||||||
|
if (tombsChanged) await _persistPendingDeletes();
|
||||||
|
if (stripped) {
|
||||||
|
await _persistCache();
|
||||||
|
await _persistImported();
|
||||||
|
}
|
||||||
|
if (notify) notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _persistPendingDeletes() async {
|
Future<void> _persistPendingDeletes() async {
|
||||||
final storage = _storage;
|
final storage = _storage;
|
||||||
if (storage == null) return;
|
if (storage == null) return;
|
||||||
|
|||||||
@@ -274,23 +274,42 @@ class SyncController extends ChangeNotifier {
|
|||||||
_armPushDebounce();
|
_armPushDebounce();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes tombstoned bank txs from PocketBase before pull/push can resurrect them.
|
/// Pull shared delete tombstones, then remove matching local bank rows.
|
||||||
|
Future<void> _pullAndApplyTxDeletes(PbSyncService service) async {
|
||||||
|
try {
|
||||||
|
final remote = await service.pullTxDeletes(onStatus: _setStatus);
|
||||||
|
await _bank.applyRemoteDeletes(remote, notify: false);
|
||||||
|
} catch (error) {
|
||||||
|
debugPrint('SyncController: tx_deletes pull skipped ($error)');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Removes tombstoned bank txs from the transactions collection.
|
||||||
|
///
|
||||||
|
/// Tombstones themselves stay local + synced via `tx_deletes` so other
|
||||||
|
/// devices learn about the delete and do not push the row back.
|
||||||
Future<void> _flushPendingTxDeletes(PbSyncService service) async {
|
Future<void> _flushPendingTxDeletes(PbSyncService service) async {
|
||||||
final pending = List<BankTransaction>.of(_bank.pendingRemoteDeletes);
|
final pending = List<BankTransaction>.of(_bank.pendingRemoteDeletes);
|
||||||
if (pending.isEmpty) return;
|
if (pending.isEmpty) return;
|
||||||
_setStatus('Removing deleted bank txs…');
|
_setStatus('Removing deleted bank txs…');
|
||||||
try {
|
try {
|
||||||
final matched = await service.deleteTransactionsMatching(pending);
|
await service.deleteTransactionsMatching(pending);
|
||||||
if (matched.isNotEmpty) {
|
// Keep tombstones — they must remain until every device has seen them.
|
||||||
_bank.clearRemoteDeletesMatching(matched);
|
|
||||||
}
|
|
||||||
// Do NOT clear unmatched tombstones — they still block pull resurrection
|
|
||||||
// until pushTransactions GC removes the remote rows.
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
debugPrint('SyncController: tx deletes failed ($error)');
|
debugPrint('SyncController: tx deletes failed ($error)');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _pushTxDeletes(PbSyncService service) async {
|
||||||
|
final tombs = List<BankTransaction>.of(_bank.pendingRemoteDeletes);
|
||||||
|
if (tombs.isEmpty) return;
|
||||||
|
try {
|
||||||
|
await service.pushTxDeletes(tombs, onStatus: _setStatus);
|
||||||
|
} catch (error) {
|
||||||
|
debugPrint('SyncController: tx_deletes push skipped ($error)');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void _armPushDebounce() {
|
void _armPushDebounce() {
|
||||||
if (!_auth.isLoggedIn) return;
|
if (!_auth.isLoggedIn) return;
|
||||||
if (_syncing) {
|
if (_syncing) {
|
||||||
@@ -323,7 +342,8 @@ class SyncController extends ChangeNotifier {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
final service = PbSyncService(_auth.client);
|
final service = PbSyncService(_auth.client);
|
||||||
// Delete first — otherwise pull re-merges the row and push recreates it.
|
// Learn deletes from other devices before merging bank txs / pushing.
|
||||||
|
await _pullAndApplyTxDeletes(service);
|
||||||
await _flushPendingTxDeletes(service);
|
await _flushPendingTxDeletes(service);
|
||||||
final remoteMeta = await service.pull(
|
final remoteMeta = await service.pull(
|
||||||
onProgress: (p) {
|
onProgress: (p) {
|
||||||
@@ -438,6 +458,7 @@ class SyncController extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
// Retry deletes in case any were queued during the pull/merge window.
|
// Retry deletes in case any were queued during the pull/merge window.
|
||||||
await _flushPendingTxDeletes(service);
|
await _flushPendingTxDeletes(service);
|
||||||
|
await _pushTxDeletes(service);
|
||||||
_pushBankPending = false;
|
_pushBankPending = false;
|
||||||
final tombs = List<BankTransaction>.of(_bank.pendingRemoteDeletes);
|
final tombs = List<BankTransaction>.of(_bank.pendingRemoteDeletes);
|
||||||
final bankSummary = await service.pushTransactions(
|
final bankSummary = await service.pushTransactions(
|
||||||
@@ -448,8 +469,6 @@ class SyncController extends ChangeNotifier {
|
|||||||
onProgress: _setProgress,
|
onProgress: _setProgress,
|
||||||
onStatus: _setStatus,
|
onStatus: _setStatus,
|
||||||
);
|
);
|
||||||
// Push GC removes orphan admin test rows; tombstones are done.
|
|
||||||
_bank.clearAllPendingRemoteDeletes();
|
|
||||||
_lastSyncedAt = DateTime.now();
|
_lastSyncedAt = DateTime.now();
|
||||||
_lastError = null;
|
_lastError = null;
|
||||||
_lastSyncSummary = '$metaSummary\n$bankSummary';
|
_lastSyncSummary = '$metaSummary\n$bankSummary';
|
||||||
@@ -601,7 +620,10 @@ class SyncController extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
if (pushBank) {
|
if (pushBank) {
|
||||||
_setStatus('Updating bank txs…');
|
_setStatus('Updating bank txs…');
|
||||||
|
// Other devices may have deleted test txs — learn that before push.
|
||||||
|
await _pullAndApplyTxDeletes(service);
|
||||||
await _flushPendingTxDeletes(service);
|
await _flushPendingTxDeletes(service);
|
||||||
|
await _pushTxDeletes(service);
|
||||||
try {
|
try {
|
||||||
final tombs = List<BankTransaction>.of(_bank.pendingRemoteDeletes);
|
final tombs = List<BankTransaction>.of(_bank.pendingRemoteDeletes);
|
||||||
final summary = await service.pushTransactions(
|
final summary = await service.pushTransactions(
|
||||||
@@ -612,7 +634,6 @@ class SyncController extends ChangeNotifier {
|
|||||||
onProgress: _setProgress,
|
onProgress: _setProgress,
|
||||||
onStatus: _setStatus,
|
onStatus: _setStatus,
|
||||||
);
|
);
|
||||||
_bank.clearAllPendingRemoteDeletes();
|
|
||||||
parts.add(summary);
|
parts.add(summary);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
debugPrint('SyncController: bank tx push skipped ($error)');
|
debugPrint('SyncController: bank tx push skipped ($error)');
|
||||||
|
|||||||
@@ -804,6 +804,70 @@ class PbSyncService {
|
|||||||
return matchedTombs;
|
return matchedTombs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Pull synced delete tombstones for admin test transactions.
|
||||||
|
Future<List<BankTransaction>> pullTxDeletes({
|
||||||
|
void Function(String message)? onStatus,
|
||||||
|
}) async {
|
||||||
|
final userId = _userId;
|
||||||
|
final filter = 'user="$userId"';
|
||||||
|
onStatus?.call('Pulling deleted test txs…');
|
||||||
|
final records = await _fullList('tx_deletes', filter);
|
||||||
|
final out = <BankTransaction>[];
|
||||||
|
for (final r in records) {
|
||||||
|
final tx = _bankTxFromRecord(r);
|
||||||
|
if (tx == null) continue;
|
||||||
|
out.add(tx.copyWith(isManual: true));
|
||||||
|
}
|
||||||
|
debugPrint('PbSyncService pull: tx_deletes=${out.length}');
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Upsert local delete tombstones. Never removes remote-only rows (union).
|
||||||
|
Future<String> pushTxDeletes(
|
||||||
|
List<BankTransaction> tombs, {
|
||||||
|
void Function(String message)? onStatus,
|
||||||
|
}) async {
|
||||||
|
if (tombs.isEmpty) return 'Tx deletes: nothing to push';
|
||||||
|
final userId = _userId;
|
||||||
|
final filter = 'user="$userId"';
|
||||||
|
onStatus?.call('Syncing deleted test txs…');
|
||||||
|
final existing = {
|
||||||
|
for (final r in await _fullList('tx_deletes', filter))
|
||||||
|
r.getStringValue('tx_id'): r,
|
||||||
|
};
|
||||||
|
|
||||||
|
final ops = <_BatchOp>[];
|
||||||
|
var skipped = 0;
|
||||||
|
final seen = <String>{};
|
||||||
|
for (final tomb in tombs) {
|
||||||
|
final syncId = _clip(tomb.id, 2000);
|
||||||
|
if (syncId.isEmpty || !seen.add(syncId)) continue;
|
||||||
|
final body = <String, dynamic>{
|
||||||
|
'user': userId,
|
||||||
|
'tx_id': syncId,
|
||||||
|
'date': _clip(tomb.date, 32),
|
||||||
|
'description': _clip(
|
||||||
|
tomb.description.isEmpty ? 'Unknown' : tomb.description,
|
||||||
|
4000,
|
||||||
|
),
|
||||||
|
'amount': tomb.amount,
|
||||||
|
'currency': _clip(
|
||||||
|
tomb.currency.isEmpty ? 'EUR' : tomb.currency.toUpperCase(),
|
||||||
|
8,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
final remote = existing[syncId];
|
||||||
|
if (remote == null) {
|
||||||
|
ops.add(_BatchOp.create(body));
|
||||||
|
} else {
|
||||||
|
skipped++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await _runWriteOps('tx_deletes', ops, onStatus: onStatus);
|
||||||
|
return 'Tx deletes: local ${tombs.length} (+${ops.length} · $skipped exist)';
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _runOne(String collection, _BatchOp op) {
|
Future<void> _runOne(String collection, _BatchOp op) {
|
||||||
final col = _pb.collection(collection);
|
final col = _pb.collection(collection);
|
||||||
final Future<dynamic> future;
|
final Future<dynamic> future;
|
||||||
@@ -968,6 +1032,12 @@ class PbSyncService {
|
|||||||
filter =
|
filter =
|
||||||
'user="$userId" && tx_id="${key.replaceAll('"', '\\"')}"';
|
'user="$userId" && tx_id="${key.replaceAll('"', '\\"')}"';
|
||||||
}
|
}
|
||||||
|
case 'tx_deletes':
|
||||||
|
final key = body['tx_id'];
|
||||||
|
if (userId != null && key is String) {
|
||||||
|
filter =
|
||||||
|
'user="$userId" && tx_id="${key.replaceAll('"', '\\"')}"';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (filter == null) {
|
if (filter == null) {
|
||||||
throw StateError('Cannot upsert $collection without filter');
|
throw StateError('Cannot upsert $collection without filter');
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
/// PocketBase migration: `tx_deletes` — synced tombstones for deleted admin
|
||||||
|
/// test transactions so another device cannot push them back into `transactions`.
|
||||||
|
///
|
||||||
|
/// Apply on the server:
|
||||||
|
///
|
||||||
|
/// sudo cp this_file /opt/pocketbase/pb_migrations/
|
||||||
|
/// sudo chown pocketbase:pocketbase /opt/pocketbase/pb_migrations/*.js
|
||||||
|
/// sudo -u pocketbase /opt/pocketbase/pocketbase migrate up
|
||||||
|
/// sudo systemctl restart pocketbase
|
||||||
|
migrate((app) => {
|
||||||
|
const users = app.findCollectionByNameOrId("usersapp");
|
||||||
|
|
||||||
|
try {
|
||||||
|
app.findCollectionByNameOrId("tx_deletes");
|
||||||
|
return;
|
||||||
|
} catch (_) {}
|
||||||
|
|
||||||
|
const ownerRules = {
|
||||||
|
listRule: "user = @request.auth.id",
|
||||||
|
viewRule: "user = @request.auth.id",
|
||||||
|
createRule: "@request.auth.id != '' && user = @request.auth.id",
|
||||||
|
updateRule: "user = @request.auth.id",
|
||||||
|
deleteRule: "user = @request.auth.id",
|
||||||
|
};
|
||||||
|
|
||||||
|
const txDeletes = new Collection({
|
||||||
|
type: "base",
|
||||||
|
name: "tx_deletes",
|
||||||
|
...ownerRules,
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
type: "relation",
|
||||||
|
name: "user",
|
||||||
|
required: true,
|
||||||
|
collectionId: users.id,
|
||||||
|
cascadeDelete: true,
|
||||||
|
maxSelect: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// Clipped BankTransaction.id
|
||||||
|
type: "text",
|
||||||
|
name: "tx_id",
|
||||||
|
required: true,
|
||||||
|
min: 1,
|
||||||
|
max: 2000,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
name: "date",
|
||||||
|
required: true,
|
||||||
|
min: 1,
|
||||||
|
max: 32,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
name: "description",
|
||||||
|
required: true,
|
||||||
|
min: 1,
|
||||||
|
max: 4000,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "number",
|
||||||
|
name: "amount",
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
name: "currency",
|
||||||
|
required: true,
|
||||||
|
min: 1,
|
||||||
|
max: 8,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
indexes: [
|
||||||
|
"CREATE UNIQUE INDEX idx_tx_deletes_user_tx ON tx_deletes (user, tx_id)",
|
||||||
|
],
|
||||||
|
});
|
||||||
|
app.save(txDeletes);
|
||||||
|
}, (app) => {
|
||||||
|
try {
|
||||||
|
app.delete(app.findCollectionByNameOrId("tx_deletes"));
|
||||||
|
} catch (_) {}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user