transaction
Each undoable command opens a transaction row and records one event per changed row, with the
row’s state before the change. undo replays those events in reverse to restore that state.
const SELECT_COLUMNS
const SELECT_COLUMNS: &str = "id, project_id, author_id, command, undone_at, created_at";The columns of transactions in the order that the transaction model’s TryFrom<Row> reads them.
fn begin
pub async fn begin(conn: &Connection, project_id: &Id, command: &str) -> Result<Model, Error>Begin a new transaction for the given command, with no author.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
project_id |
&Id | the project the transaction belongs to |
command |
&str | the command text recorded on the transaction, which [undo] returns |
| returns | Result<Model, Error> | The inserted transaction as read back from the database, or an error when the insert or the read fails. |
fn find_by_id
pub async fn find_by_id(conn: &Connection, id: impl Into<Id>) -> Result<Option<Model>, Error>Find a transaction by its ID.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
id |
impl Into<Id> | the full transaction id |
| returns | Result<Option<Model>, Error> | The transaction, None when no row has that id, or an error when the query or row decoding fails. |
fn latest_undoable
pub async fn latest_undoable(conn: &Connection, project_id: &Id) -> Result<Option<Model>, Error>Return the most recent non-undone transaction for a project that changed something.
A transaction with no events, opened by a command that changed nothing, is not an undo step.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
project_id |
&Id | the project to search |
| returns | Result<Option<Model>, Error> | The transaction, newest by created_at; None when there is none, or an error when the query or row decoding fails. |
fn record_event
pub async fn record_event(
conn: &Connection,
transaction_id: &Id,
table_name: &str,
row_id: &str,
event_type: &str,
before_data: Option<&Value>,
) -> Result<(), Error>Record a change event within a transaction.
This records the row-level audit metadata needed for undo replay and leaves
the timeline-facing semantic fields NULL. Use record_semantic_event
instead when the mutation maps to a user-facing activity entry
(create/status-change/phase-change/priority-change/archive/complete/cancel).
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
transaction_id |
&Id | the transaction the event belongs to |
table_name |
&str | the table of the changed row, as its SQL name |
row_id |
&str | the primary key of the changed row |
event_type |
&str | created, modified or deleted; [undo] ignores any other value |
before_data |
Option<&Value> | the row as JSON before the change, or None for a created row; a created row in a join table carries its key columns instead, as [undo] needs them |
| returns | Result<(), Error> | Ok(()) once the event row is inserted, or an error when the insert fails. |
fn record_semantic_event
pub async fn record_semantic_event(
conn: &Connection,
transaction_id: &Id,
table_name: &str,
row_id: &str,
event_type: &str,
before_data: Option<&Value>,
semantic_type: Option<&str>,
old_value: Option<&str>,
new_value: Option<&str>,
) -> Result<(), Error>Record a change event with semantic timeline metadata.
semantic_type, old_value, and new_value feed the unified activity
timeline. They should be populated for user-facing mutations and left
None for internal or free-form edits that have no human-readable
timeline entry (in which case the plain record_event is sufficient).
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
transaction_id |
&Id | the transaction the event belongs to |
table_name |
&str | the table of the changed row, as its SQL name |
row_id |
&str | the primary key of the changed row |
event_type |
&str | created, modified or deleted; [undo] ignores any other value |
before_data |
Option<&Value> | the row as JSON before the change, or None for a created row; a created row in a join table carries its key columns instead, as [undo] needs them |
semantic_type |
Option<&str> | the activity kind |
old_value |
Option<&str> | the value before the change |
new_value |
Option<&str> | the value after the change |
| returns | Result<(), Error> | Ok(()) once the event row is inserted, or an error when the insert fails. |
struct SemanticEvent
pub struct SemanticEventA semantic event entry for the activity timeline, joined with its parent transaction’s author and creation timestamp.
| Field | Type | Description |
|---|---|---|
author_id |
Option<Id> | Author of the parent transaction, if any. |
created_at |
chrono::DateTime<Utc> | When the event was recorded. |
id |
Id | Unique identifier of the event row. |
new_value |
Option<String> | Post-change value, when applicable. |
old_value |
Option<String> | Pre-change value, when applicable. |
row_id |
String | Primary key of the row the event refers to. |
semantic_type |
String | User-facing activity kind (e.g. created, status-change). |
table_name |
String | Name of the table the row belongs to. |
fn semantic_events_for_row
pub async fn semantic_events_for_row(
conn: &Connection,
table_name: &str,
row_id: &Id,
) -> Result<Vec<SemanticEvent>, Error>Return all semantic events for a given row, ordered by creation time ascending.
Filters out events with semantic_type IS NULL, so free-form edits
never appear in the activity timeline. Events of undone transactions are left out too.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
table_name |
&str | the table of the row, as its SQL name |
row_id |
&Id | the primary key of the row |
| returns | Result<Vec<SemanticEvent>, Error> | The events, each with its transaction’s author, or an error when the query fails or a stored id or timestamp does not parse. |
fn undo
pub async fn undo(conn: &Connection, transaction_id: &Id) -> Result<String, Error>Undo a transaction by replaying its events in reverse order of insertion. The order does not use
created_at, so a clock that steps back during a command cannot swap two of its events.
The replay runs in its own transaction (BEGIN IMMEDIATE), or in a savepoint when the caller already holds
a transaction. When any event fails, or the commit fails, every change of the undo is rolled back and the
transaction stays undoable. A caller that holds a transaction should roll it back when undo returns an
error: in the rare case that the savepoint cannot be rolled back, the caller’s commit would keep a partial
undo.
A created event deletes the row whose id is the event’s row_id, a modified event writes back the
columns of before_data other than the table’s key columns, and a deleted event re-inserts before_data with
INSERT OR IGNORE. The transaction is then marked undone. entity_tags and iteration_tasks have no id
column, so a created or modified event on them carries the row’s key columns (Table::key_columns) in
before_data, and undo deletes or updates the row with that key. Such an event recorded without its key fails
with Error::InvalidValue; a modified event with no before_data at all, such as the timeline’s
phase-change entry, changes nothing.
In a modified event, a JSON null in before_data writes back SQL NULL, except in a JSON column such as
metadata, where it writes back the text null. A deleted event’s before_data is the raw row, so its
values are written back as stored.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
transaction_id |
&Id | the transaction to undo |
| returns | Result<String, Error> | The command text of the undone transaction, Error::NothingToUndo when no transaction has that id, or Error::InvalidValue when an event names an unknown table or column, or a database error. |
fn missing_join_key
pub async fn missing_join_key(
conn: &Connection,
transaction_id: &Id,
) -> Result<Option<String>, Error>Find the first join-table event in a transaction that undo cannot replay because it lacks its key.
Gest recorded created events on entity_tags and iteration_tasks without the row’s key columns before
those events carried them in before_data. undo refuses such an event, so the transaction can never be
undone. This applies the same rule as undo: a created event needs every key column, and a modified
event that would write a column needs them too.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
transaction_id |
&Id | the transaction to check |
| returns | Result<Option<String>, Error> | Some("<table> <event> event has no <column>") for the first such event, None when every event has its key, or a database or decoding error. An event on an unknown table is left for undo to report. |
fn mark_skipped
pub async fn mark_skipped(conn: &Connection, transaction_id: &Id) -> Result<(), Error>Mark a transaction that undo cannot replay as done with, so undo no longer offers it.
Its changes stay in the database. It is marked through undone_at, the field undo already uses to pass over a
transaction.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
transaction_id |
&Id | the transaction to pass over |
| returns | Result<(), Error> | Ok(()), or the database error. |
fn replay
async fn replay(conn: &Connection, transaction_id: &Id) -> Result<String, Error>Replays the events of one transaction and marks it undone, for undo, which wraps it in a transaction.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the database connection |
transaction_id |
&Id | the transaction to undo |
| returns | Result<String, Error> | The command text of the undone transaction, or the first error. |
const JSON_COLUMNS
const JSON_COLUMNS: &[&str] = &["metadata"];Columns that store JSON as text. Their value is written back as JSON text, even when it is null.
fn json_to_sql
fn json_to_sql(column: &str, value: &Value) -> ValueConverts one value of a modified event’s before_data into the SQL value that restores it.
A modified event’s before_data is the serialized model, so a JSON column such as metadata holds a JSON
value, not its stored text.
| Parameter | Type | Description |
|---|---|---|
column |
&str | the column the value belongs to |
value |
&Value | the JSON value of that column |
| returns | Value | For a JSON column, the JSON text of the value, so a null comes back as the text null and not as SQL NULL. For any other column, the same value as raw_row_to_sql. |
fn raw_row_to_sql
fn raw_row_to_sql(value: &Value) -> ValueConverts one value of a raw row, as a deleted event stores it, into the SQL value that restores it.
| Parameter | Type | Description |
|---|---|---|
value |
&Value | the JSON value of one column of the row |
| returns | Value | SQL NULL for a JSON null, the text for a JSON string, and the JSON text of any other value. |