From c0f220582cd61f0d88e42dfbc29d55b3be1e3b19 Mon Sep 17 00:00:00 2001 From: soryu Date: Sun, 8 Feb 2026 16:18:13 +0000 Subject: Fix directive deletion and stop local only on contracts --- makima/src/db/repository.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'makima/src/db/repository.rs') 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 { + // 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 -- cgit v1.2.3