-- Read-your-writes pending-ops tracking (PM-47).
--
-- Eager local writes/deletes — both PM-28 mediated writes and the PM-47
-- passthrough proxy — mark a record "pending" until the Hydrant firehose
-- re-delivers the same event, at which point the PDS/AppView is authoritative
-- again. `repo.getRecord` of a `space.polymodel.*` record consults these tables
-- to reflect a not-yet-confirmed local op:
--   * a pending write whose cid differs from the PDS returns the local value;
--   * a pending delete returns RecordNotFound even if a lagging source still
--     serves the record.
--
-- These tables are collection-agnostic (keyed by record URI); they do not touch
-- the per-collection projection tables. The matching row is cleared by the
-- firehose record-event projection path once the Hydrant `#commit` redelivers
-- the event.

CREATE TABLE pending_writes (
    -- Canonical record URI: at://{did}/{collection}/{rkey}.
    uri TEXT PRIMARY KEY,
    cid TEXT NOT NULL,
    -- The raw record JSON, so repo.getRecord can rebuild `value` without a
    -- collection-specific lookup.
    value TEXT NOT NULL,
    written_at INTEGER NOT NULL
);

CREATE TABLE pending_deletes (
    uri TEXT PRIMARY KEY,
    deleted_at INTEGER NOT NULL
);
