delete

Cascade delete for top-level entities (artifacts, iterations, tasks).

This helper performs a hard delete of an entity together with every dependent row (notes, entity_tags, relationships, iteration_tasks), recording a deleted event for each row via the transaction log so the operation can be undone.

Event ordering is intentional: children are recorded before the parent so that the reverse-order undo replay re-inserts the parent row first and every child row afterwards (whose FKs reference the parent). The forward delete uses the same child-first ordering so FK constraints are never violated during the mutation itself.

This module does not write tombstone files, prompt the user, or begin a transaction — those concerns belong to the caller.

Captured children are modeled as a typed CapturedChild enum rather than a loose serde_json::Map. This keeps the capture → delete → audit pipeline compile-time checked: adding a new column to a child table forces a corresponding change in the enum variant, and the audit JSON payload is only constructed once, at the point where it is handed to transaction::record_event.

struct DeleteReport

source

pub struct DeleteReport

Per-table counts of rows removed by a delete_with_cascade call.

Field Type Description
iteration_tasks usize Number of iteration_tasks join rows removed.
notes usize Number of attached notes removed.
relationships usize Number of relationships (in either direction) removed.
tags usize Number of entity_tags attachments removed.

Implements Clone, Copy, Debug, Default, Eq, PartialEq.

enum CapturedChild

source

enum CapturedChild

A typed snapshot of a dependent row captured prior to deletion.

Each variant carries the exact columns required to both (a) re-emit the row as an audit payload whose shape matches the on-disk format expected by transaction undo replay, and (b) issue a typed DELETE statement without having to round-trip through serde_json.

Variant Description
EntityTag { .. } An entity_tags row that attaches a tag to the deleted entity.
IterationTask { .. } An iteration_tasks row that links the deleted task or iteration to its partner.
Note { .. } A notes row attached to the deleted entity.
Relationship { .. } A relationships row with the deleted entity as its source or its target.

CapturedChild::audit_row_id

source

fn audit_row_id(&self) -> String

Logical primary key used to identify this row in the audit log.

Parameter Type Description
returns String entity_type:entity_id:tag_id for an entity tag, iteration_id:task_id for an iteration task, and the row’s own id for a note or a relationship.

CapturedChild::delete

source

async fn delete(&self, conn: &Connection) -> Result<(), Error>

Delete this captured row from its backing table.

Parameter Type Description
conn &Connection the connection that runs the DELETE
returns Result<(), Error> Ok(()) once the statement has run, or the database error.

CapturedChild::table

source

fn table(&self) -> &'static str

Name of the backing SQL table this row was captured from.

Parameter Type Description
returns &'static str One of entity_tags, iteration_tasks, notes or relationships.

CapturedChild::to_audit_payload

source

fn to_audit_payload(&self) -> JsonValue

Build the JSON payload stored in the audit log for this row.

The shape here is load-bearing: transaction::undo reconstructs the row via an INSERT whose column list is the object’s key set, so the set of keys and their value types must continue to match the backing table’s schema.

Parameter Type Description
returns JsonValue A JSON object with one key per column of the row; a note without an author has author_id set to null.

fn delete_with_cascade

source

pub async fn delete_with_cascade(
    conn: &Connection,
    transaction_id: &Id,
    entity_type: EntityType,
    entity_id: &Id,
) -> Result<DeleteReport, Error>

Delete an entity and all rows that depend on it, recording transaction events for each removed row so the operation can be undone.

  • Callers are expected to have already begun a transaction and resolved transaction_id; this helper only appends events and issues DELETE statements.
Parameter Type Description
conn &Connection the database connection
transaction_id &Id the transaction that the deleted events are recorded under
entity_type EntityType whether the entity is an artifact, an iteration or a task
entity_id &Id entity_id must be the fully-resolved id of the entity.
returns Result<DeleteReport, Error> Returns a DeleteReport with the number of rows removed from each dependent table. It is Error::NotFound when no entity has that id, or a database error.

fn delete_many_with_cascade

source

pub async fn delete_many_with_cascade(
    conn: &Connection,
    transaction_id: &Id,
    entity_type: EntityType,
    entity_ids: &[&Id],
) -> Result<DeleteReport, Error>

Batched equivalent of delete_with_cascade for a slice of same-type ids.

