| 1 | //! A workspace renamed: rows kept under its old slug move to the one it |
| 2 | //! has now. See `g1t_kit::rename`. |
| 3 | |
| 4 | /// `?1` is the current slug, `?2` a stale one. |
| 5 | pub const STATEMENTS: &[&str] = &[ |
| 6 | "UPDATE connections SET workspace = ?1 WHERE workspace = ?2", |
| 7 | "UPDATE ai_providers SET workspace = ?1 WHERE workspace = ?2", |
| 8 | "UPDATE links SET workspace = ?1 WHERE workspace = ?2", |
| 9 | "UPDATE model_sessions SET workspace = ?1 WHERE workspace = ?2", |
| 10 | "UPDATE model_sessions SET repo = ?1 || substr(repo, length(?2) + 1) WHERE substr(repo, 1, length(?2) + 1) = ?2 || '/'", |
| 11 | "UPDATE deliveries SET item = ?1 || substr(item, length(?2) + 1) WHERE substr(item, 1, length(?2) + 1) = ?2 || '#'", |
| 12 | "UPDATE OR IGNORE ai_routes SET workspace = ?1 WHERE workspace = ?2", |
| 13 | "DELETE FROM ai_routes WHERE workspace = ?2", |
| 14 | "UPDATE OR IGNORE github_installations SET workspace = ?1 WHERE workspace = ?2", |
| 15 | "DELETE FROM github_installations WHERE workspace = ?2", |
| 16 | "UPDATE github_repos SET workspace = ?1 WHERE workspace = ?2", |
| 17 | "UPDATE github_repos SET repo = ?1 || substr(repo, length(?2) + 1) WHERE substr(repo, 1, length(?2) + 1) = ?2 || '/'", |
| 18 | "UPDATE agent_runs SET workspace = ?1 WHERE workspace = ?2", |
| 19 | "UPDATE agent_runs SET repo = ?1 || substr(repo, length(?2) + 1) WHERE substr(repo, 1, length(?2) + 1) = ?2 || '/'", |
| 20 | ]; |
| 21 | |
| 22 | /// A repository transferred (see `g1t_kit::transfer`): what was kept for |
| 23 | /// it follows its new path. Work items, and so their links to outside |
| 24 | /// trackers, stay with their workspace, as do the workspace's connections |
| 25 | /// and AI providers. |
| 26 | pub const TRANSFERRED: &[&str] = &[ |
| 27 | "UPDATE model_sessions SET repo = ?1 WHERE repo = ?2", |
| 28 | "UPDATE github_repos SET workspace = ?3, repo = ?1 WHERE repo = ?2", |
| 29 | "UPDATE agent_runs SET workspace = ?3, repo = ?1 WHERE repo = ?2", |
| 30 | ]; |
| 31 | |
| 32 | /// A repository purged: the model sessions of its runs and its link to a |
| 33 | /// GitHub repository go. Work items outlive it, with their links. `?1` its |
| 34 | /// id, `?2` its path (`namespace/name`). |
| 35 | pub const PURGED: &[&str] = &[ |
| 36 | "DELETE FROM model_sessions WHERE repo = ?2", |
| 37 | "DELETE FROM github_repos WHERE repo_id = ?1", |
| 38 | "UPDATE agent_runs SET status = 'ended' WHERE repo = ?2", |
| 39 | ]; |
| 40 | |
| 41 | /// Runs [`PURGED`] for `event` when it is `repo.purged`. |
| 42 | pub async fn on_purged(db: &worker::D1Database, event: &g1t_contracts::events::Event) -> worker::Result<()> { |
| 43 | let Some(g1t_kit::lifecycle::Lifecycle::Purged(purged)) = g1t_kit::lifecycle::read(event) else { |
| 44 | return Ok(()); |
| 45 | }; |
| 46 | let path = format!("{}/{}", purged.namespace, purged.name); |
| 47 | let values: [worker::wasm_bindgen::JsValue; 2] = [purged.repo_id.as_str().into(), path.as_str().into()]; |
| 48 | let mut batch = Vec::with_capacity(PURGED.len()); |
| 49 | for sql in PURGED { |
| 50 | batch.push(db.prepare(*sql).bind(&values[..g1t_kit::transfer::parameters(sql)])?); |
| 51 | } |
| 52 | db.batch(batch).await?; |
| 53 | Ok(()) |
| 54 | } |
| 55 | |
| 56 | /// A workspace deleted: its connections, links, AI providers and what Ask |
| 57 | /// answers with go. `?1` its slug. |
| 58 | pub const DELETED: &[&str] = &[ |
| 59 | "DELETE FROM managed_agents WHERE connection_id IN (SELECT id FROM ai_providers WHERE workspace = ?1)", |
| 60 | "DELETE FROM connections WHERE workspace = ?1", |
| 61 | "DELETE FROM ai_providers WHERE workspace = ?1", |
| 62 | "DELETE FROM links WHERE workspace = ?1", |
| 63 | "DELETE FROM ai_routes WHERE workspace = ?1", |
| 64 | "DELETE FROM github_installations WHERE workspace = ?1", |
| 65 | "DELETE FROM github_repos WHERE workspace = ?1", |
| 66 | "DELETE FROM agent_runs WHERE workspace = ?1", |
| 67 | ]; |
| 68 | |
| 69 | #[cfg(test)] |
| 70 | mod tests { |
| 71 | use super::{DELETED, PURGED, STATEMENTS, TRANSFERRED}; |
| 72 | |
| 73 | #[test] |
| 74 | fn every_statement_takes_the_two_slugs() { |
| 75 | for sql in STATEMENTS { |
| 76 | assert_eq!(g1t_kit::rename::parameters(sql), 2, "{sql}"); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | #[test] |
| 81 | fn transfer_and_deletion_statements_take_what_they_name() { |
| 82 | for sql in TRANSFERRED { |
| 83 | assert!((1..=5).contains(&g1t_kit::transfer::parameters(sql)), "{sql}"); |
| 84 | } |
| 85 | for sql in DELETED { |
| 86 | assert_eq!(g1t_kit::rename::parameters(sql), 1, "{sql}"); |
| 87 | } |
| 88 | for sql in PURGED { |
| 89 | assert!((1..=2).contains(&g1t_kit::transfer::parameters(sql)), "{sql}"); |
| 90 | } |
| 91 | } |
| 92 | } |