Files
Chronos/src/Chronos.Data/Migrations/postgres/0012_found_copies.sql
T
Alby96andClaude Opus 5.5 fe752a0a21 fix(copies): address the owner review 6 findings
The review of the found-copies work showed pairs that could stick, jobs that
could act on a pair the user had answered otherwise, and detection gaps:

- forgetting a link on an address-keyed server deletes the pair and Candidate
  rows of that remote and resets the probe, so the sequence is found again
  when it is back; a Linked/Integrated row with no link is asked again
- 404s count only on id-keyed servers, at most once per recheck interval
- probes store the server's newest update time (copy_probes.server_newest,
  migration 0012) instead of comparing with this PC's clock; the lookup
  sample carries hashed photos by size and capture time too, for servers
  that have not hashed their copy
- the older-server gate no longer re-compares unchanged manifests
- a failed Integrate keeps its job id so a retry resumes only an untouched
  pair; cancel and stale plans release the pair (ReleaseAsync)
- automatic photo transfers never create or retarget a sequence
- a first upload files photos the server holds elsewhere into the new
  sequence
- Unlink re-compares before keeping apart; Trash here names only true last
  copies; Drop... trashes one version on both sides after re-checking the
  other
- a merge keeping the pair's other sequence swaps the dialog's columns and
  re-keys Different choices; Upload all reads every pair

Each fix has a regression test where it makes sense.

Refs #73

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 13:12:00 +01:00

61 lines
3.1 KiB
SQL

-- Found copies (owner review 6, #73): see sqlite/0012_found_copies.sql. Mirrors it.
CREATE TABLE found_copies (
id uuid PRIMARY KEY,
kind integer NOT NULL,
local_id uuid NOT NULL REFERENCES sequences (id) ON DELETE CASCADE,
server text COLLATE "C",
remote_id uuid,
other_id uuid REFERENCES sequences (id) ON DELETE CASCADE,
evidence integer NOT NULL,
state integer NOT NULL DEFAULT 0,
shared integer NOT NULL DEFAULT 0,
shared_unhashed integer NOT NULL DEFAULT 0,
shared_removed integer NOT NULL DEFAULT 0,
only_here integer NOT NULL DEFAULT 0,
only_there integer NOT NULL DEFAULT 0,
differs integer NOT NULL DEFAULT 0,
deleted_here integer NOT NULL DEFAULT 0,
deleted_there integer NOT NULL DEFAULT 0,
versions integer NOT NULL DEFAULT 0,
local_photos integer NOT NULL DEFAULT 0,
other_photos integer NOT NULL DEFAULT 0,
containment integer NOT NULL DEFAULT 0,
overlap integer NOT NULL DEFAULT 0,
hints integer NOT NULL DEFAULT 0,
local_frames_version integer NOT NULL DEFAULT 0,
other_frames_version integer NOT NULL DEFAULT 0,
fingerprint text NOT NULL,
answered_fingerprint text,
answered_shared integer,
auto_photos boolean NOT NULL DEFAULT false,
job_id uuid,
previous_state integer,
remote_gone_at timestamptz,
remote_gone_hits integer NOT NULL DEFAULT 0,
found_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL,
answered_at timestamptz,
CHECK ((kind = 0 AND server IS NOT NULL AND remote_id IS NOT NULL AND other_id IS NULL)
OR (kind = 1 AND server IS NULL AND remote_id IS NULL AND other_id IS NOT NULL AND other_id <> local_id))
);
CREATE UNIQUE INDEX ux_found_copies_remote ON found_copies (local_id, server, remote_id) WHERE kind = 0;
CREATE UNIQUE INDEX ux_found_copies_local ON found_copies (local_id, other_id) WHERE kind = 1;
CREATE INDEX ix_found_copies_server ON found_copies (server, remote_id);
CREATE INDEX ix_found_copies_other ON found_copies (other_id);
CREATE INDEX ix_found_copies_state ON found_copies (state);
CREATE TABLE copy_probes (
sequence_id uuid NOT NULL REFERENCES sequences (id) ON DELETE CASCADE,
scope text COLLATE "C" NOT NULL,
frames_version integer NOT NULL,
hashed integer NOT NULL,
probed_at timestamptz NOT NULL,
server_newest timestamptz,
PRIMARY KEY (sequence_id, scope)
);
INSERT INTO found_copies (id, kind, local_id, server, remote_id, evidence, state, fingerprint, found_at, updated_at)
SELECT gen_random_uuid(), 0, local_id, server, remote_id, 0, 2, 'link', now(), now()
FROM sync_links WHERE local_id IS NOT NULL;