Captures every dependent row (notes, entity_tags, relationships, iteration_tasks) for the full id set in a single IN (...) query per child table, records one audit event per captured row, then issues a single batched DELETE ... WHERE ... IN (...) per table. The overall ordering is identical to delete_with_cascade: children first, parents last, so FK constraints stay satisfied and undo replay restores rows in reverse.

Parameter Type Description
conn &Connection the connection, inside a transaction the caller has begun
transaction_id &Id the transaction that the deleted events are recorded under
entity_type EntityType the type that every id in entity_ids has
entity_ids &[&Id] the fully-resolved ids of the entities to delete
returns Result<DeleteReport, Error> Returns a combined DeleteReport covering every deleted row across the whole batch. An empty slice returns a zero report without issuing any queries. It is Error::NotFound when any id has no entity, or a database error.

fn capture_entity_tags

source

async fn capture_entity_tags(
    conn: &Connection,
    entity_type: &str,
    entity_id: &str,
) -> Result<Vec<CapturedChild>, Error>

Read the entity_tags rows of one entity, before they are deleted.

Parameter Type Description
conn &Connection the connection to query
entity_type &str the entity’s type, as stored in entity_tags.entity_type
entity_id &str the entity’s full id
returns Result<Vec<CapturedChild>, Error> One CapturedChild::EntityTag per tag attached to the entity, possibly none, or a database error.

fn capture_iteration_tasks_for_iteration

source

async fn capture_iteration_tasks_for_iteration(
    conn: &Connection,
    iteration_id: &str,
) -> Result<Vec<CapturedChild>, Error>

Read the iteration_tasks rows that put tasks in one iteration, before they are deleted.

Parameter Type Description
conn &Connection the connection to query
iteration_id &str the iteration’s full id
returns Result<Vec<CapturedChild>, Error> One CapturedChild::IterationTask per task in the iteration, possibly none, or a database error.

fn capture_iteration_tasks_for_task

source

async fn capture_iteration_tasks_for_task(
    conn: &Connection,
    task_id: &str,
) -> Result<Vec<CapturedChild>, Error>

Read the iteration_tasks rows that put one task in iterations, before they are deleted.

Parameter Type Description
conn &Connection the connection to query
task_id &str the task’s full id
returns Result<Vec<CapturedChild>, Error> One CapturedChild::IterationTask per iteration that holds the task, possibly none, or a database error.

fn capture_notes

source

async fn capture_notes(
    conn: &Connection,
    entity_type: &str,
    entity_id: &str,
) -> Result<Vec<CapturedChild>, Error>

Read the notes attached to one entity, before they are deleted.

Parameter Type Description
conn &Connection the connection to query
entity_type &str the entity’s type, as stored in notes.entity_type
entity_id &str the entity’s full id
returns Result<Vec<CapturedChild>, Error> One CapturedChild::Note per note on the entity, possibly none, or a database error.

fn capture_parent

source

async fn capture_parent(
    conn: &Connection,
    entity_type: EntityType,
    id: &str,
) -> Result<Value, Error>

Read the entity’s own row as a JSON object, for its deleted audit event.

Parameter Type Description
conn &Connection the connection to query
entity_type EntityType picks the table and its SELECT_COLUMNS
id &str the entity’s full id
returns Result<Value, Error> An object keyed by the table’s SELECT_COLUMNS, Error::NotFound when no row has the id, or a database error.

fn capture_relationships

source

async fn capture_relationships(
    conn: &Connection,
    entity_type: &str,
    entity_id: &str,
) -> Result<Vec<CapturedChild>, Error>

Read the relationships with the entity as source or target, before they are deleted.

Parameter Type Description
conn &Connection the connection to query
entity_type &str the entity’s type, matched against source_type or target_type
entity_id &str the entity’s full id
returns Result<Vec<CapturedChild>, Error> One CapturedChild::Relationship per relationship in either direction, possibly none, or a database error.

fn collect_iteration_task_rows

source

async fn collect_iteration_task_rows(rows: &mut libsql::Rows) -> Result<Vec<CapturedChild>, Error>

Turn rows of iteration_id, task_id, phase, created_at into captured children.

Parameter Type Description
rows &mut libsql::Rows the query result, read to its end
returns Result<Vec<CapturedChild>, Error> One CapturedChild::IterationTask per row, or the error from reading a row.

