summaryrefslogtreecommitdiff
path: root/makima/src/db/repository.rs
diff options
context:
space:
mode:
Diffstat (limited to 'makima/src/db/repository.rs')
-rw-r--r--makima/src/db/repository.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/makima/src/db/repository.rs b/makima/src/db/repository.rs
index 63493b9..6b3f15f 100644
--- a/makima/src/db/repository.rs
+++ b/makima/src/db/repository.rs
@@ -5130,11 +5130,32 @@ pub async fn update_directive_for_owner(
}
/// Delete a directive by ID, scoped to owner.
+/// Also deletes all contracts (and their cascaded tasks/files) associated with this directive.
pub async fn delete_directive_for_owner(
pool: &PgPool,
id: Uuid,
owner_id: Uuid,
) -> Result<bool, sqlx::Error> {
+ // First verify the directive exists and belongs to the owner
+ let directive = get_directive_for_owner(pool, id, owner_id).await?;
+ let Some(_directive) = directive else {
+ return Ok(false);
+ };
+
+ // Delete all contracts linked to this directive (tasks/files cascade from contracts).
+ // This covers step contracts (directive_id FK) and the orchestrator contract.
+ sqlx::query(
+ r#"
+ DELETE FROM contracts
+ WHERE directive_id = $1
+ OR id = (SELECT orchestrator_contract_id FROM directives WHERE id = $1)
+ "#,
+ )
+ .bind(id)
+ .execute(pool)
+ .await?;
+
+ // Now delete the directive itself (chains, steps, events, evaluations cascade via FK)
let result = sqlx::query(
r#"
DELETE FROM directives