Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

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