fn libsql_value_to_json

source

fn libsql_value_to_json(value: Value) -> Value

Convert one column value to JSON for an audit payload.

A real that JSON cannot hold, such as NaN, becomes null. A blob becomes the text <blob N bytes>, so its bytes are not kept.

Parameter Type Description
value Value the column value from libsql
returns Value The JSON value: null, a number or a string.

fn record_and_delete_child

source

async fn record_and_delete_child(
    conn: &Connection,
    transaction_id: &Id,
    child: &CapturedChild,
) -> Result<(), Error>

Record a deleted event for one captured child, then delete its row.

Parameter Type Description
conn &Connection the connection, inside the caller’s transaction
transaction_id &Id the transaction that the event is recorded under
child &CapturedChild the row to record and delete
returns Result<(), Error> Ok(()) once both have run, or the database error.

fn record_and_delete_parent

source

async fn record_and_delete_parent(
    conn: &Connection,
    transaction_id: &Id,
    entity_type: EntityType,
    before: &Value,
    id: &str,
) -> Result<(), Error>

Record a deleted event for the entity’s own row, then delete it.

Parameter Type Description
conn &Connection the connection, inside the caller’s transaction
transaction_id &Id the transaction that the event is recorded under
entity_type EntityType picks the table
before &Value the row as captured by [capture_parent], stored as the event’s payload
id &str the entity’s full id
returns Result<(), Error> Ok(()) once both have run, or the database error.

fn parent_table_and_sql

source

fn parent_table_and_sql(entity_type: EntityType) -> (&'static str, &'static str)

Returns the (table_name, single-row DELETE sql) for an entity-parent table.

Parameter Type Description
entity_type EntityType the type of the entity
returns (&'static str, &'static str) The table is artifacts, iterations or tasks, and the SQL is DELETE ... WHERE id = ?1 on it.

fn placeholders

source

fn placeholders(start: usize, count: usize) -> String

Build the ?N, ?N+1, ... placeholder list starting at the given 1-based index.

Parameter Type Description
start usize the number of the first placeholder
count usize how many placeholders to write
returns String The placeholders joined by , , such as ?2, ?3, ?4; empty when count is 0.

fn capture_entity_tags_many

source

async fn capture_entity_tags_many(
    conn: &Connection,
    entity_type: &str,
    entity_ids: &[String],
) -> Result<Vec<CapturedChild>, Error>

Batched capture_entity_tags: read the entity_tags rows of many entities in one query.

Parameter Type Description
conn &Connection the connection to query
entity_type &str the type of every entity
entity_ids &[String] the entities’ full ids
returns Result<Vec<CapturedChild>, Error> One CapturedChild::EntityTag per row found; empty, with no query, when entity_ids is empty. Or a database error.

fn capture_iteration_tasks_for_iterations

source

async fn capture_iteration_tasks_for_iterations(
    conn: &Connection,
    iteration_ids: &[String],
) -> Result<Vec<CapturedChild>, Error>

Batched capture_iteration_tasks_for_iteration: read the iteration_tasks rows of many iterations.

Parameter Type Description
conn &Connection the connection to query
iteration_ids &[String] the iterations’ full ids
returns Result<Vec<CapturedChild>, Error> One CapturedChild::IterationTask per row found; empty, with no query, when iteration_ids is empty. Or a database error.

fn capture_iteration_tasks_for_tasks

source

async fn capture_iteration_tasks_for_tasks(
    conn: &Connection,
    task_ids: &[String],
) -> Result<Vec<CapturedChild>, Error>

Batched capture_iteration_tasks_for_task: read the iteration_tasks rows of many tasks.

Parameter Type Description
conn &Connection the connection to query
task_ids &[String] the tasks’ full ids
returns Result<Vec<CapturedChild>, Error> One CapturedChild::IterationTask per row found; empty, with no query, when task_ids is empty. Or a database error.

fn capture_notes_many

source

async fn capture_notes_many(
    conn: &Connection,
    entity_type: &str,
    entity_ids: &[String],
) -> Result<Vec<CapturedChild>, Error>

Batched capture_notes: read the notes of many entities in one query.

Parameter Type Description
conn &Connection the connection to query
entity_type &str the type of every entity
entity_ids &[String] the entities’ full ids
returns Result<Vec<CapturedChild>, Error> One CapturedChild::Note per note found; empty, with no query, when entity_ids is empty. Or a database error.

