Expand description
The canonical pimdir SQL, inlined verbatim from the spec so the crate
is self-contained. Kept in sync with pimdir/migrations/ and
pimdir/queries/, where the source of truth is.
A store keeps one shared item per logical thing (its flags, body and summary) and one binding per source that syncs it (that source’s last agreed base). A single-source store is the degenerate case of one binding per item; a two-source store keeps two.
Constants§
- ADJUST_
REFCOUNT - Adjusts one object’s refcount by a signed delta; the hash’s primary key makes this an indexed point update.
- ALL
- Every statement in this module, paired with its constant name.
- BACKFILL_
SHARED_ OBJECT - Gives every binding written before
shared_objectexisted the item’s own body as its agreement point, once the column has been added (spec §6, thedraftallowance). - BUMP_
ATTEMPTS - Records a failed apply attempt without parking: the retry path, the
reference
park_actionwith aNULLerror. - BUMP_
GENERATION - The owner’s handle-space reset marker (spec §12): run in the same transaction as the rebuild it records.
- BUMP_
NEXT_ SEQ - Hands out, and advances, the store-global next public id via
RETURNING. The counter only ever increases, so aseqis never reused. Run only when the message has no id yet. - CANCEL_
ACTION - One queue row removed by request rather than by application, pending
or parked (spec §15.5): a queued item withdrawn, or a performed intent
acknowledged by the process that carried it out. The same delete as
DELETE_ACTION, named apart because the trigger is a request. It releases the row’sobject_hashpin, so it runs in one transaction with the refcount settle. - CLAIM_
ACTION - Deletes the row an owner is about to apply, and reports whether it was still there.
- COUNT_
ITEMS - Counts a collection’s live items (tombstones excluded).
- COUNT_
RETAINED - Counts a collection’s retained items, the counterpart of
COUNT_ITEMS; rides theitems_retainedpartial index. - COUNT_
RETAINED_ BEFORE - What a purge with this cutoff would retire, and what its bodies weigh:
the preview a confirmation prints,
PURGE_RETAINED_BEFOREbeing the act. - DANGLING_
BINDINGS - The bindings whose item is gone: the one dangling row a repair can clear,
since nothing can read it (
DELETE_DANGLING_BINDINGS). - DANGLING_
ITEM_ OBJECTS - The items whose body is not indexed. Reported, never repaired: the item is still the item.
- DANGLING_
QUEUE_ OBJECTS - The queue rows whose body is not indexed. Reported, never repaired: the row is still an intent somebody expressed.
- DELETE_
BINDING - Deletes one source’s binding of an item.
- DELETE_
DANGLING_ BINDINGS - Deletes the bindings whose item is gone, the one dangling row a repair can clear without guessing: a binding with no item is unreachable, where an item with no object row still holds the item.
- DELETE_
GARBAGE_ OBJECTS - Drops the unreferenced object rows inside the collector’s transaction; their blobs are unlinked after the commit, so a crash leaves at worst an orphan blob.
- DELETE_
ITEM_ BINDINGS - Deletes every binding of one item, for the retire path: the row survives, but no source holds it, so no base does either. A retained row carrying no binding is the persisted form of “the removal has finished propagating” (spec §11).
- ENQUEUE_
ACTION - A producer’s append. Runs after
ENSURE_COLLECTION, in one transaction with theSTORE_OBJECTupsert when the payload references a body (spec §15.1). - ENSURE_
COLLECTION - Creates a collection row if it does not exist yet, leaving an existing one
untouched (the kind is declared separately by
SET_COLLECTION_KIND). - ENSURE_
INDEXES - Every index the schema grew after version 1 was first published, as one idempotent batch: a store written by an earlier draft has the tables but not these, and an index is not something a reader can do without.
- GET_
ITEM - Fetches one live item by its public id (
seq), the client-facing key. - HANDLE_
FOR_ LINK - One source’s handle for an item, which its binding’s primary key answers directly: the lookup a queued action needs to name the placement it edits.
- INSERT_
BINDING - Inserts one item’s binding for one source (the new-binding path;
UPDATE_BINDINGhandles an existing one). - INSERT_
ITEM - Inserts one item row (the new-placement path;
UPDATE_ITEMhandles an existing one). - ITEM_
BINDINGS - Every source’s binding of one item: the handle it is bound to, the base
the last sync agreed on, and the conflict it is stuck on. The read behind
item show, which names one item and can afford to say everything about it. - LINK_
FOR_ HANDLE - The link id one source’s handle is bound to, for a batch that drops a placement: a drop names a handle, and the hub is keyed by link id.
- LIST_
ACCOUNTS - The accounts owning at least one collection. A store knows an account only through its collections (spec §9.2), so this is not a configured roster.
- LIST_
COLLECTIONS - Lists every collection with its display metadata and generation, ordered by
sort_orderthen id, the ones carrying no sort order coming last. - LIST_
COLLECTIONS_ BY_ ACCOUNT - One account’s collections, the filter axis of a merged view.
ISso bindingNULLselects the collections of a single-account store. - LIST_
CONFLICTED_ BINDINGS - The bindings waiting for a decision, across an account’s collections: what each one is, and the three bodies a resolver merges.
- LIST_
GARBAGE_ OBJECTS - Lists the objects nothing references any more: what the collector takes, and never a write’s business, since the batch that attaches a body may not be the one that indexed it (spec §5).
- LIST_
ITEMS_ PAGE - A keyset page of a collection’s live items in link-id order.
:afteris the exclusive lower bound onlink_id, the empty string starting from the beginning since alink_idis never empty; rides theitemsprimary key, with no extra index. - LIST_
ITEMS_ PAGE_ ASC - A keyset page of a collection’s live items in the kind’s own ascending order (spec §9.3): A to Z for contacts, earliest first for mail and calendars.
- LIST_
ITEMS_ PAGE_ DESC - The same page descending: newest first for mail and calendars, Z to A for contacts.
- LIST_
LINK_ PLACEMENTS - Every live placement of one identity, with the collection and account it sits in (spec §9.2). The store reports where a link id occurs and takes no position on whether the placements are one thing: a mail view lists them, a contact view may offer to merge them, off these rows.
- LIST_
OBJECT_ HASHES - Every hash the index holds. For the diagnosis that has to visit every
row anyway, never for the collector, which asks about the file in
front of it with
OBJECT_EXISTSrather than holding the whole index in memory. - LIST_
OBJECT_ PLACEMENTS - The same on the dedup axis, by body rather than identity, so it pairs placements two servers gave different link ids.
- LIST_
QUEUED_ COLLECTIONS - The collections with pending work, for the owner’s drain loop.
- LIST_
RETAINED_ PAGE - A keyset page of a collection’s retained items, joined to the body size the
row still pins (
NULLwhen unhydrated): the trash listing besideLIST_ITEMS_PAGE, and the only read that returns them. - LIST_
SOURCES - The distinct source names the store has synced, across all collections, so a client discovers which source to attribute writes to.
- LIVE_
BYTES - The bytes held by objects at least one live item binds. An object a live and a retained item share counts here, since purging the retained one frees nothing.
- LIVE_
ITEM_ FOR_ LINK - Whether a collection holds a live item under a link id: the collision
check a queued
addruns before staging. - LOAD_
ACCOUNT - Reads a collection’s owning account.
- LOAD_
ACTION_ ROW - One queue row’s spent attempts and pinned body, for a caller acting on a row by id: cancelling it, acknowledging an intent it performed out of band, or recording a failure.
- LOAD_
BINDINGS - Loads every per-source binding of a collection: the stored base (handle, flags, object, revision) each sync merges against.
- LOAD_
BINDINGS_ BY_ LINK - The same rows, narrowed to the link ids one write batch touches: the binding
half of
LOAD_ITEMS_BY_LINK. - LOAD_
CHECKPOINT - Reads one source’s sync checkpoint for a collection.
- LOAD_
CONFLICT - Reads a collection’s conflict policy.
- LOAD_
GENERATION - A collection’s handle-space epoch, so a reader derives epoch-dependent protocol values (an IMAP UIDVALIDITY) from the store alone.
- LOAD_
ITEMS - Loads a whole collection for the sync seam: every item, tombstones included, unpaginated and unordered.
- LOAD_
ITEMS_ BY_ LINK - The same rows, narrowed to the link ids one write batch touches (spec §14).
- LOAD_
KIND - Reads a collection’s declared kind.
- LOAD_
PARKED_ ACTIONS - The parked actions, for status surfaces and operator repair.
- LOAD_
PENDING_ ACTIONS - The owner’s drain: a collection’s pending (non-parked) actions, in append order. A reader runs the same statement to overlay pending actions on its item projection (read-your-writes, spec §15.4).
- LOOKUP_
OBJECTS - Resolves the object hash currently bound to each of the given link ids (passed as a JSON array), skipping the ones carrying no body.
- MIGRATION_
0001 - Schema version 1 (
migrations/0001_init.sql), the whole draft schema including the action queue and collection generations. Applied to a fresh database; the caller setsPRAGMA user_version = 1on success. - MINTED_
KEYS - How many minted keys (spec §9,
dup:<hint>#<handle>) each collection holds: the second copy of an identity a source hands over twice, filed as an item of its own. - OBJECT_
EXISTS - Whether the index still holds a body: the collector’s question about the one file in front of it, asked on the primary key (spec §5).
- OBJECT_
SIZE - One object’s stored size.
- OBJECT_
STATS - How many objects are indexed and what they weigh.
- PARK_
ACTION - A permanently failing action: recorded and skipped, visible to operators instead of blocking the collection’s queue.
- PURGE_
ITEM - Purges one retained item by its public id: the only true delete. Its
bindings cascade, and the body it released is unlinked by the
collector once nothing else references it. Guarded on
retained_at, so a purge can never take a live item. - PURGE_
RETAINED_ BEFORE - The time-based sweep: every item retired strictly before
:cutoff(RFC 3339), so one retained exactly at that instant is kept. Store-wide, since how long to keep is the owner’s policy. The cutoff is the caller’s parameter, not the store’s clock, so the boundary is deterministic even though the stamps are SQLite’s. - RECOMPUTE_
REFCOUNTS - Recomputes every object’s refcount from the five columns that pin one (spec §7): an item’s body, an item’s conflict copy, a source’s stored base, a binding’s diverging remote body and a pending queue action’s body.
- REFCOUNT_
DRIFT - The objects whose stored refcount disagrees with the five pointer columns
that justify it: the read
RECOMPUTE_REFCOUNTSsettles. - RELEASE_
PINS - Releases one reference from each of the given hashes (a JSON array), the
set-based form of
ADJUST_REFCOUNTat-1. - RENAME_
COLLECTION - Gives a collection a new id, carrying its whole contents with it: every
foreign key onto
collections(id)isON UPDATE CASCADE, so the items, bindings, sources, queue rows and child collections follow in the same statement (spec §14). - RESHAPED_
INDEXES - Indexes an earlier draft created under the same name over different
columns, as
(name, the columns it must hold now). - RETAINED_
BYTES - The store-wide size of the bodies retention is holding, each distinct object counted once. An upper bound on what a purge reclaims: an object a live item also points at keeps a reference and survives.
- RETAINED_
ITEM - The retained row holding a link id, if any: its public id and the objects it pins, which revive releases and purge reclaims.
- RETAIN_
ITEM - Retires one item: it stands exactly where a hard-deleting store would
have issued its delete. The row keeps its
object_hash, so the body keeps its reference and its blob survives the sweep. SQLite stamps the instant itself, so no clock is plumbed through the crate; a purge’s cutoff is by contrast the caller’s parameter. - REVIVE_
ITEM - Revives a retained row: the link id is back, from a source-side
resurrection or a client
add, so it stops being retained instead of conflicting on the primary key. The caller adopts the new content withUPDATE_ITEMin the same transaction, and the row keeps itsseq. - SEQ_
BY_ LINK - Resolves an item’s public id (
seq) from its internallink_id, the inverse ofGET_ITEM, for a consumer that just staged an add. - SEQ_
FOR_ LINK_ ANY - The message’s existing public id, if any placement of this
link_idalready has one (in any collection), so all placements of a message share one id. - SET_
COLLECTION_ ACCOUNT - Regroups a collection under another account, or out of one with
NULL. Safe at any time: the account partitions no identifier (spec §9.2), so the move leaves seqs, link ids and objects alone. - SET_
COLLECTION_ KIND - Declares (or re-declares) a collection’s kind, creating the row if the collection is not known yet. Updates the kind alone, so a collection never changes account as a side effect of a sync declaring its media type.
- SET_
CONFLICT - Stores a collection’s conflict policy.
- SET_
SORT_ KEY - Restates one item’s ordering key, for a re-projection over items already stored: a store written before its kind had a convention, one whose convention changed, or a consumer whose sync engine does not carry the key inline (spec §9.3). Not the ordinary write path.
- STORE_
OBJECT - Indexes an object by its content hash at refcount 0; re-storing a known
hash only refreshes its size, since the count belongs to
ADJUST_REFCOUNT. - UPDATE_
BINDING - Updates one existing binding’s columns in place (its primary key
(collection, link_id, source)is unchanged). - UPDATE_
ITEM - Updates one existing item’s columns in place (the diffed-save path; the
primary key
(collection, link_id)is unchanged). - UPSERT_
CHECKPOINT - Writes one source’s sync checkpoint for a collection, replacing the previous one.
- VERSION
- The current schema version.