fix(docsync): act on a deletion once, and know what to do when it is undone

Both planner branches planned local_deleted for as long as a file sat in
TREK's trash. Under unlink nothing showed, until the binding was switched
to trash: the next run then binned every document ever deleted in TREK,
months back included. A binding on trash from the start asked the provider
to bin the same copy on every run. A local_deleted row is now left alone,
so the policy is applied once, under the policy in force at that moment.

That guard also meant a file taken back out of TREK's trash stayed on
local_deleted for good, which it already did before: no transfer, no
upload, no flag, and a second deletion of the same file would never have
propagated. The row now leaves that state by what became of the provider
copy:

- still there: the pairing counts again and the row is judged like any
  synced one, so an upstream edit made meanwhile comes down;
- binned by TREK: the pairing is dropped and the file goes up again as a
  new document through the ordinary push, keeping its trek_doc_uid;
- deleted by somebody else: flagged remote_missing, never uploaded over
  their deletion.

Telling the last two apart needs a record of what TREK did, so the new
remote_trashed_at column (migration 239) is written when TREK bins a copy
and cleared when the pairing is back, or when the binned copy is listed
again because somebody restored it at the provider.

A file purged from the trash came straight back as well: file_id is ON
DELETE SET NULL, and the row read as a download that never landed. A
tombstone now stays a tombstone, and a purge no run had seen yet is
treated as the deletion it is.

Also drops snapshotHash, which nothing read.
This commit is contained in:
Maurice
2026-09-19 12:04:43 +02:00
parent dc1ac0ae85
commit dd3d97589b
5 changed files with 581 additions and 37 deletions
+21
View File
@@ -5125,6 +5125,27 @@ function runMigrations(db: Database.Database): void {
CREATE INDEX IF NOT EXISTS idx_document_sync_items_due ON document_sync_items(link_id, next_attempt_at);
`);
},
/*
* Document providers: when TREK itself put a provider copy in the bin.
*
* A file deleted in TREK under the `trash` policy takes its provider copy
* with it. Taken back out of TREK's trash, it has to go up again; a copy
* somebody else deleted in the meantime must not. Both leave the same gap
* in a listing, so the difference is written down when TREK acts rather
* than guessed at later.
*
* No backfill from the binding's current policy: that policy may not be
* the one the deletion ran under, and reading it back is the retroactive
* mistake this column exists to avoid. A row left NULL is treated like a
* copy somebody else deleted, which flags it instead of uploading it.
*/
() => {
const hasColumn = db
.prepare("SELECT 1 FROM pragma_table_info('document_sync_items') WHERE name = 'remote_trashed_at'")
.get();
if (!hasColumn) db.exec('ALTER TABLE document_sync_items ADD COLUMN remote_trashed_at TEXT');
},
];
if (currentVersion < migrations.length) {