fn capture_parents_many

source

async fn capture_parents_many(
    conn: &Connection,
    entity_type: EntityType,
    entity_ids: &[String],
) -> Result<Vec<(String, Value)>, Error>

Batched capture_parent: read many entities’ own rows as JSON objects in one query.

Parameter Type Description
conn &Connection the connection to query
entity_type EntityType picks the table and its SELECT_COLUMNS
entity_ids &[String] the entities’ full ids
returns Result<Vec<(String, Value)>, Error> One (id, row object) pair per entity, in the order the query returns them; empty when entity_ids is empty. It is Error::NotFound when any id has no row, or a database error.

fn capture_relationships_many

source

async fn capture_relationships_many(
    conn: &Connection,
    entity_type: &str,
    entity_ids: &[String],
) -> Result<Vec<CapturedChild>, Error>

Batched capture_relationships: read the relationships of many entities in one query.

Parameter Type Description
conn &Connection the connection to query
entity_type &str the type of every entity
entity_ids &[String] the entities’ full ids, matched as source or target
returns Result<Vec<CapturedChild>, Error> One CapturedChild::Relationship per relationship found; empty, with no query, when entity_ids is empty. Or a database error.

fn delete_entity_tags_many

source

async fn delete_entity_tags_many(
    conn: &Connection,
    entity_type: &str,
    entity_ids: &[String],
    any: bool,
) -> Result<(), Error>

Delete the entity_tags rows of many entities in one statement.

Parameter Type Description
conn &Connection the connection, inside the caller’s transaction
entity_type &str the type of every entity
entity_ids &[String] the entities’ full ids
any bool whether any rows were captured; when false nothing runs
returns Result<(), Error> Ok(()) once the statement has run or been skipped, or the database error.

fn delete_iteration_tasks_for_iterations

source

async fn delete_iteration_tasks_for_iterations(
    conn: &Connection,
    iteration_ids: &[String],
) -> Result<(), Error>

Delete the iteration_tasks rows of many iterations in one statement.

Parameter Type Description
conn &Connection the connection, inside the caller’s transaction
iteration_ids &[String] the iterations’ full ids
returns Result<(), Error> Ok(()) once the statement has run, or the database error.

fn delete_iteration_tasks_for_tasks

source

async fn delete_iteration_tasks_for_tasks(
    conn: &Connection,
    task_ids: &[String],
) -> Result<(), Error>

Delete the iteration_tasks rows of many tasks in one statement.

Parameter Type Description
conn &Connection the connection, inside the caller’s transaction
task_ids &[String] the tasks’ full ids
returns Result<(), Error> Ok(()) once the statement has run, or the database error.

fn delete_notes_many

source

async fn delete_notes_many(
    conn: &Connection,
    entity_type: &str,
    entity_ids: &[String],
    any: bool,
) -> Result<(), Error>

Delete the notes of many entities in one statement.

Parameter Type Description
conn &Connection the connection, inside the caller’s transaction
entity_type &str the type of every entity
entity_ids &[String] the entities’ full ids
any bool whether any notes were captured; when false nothing runs
returns Result<(), Error> Ok(()) once the statement has run or been skipped, or the database error.

fn delete_parents_many

source

async fn delete_parents_many(
    conn: &Connection,
    entity_type: EntityType,
    entity_ids: &[String],
) -> Result<(), Error>

Delete many entities’ own rows in one statement.

Parameter Type Description
conn &Connection the connection, inside the caller’s transaction
entity_type EntityType picks the table: artifacts, iterations or tasks
entity_ids &[String] the entities’ full ids
returns Result<(), Error> Ok(()) once the statement has run, or the database error.

fn delete_relationships_many

source

async fn delete_relationships_many(
    conn: &Connection,
    entity_type: &str,
    entity_ids: &[String],
    any: bool,
) -> Result<(), Error>

Delete the relationships of many entities, in either direction, in one statement.

Parameter Type Description
conn &Connection the connection, inside the caller’s transaction
entity_type &str the type of every entity
entity_ids &[String] the entities’ full ids, matched as source or target
any bool whether any relationships were captured; when false nothing runs
returns Result<(), Error> Ok(()) once the statement has run or been skipped, or the database error.