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