g1t/services/integrations/src/rename.rs
| 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 links SET workspace = ?1 WHERE workspace = ?2", |
| 8 | "UPDATE links SET repo = ?1 || substr(repo, length(?2) + 1) WHERE substr(repo, 1, length(?2) + 1) = ?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 issue = ?1 || substr(issue, length(?2) + 1) WHERE substr(issue, 1, length(?2) + 1) = ?2 || '/'", |
| 12 | "UPDATE OR IGNORE model_routes SET workspace = ?1 WHERE workspace = ?2", |
| 13 | "DELETE FROM model_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 | ]; |
| 19 | |
| 20 | /// A repository transferred (see `g1t_kit::transfer`): its issues' links to |
| 21 | /// outside trackers, and what was sent about them, follow its new path. |
| 22 | /// The workspace's connections stay with the workspace. |
| 23 | pub const TRANSFERRED: &[&str] = &[ |
| 24 | "UPDATE links SET workspace = ?3, repo = ?1 WHERE repo = ?2", |
| 25 | "UPDATE model_sessions SET repo = ?1 WHERE repo = ?2", |
| 26 | "UPDATE github_repos SET workspace = ?3, repo = ?1 WHERE repo = ?2", |
| 27 | "UPDATE deliveries SET issue = ?1 || substr(issue, length(?2) + 1) WHERE substr(issue, 1, length(?2) + 1) = ?2 || '#'", |
| 28 | ]; |
| 29 | |
| 30 | /// A repository purged: its issues' links to outside trackers, the model |
| 31 | /// sessions of its runs, what was received about its issues, and its link |
| 32 | /// to a GitHub repository go. `?1` its id, `?2` its path (`namespace/name`). |
| 33 | pub const PURGED: &[&str] = &[ |
| 34 | "DELETE FROM links WHERE repo_id = ?1", |
| 35 | "DELETE FROM model_sessions WHERE repo = ?2", |
| 36 | "DELETE FROM deliveries WHERE substr(issue, 1, length(?2) + 1) = ?2 || '#'", |
| 37 | "DELETE FROM github_repos WHERE repo_id = ?1", |
| 38 | ]; |
| 39 | |
| 40 | /// Runs [`PURGED`] for `event` when it is `repo.purged`. |
| 41 | pub async fn on_purged(db: &worker::D1Database, event: &g1t_contracts::events::Event) -> worker::Result<()> { |
| 42 | let Some(g1t_kit::lifecycle::Lifecycle::Purged(purged)) = g1t_kit::lifecycle::read(event) else { |
| 43 | return Ok(()); |
| 44 | }; |
| 45 | let path = format!("{}/{}", purged.namespace, purged.name); |
| 46 | let values: [worker::wasm_bindgen::JsValue; 2] = [purged.repo_id.as_str().into(), path.as_str().into()]; |
| 47 | let mut batch = Vec::with_capacity(PURGED.len()); |
| 48 | for sql in PURGED { |
| 49 | batch.push(db.prepare(*sql).bind(&values[..g1t_kit::transfer::parameters(sql)])?); |
| 50 | } |
| 51 | db.batch(batch).await?; |
| 52 | Ok(()) |
| 53 | } |
| 54 | |
| 55 | /// A workspace deleted: its connections, links and model routes go. `?1` |
| 56 | /// its slug. |
| 57 | pub const DELETED: &[&str] = &[ |
| 58 | "DELETE FROM connections WHERE workspace = ?1", |
| 59 | "DELETE FROM links WHERE workspace = ?1", |
| 60 | "DELETE FROM model_routes WHERE workspace = ?1", |
| 61 | "DELETE FROM github_installations WHERE workspace = ?1", |
| 62 | "DELETE FROM github_repos WHERE workspace = ?1", |
| 63 | ]; |
| 64 | |
| 65 | #[cfg(test)] |
| 66 | mod tests { |
| 67 | use super::{DELETED, PURGED, STATEMENTS, TRANSFERRED}; |
| 68 | |
| 69 | #[test] |
| 70 | fn every_statement_takes_the_two_slugs() { |
| 71 | for sql in STATEMENTS { |
| 72 | assert_eq!(g1t_kit::rename::parameters(sql), 2, "{sql}"); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | #[test] |
| 77 | fn transfer_and_deletion_statements_take_what_they_name() { |
| 78 | for sql in TRANSFERRED { |
| 79 | assert!((1..=5).contains(&g1t_kit::transfer::parameters(sql)), "{sql}"); |
| 80 | } |
| 81 | for sql in DELETED { |
| 82 | assert_eq!(g1t_kit::rename::parameters(sql), 1, "{sql}"); |
| 83 | } |
| 84 | for sql in PURGED { |
| 85 | assert!((1..=2).contains(&g1t_kit::transfer::parameters(sql)), "{sql}"); |
| 86 | } |
| 87 | } |
| 88 | } |