pr_01m47d24b0e6n91zwymwxg0vpx/services/webhooks/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 hooks SET workspace = ?1 WHERE workspace = ?2", |
| 7 | "UPDATE hooks SET repo = ?1 || substr(repo, length(?2) + 1) WHERE substr(repo, 1, length(?2) + 1) = ?2 || '/'", |
| 8 | "UPDATE repo_names SET namespace = ?1 WHERE namespace = ?2", |
| 9 | ]; |
| 10 | |
| 11 | /// A repository renamed or transferred (see `g1t_kit::transfer`): its own |
| 12 | /// webhooks go with it, and its payloads name it by its new path. After a |
| 13 | /// transfer the old workspace's webhooks stop hearing about it; the new |
| 14 | /// one's start. |
| 15 | pub const TRANSFERRED: &[&str] = &[ |
| 16 | "UPDATE hooks SET workspace = ?3, repo = ?1 WHERE scope = 'repo' AND repo_id = ?5", |
| 17 | "UPDATE repo_names SET namespace = ?3, name = ?6 WHERE repo_id = ?5", |
| 18 | ]; |
| 19 | |
| 20 | /// A repository purged (see `g1t_kit::lifecycle`), once its last event is |
| 21 | /// delivered: its own webhooks and what was sent to them go. `?1` its id. |
| 22 | /// What its workspace's webhooks were sent stays, as their history. |
| 23 | pub const PURGED: &[&str] = &[ |
| 24 | "DELETE FROM deliveries WHERE hook_id IN (SELECT id FROM hooks WHERE scope = 'repo' AND repo_id = ?1)", |
| 25 | "DELETE FROM hooks WHERE scope = 'repo' AND repo_id = ?1", |
| 26 | "DELETE FROM repo_names WHERE repo_id = ?1", |
| 27 | ]; |
| 28 | |
| 29 | /// A workspace deleted: its own webhooks go. `?1` its slug. |
| 30 | pub const DELETED: &[&str] = &["DELETE FROM hooks WHERE scope = 'workspace' AND workspace = ?1"]; |
| 31 | |
| 32 | #[cfg(test)] |
| 33 | mod tests { |
| 34 | use super::{DELETED, PURGED, STATEMENTS, TRANSFERRED}; |
| 35 | |
| 36 | #[test] |
| 37 | fn every_statement_takes_the_two_slugs() { |
| 38 | for sql in STATEMENTS { |
| 39 | assert_eq!(g1t_kit::rename::parameters(sql), 2, "{sql}"); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn transfer_and_deletion_statements_take_what_they_name() { |
| 45 | for sql in TRANSFERRED { |
| 46 | assert!((1..=7).contains(&g1t_kit::transfer::parameters(sql)), "{sql}"); |
| 47 | } |
| 48 | for sql in PURGED { |
| 49 | assert_eq!(g1t_kit::transfer::parameters(sql), 1, "{sql}"); |
| 50 | } |
| 51 | for sql in DELETED { |
| 52 | assert_eq!(g1t_kit::rename::parameters(sql), 1, "{sql}"); |
| 53 | } |
| 54 | } |
| 55 | } |