event

Per-entity sync adapter for events.

Events are append-only and live at event/<yyyy-mm>/<event_id>.yaml. Monthly sharding (per ADR-0016 §4) keeps any single directory manageable. Two collaborators recording new events on the same day write different files, so events never produce merge conflicts.

Data source

Events live in the transaction_events table. ADR-0016 §8 says transactions and their before_data snapshots are local-only undo state that should NOT be synced; this adapter therefore only writes events that carry a semantic_type (the audit-log subset) and only the audit-log columns. The transaction_id and before_data columns stay local.

const INSERT_TRANSACTION_EVENT_SQL

source

const INSERT_TRANSACTION_EVENT_SQL: &str = "INSERT INTO transaction_events \
  (id, transaction_id, before_data, created_at, event_type, row_id, table_name, semantic_type, old_value, new_value) \
  VALUES (?1, ?2, NULL, ?3, ?4, ?5, ?6, ?7, ?8, ?9) \
  ON CONFLICT(id) DO NOTHING";

Insert SQL for importing a synced event into transaction_events.

const SELECT_TRANSACTION_EVENTS_SQL

source

const SELECT_TRANSACTION_EVENTS_SQL: &str = "SELECT \
  te.id, te.table_name, te.row_id, te.event_type, te.semantic_type, te.old_value, te.new_value, te.created_at \
  FROM transaction_events te \
  JOIN transactions t ON t.id = te.transaction_id \
  WHERE t.project_id = ?1 AND te.semantic_type IS NOT NULL \
  ORDER BY te.created_at, te.id";

Select SQL for the audit-log subset of transaction_events scoped to a project.

struct EventFile

source

struct EventFile

On-disk wrapper for .gest/event/<yyyy-mm>/<id>.yaml.

Field Type Description
deleted_at Option<DateTime<Utc>> Tombstone instant; when set, import deletes the event row instead of inserting it.
id Id Stable event identifier; also the file’s name stem.
table_name String The table of the row the event changed, such as tasks.
row_id String The id of the row the event changed.
event_type String The event_type column of transaction_events.
semantic_type String The audit-log kind of the event; only events that have one are synced.
old_value Option<String> The value before the change, if the event recorded one.
new_value Option<String> The value after the change, if the event recorded one.
created_at DateTime<Utc> When the event was recorded; it also picks the <yyyy-mm> folder of the file.

Implements Clone, Debug, Deserialize<'de>, Eq, PartialEq, Serialize.

fn read_all

source

pub async fn read_all(conn: &Connection, project_id: &Id, gest_dir: &Path) -> Result<(), Error>

Import every event/**/*.yaml file into SQLite.

Imported events are inserted directly into transaction_events with no owning transaction (the source collaborator’s transaction is local). The transaction_id column is NOT NULL, so this adapter creates a synthetic “synced” transaction per import batch to satisfy the foreign key.

Events already in the table are left as they are (ON CONFLICT DO NOTHING). The synthetic transaction is deleted again when every file was missing or a tombstone, so that no insert was tried.

Parameter Type Description
conn &Connection the open database connection
project_id &Id the project that owns the synthetic transaction
gest_dir &Path the project’s .gest/ directory
returns Result<(), Error> () when there are no files or every file is imported, or the first read, parse or database error.

fn write_all

source

pub async fn write_all(conn: &Connection, project_id: &Id, gest_dir: &Path) -> Result<(), Error>

Export the audit-log subset of transaction_events to per-event YAML files.

Only events of this project’s transactions that carry a semantic_type are written. Files for events no longer in that set are removed afterwards, except tombstone files.

Parameter Type Description
conn &Connection the open database connection
project_id &Id the project whose events are exported, and whose sync_digests cache records the writes
gest_dir &Path the project’s .gest/ directory
returns Result<(), Error> () once every file is written and orphans are cleaned up, or the first database, parse or I/O error.