Skip to content

Commit 6773794

Browse files
committed
Writing tests and connecting to query
1 parent b682441 commit 6773794

File tree

9 files changed

+205
-33
lines changed

9 files changed

+205
-33
lines changed

Cargo.lock

Lines changed: 18 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ async-compression = { version = "0.3.13", default-features = false, features = [
9898
"gzip",
9999
"tokio",
100100
] }
101+
async-graphql = "7.0.7"
102+
async-graphql-axum = "7.0.7"
101103
async-trait = "0.1.64"
102104
atty = "0.2.14"
103105
axum = "0.7.5"

crates/turborepo-api-client/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ turborepo-vercel-api-mock = { workspace = true }
2121
workspace = true
2222

2323
[dependencies]
24+
async-graphql = { workspace = true }
2425
bytes.workspace = true
2526
chrono = { workspace = true, features = ["serde"] }
2627
lazy_static = { workspace = true }

crates/turborepo-api-client/src/spaces.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use async_graphql::{Enum, SimpleObject};
12
use chrono::{DateTime, Local};
23
use reqwest::Method;
34
use serde::Serialize;
@@ -29,7 +30,7 @@ pub struct SpaceClientSummary {
2930
pub version: String,
3031
}
3132

32-
#[derive(Debug, Serialize, Default)]
33+
#[derive(Debug, Serialize, Default, SimpleObject)]
3334
#[serde(rename_all = "camelCase")]
3435
pub struct SpacesCacheStatus {
3536
pub status: CacheStatus,
@@ -38,21 +39,21 @@ pub struct SpacesCacheStatus {
3839
pub time_saved: u64,
3940
}
4041

41-
#[derive(Debug, Serialize, Copy, Clone)]
42+
#[derive(Debug, Serialize, Copy, Clone, PartialEq, Eq, Enum)]
4243
#[serde(rename_all = "UPPERCASE")]
4344
pub enum CacheStatus {
4445
Hit,
4546
Miss,
4647
}
4748

48-
#[derive(Debug, Serialize, Copy, Clone)]
49+
#[derive(Debug, Serialize, Copy, Clone, PartialEq, Eq, Enum)]
4950
#[serde(rename_all = "UPPERCASE")]
5051
pub enum CacheSource {
5152
Local,
5253
Remote,
5354
}
5455

55-
#[derive(Default, Debug, Serialize)]
56+
#[derive(Default, Debug, Serialize, SimpleObject)]
5657
#[serde(rename_all = "camelCase")]
5758
pub struct SpaceTaskSummary {
5859
pub key: String,

crates/turborepo-db/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
async-graphql = "7.0.7"
78
camino = { workspace = true }
89
serde_json = { workspace = true }
910
sqlx = { version = "0.8.1", features = ["runtime-tokio", "sqlite"] }
11+
tempfile = { workspace = true }
1012
thiserror = { workspace = true }
13+
tokio = { workspace = true, features = ["full"] }
1114
turbopath = { workspace = true }
1215
turborepo-api-client = { workspace = true }
1316
uuid = { workspace = true, features = ["v4"] }
1417

18+
[dev-dependencies]
19+
anyhow = { workspace = true }
20+
1521
[lints]
1622
workspace = true

crates/turborepo-db/migrations/20240828183512_initial_tables.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CREATE TABLE IF NOT EXISTS runs (
22
id TEXT PRIMARY KEY, -- primary key should be uuid
3-
start_time INTEGER NOT NULL,
4-
end_time INTEGER,
3+
start_time BIGINT NOT NULL,
4+
end_time BIGINT,
55
exit_code INTEGER,
66
status TEXT NOT NULL,
77
command TEXT NOT NULL,

crates/turborepo-db/src/lib.rs

Lines changed: 160 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
use async_graphql::SimpleObject;
12
use camino::Utf8Path;
2-
use sqlx::{Pool, Sqlite, SqlitePool};
3+
use sqlx::{sqlite::SqliteRow, Pool, Row, Sqlite, SqlitePool};
34
use thiserror::Error;
45
use turbopath::{AbsoluteSystemPath, AbsoluteSystemPathBuf};
5-
use turborepo_api_client::spaces::{CreateSpaceRunPayload, RunStatus, SpaceTaskSummary};
6+
use turborepo_api_client::spaces::{
7+
CreateSpaceRunPayload, RunStatus, SpaceTaskSummary, SpacesCacheStatus,
8+
};
69
use uuid::Uuid;
710

811
#[derive(Debug, Error)]
@@ -15,6 +18,24 @@ pub enum Error {
1518
Serialize(#[from] serde_json::Error),
1619
}
1720

21+
#[derive(Debug, Default, SimpleObject)]
22+
pub struct Run {
23+
id: String,
24+
start_time: i64,
25+
end_time: Option<i64>,
26+
exit_code: Option<u32>,
27+
status: String,
28+
command: String,
29+
package_inference_root: Option<String>,
30+
context: String,
31+
git_branch: Option<String>,
32+
git_sha: Option<String>,
33+
origination_user: String,
34+
client_id: String,
35+
client_name: String,
36+
client_version: String,
37+
}
38+
1839
#[derive(Clone)]
1940
pub struct DatabaseHandle {
2041
pool: Pool<Sqlite>,
@@ -34,6 +55,71 @@ impl DatabaseHandle {
3455
Ok(Self { pool })
3556
}
3657

58+
pub async fn get_runs(&self, limit: Option<u32>) -> Result<Vec<Run>, Error> {
59+
let query = if let Some(limit) = limit {
60+
sqlx::query(
61+
"SELECT id, start_time, end_time, exit_code, status, command, \
62+
package_inference_root, context, git_branch, git_sha, origination_user, \
63+
client_id, client_name, client_version FROM runs LIMIT ?",
64+
)
65+
.bind(limit)
66+
} else {
67+
sqlx::query(
68+
"SELECT id, start_time, end_time, exit_code, status, command, \
69+
package_inference_root, context, git_branch, git_sha, origination_user, \
70+
client_id, client_name, client_version FROM runs",
71+
)
72+
};
73+
74+
Ok(query
75+
.map(|row| Run {
76+
id: row.get("id"),
77+
start_time: row.get("start_time"),
78+
end_time: row.get("end_time"),
79+
exit_code: row.get("exit_code"),
80+
status: row.get("status"),
81+
command: row.get("command"),
82+
package_inference_root: row.get("package_inference_root"),
83+
context: row.get("context"),
84+
git_branch: row.get("git_branch"),
85+
git_sha: row.get("git_sha"),
86+
origination_user: row.get("origination_user"),
87+
client_id: row.get("client_id"),
88+
client_name: row.get("client_name"),
89+
client_version: row.get("client_version"),
90+
})
91+
.fetch_all(&self.pool)
92+
.await?)
93+
}
94+
95+
pub async fn get_tasks_for_run(&self, run_id: Uuid) -> Result<Vec<SpaceTaskSummary>, Error> {
96+
let query = sqlx::query(
97+
"SELECT key, name, workspace, hash, start_time, end_time, cache_status, exit_code, \
98+
logs FROM tasks WHERE run_id = ?",
99+
)
100+
.bind(run_id.to_string());
101+
Ok(query
102+
.map(|row: SqliteRow| SpaceTaskSummary {
103+
key: row.get("key"),
104+
name: row.get("name"),
105+
workspace: row.get("workspace"),
106+
hash: row.get("hash"),
107+
start_time: row.get("start_time"),
108+
end_time: row.get("end_time"),
109+
cache: SpacesCacheStatus {
110+
status: row.get("cache_status"),
111+
source: None,
112+
time_saved: row.get("time_saved"),
113+
},
114+
exit_code: row.get("exit_code"),
115+
dependencies: row.get("dependencies"),
116+
dependents: row.get("dependents"),
117+
logs: row.get("logs"),
118+
})
119+
.fetch_all(&self.pool)
120+
.await?)
121+
}
122+
37123
pub async fn create_run(&self, payload: &CreateSpaceRunPayload) -> Result<Uuid, Error> {
38124
let id = Uuid::new_v4();
39125
sqlx::query(
@@ -111,3 +197,75 @@ impl DatabaseHandle {
111197
Ok(())
112198
}
113199
}
200+
201+
#[cfg(test)]
202+
mod test {
203+
use turbopath::AbsoluteSystemPath;
204+
use turborepo_api_client::spaces::{
205+
CacheStatus, CreateSpaceRunPayload, RunStatus, SpaceClientSummary, SpaceRunType,
206+
SpaceTaskSummary, SpacesCacheStatus,
207+
};
208+
209+
use crate::DatabaseHandle;
210+
211+
#[tokio::test]
212+
async fn test_get_runs() -> Result<(), anyhow::Error> {
213+
let dir = tempfile::tempdir().unwrap();
214+
215+
let db = DatabaseHandle::new(
216+
dir.path().try_into()?,
217+
AbsoluteSystemPath::from_std_path(dir.path())?,
218+
)
219+
.await?;
220+
221+
let id = db
222+
.create_run(&CreateSpaceRunPayload {
223+
start_time: 0,
224+
status: RunStatus::Running,
225+
command: "test".to_string(),
226+
package_inference_root: "test".to_string(),
227+
run_context: "",
228+
git_branch: None,
229+
git_sha: None,
230+
ty: SpaceRunType::Turbo,
231+
user: "test".to_string(),
232+
client: SpaceClientSummary {
233+
id: "my-id",
234+
name: "turbo",
235+
version: "1.0.0".to_string(),
236+
},
237+
})
238+
.await
239+
.unwrap();
240+
let runs = db.get_runs(None).await.unwrap();
241+
assert_eq!(runs.len(), 1);
242+
assert_eq!(runs[0].id.len(), 36);
243+
assert_eq!(runs[0].git_sha, Some("test".to_string()));
244+
assert_eq!(runs[0].status, "RUNNING".to_string());
245+
246+
db.finish_task(
247+
id.clone(),
248+
&SpaceTaskSummary {
249+
key: "test#build".to_string(),
250+
name: "test".to_string(),
251+
workspace: "test".to_string(),
252+
hash: "test".to_string(),
253+
start_time: 0,
254+
end_time: 0,
255+
cache: SpacesCacheStatus {
256+
status: CacheStatus::Miss,
257+
source: None,
258+
time_saved: 0,
259+
},
260+
exit_code: Some(0),
261+
dependencies: vec![],
262+
dependents: vec![],
263+
logs: "".to_string(),
264+
},
265+
)
266+
.await
267+
.unwrap();
268+
269+
Ok(())
270+
}
271+
}

crates/turborepo-lib/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ turborepo-vercel-api-mock = { workspace = true }
3232
workspace = true
3333

3434
[dependencies]
35-
async-graphql = "7.0.7"
36-
async-graphql-axum = "7.0.7"
35+
async-graphql = { workspace = true }
36+
async-graphql-axum = { workspace = true }
3737
atty = { workspace = true }
3838
axum = { workspace = true }
3939
biome_deserialize = { workspace = true }

0 commit comments

Comments
 (0)