-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing persistence using spaces
- Loading branch information
1 parent
e50f9de
commit b682441
Showing
12 changed files
with
854 additions
and
71 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "turborepo-db" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
camino = { workspace = true } | ||
serde_json = { workspace = true } | ||
sqlx = { version = "0.8.1", features = ["runtime-tokio", "sqlite"] } | ||
thiserror = { workspace = true } | ||
turbopath = { workspace = true } | ||
turborepo-api-client = { workspace = true } | ||
uuid = { workspace = true, features = ["v4"] } | ||
|
||
[lints] | ||
workspace = true |
35 changes: 35 additions & 0 deletions
35
crates/turborepo-db/migrations/20240828183512_initial_tables.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
CREATE TABLE IF NOT EXISTS runs ( | ||
id TEXT PRIMARY KEY, -- primary key should be uuid | ||
start_time INTEGER NOT NULL, | ||
end_time INTEGER, | ||
exit_code INTEGER, | ||
status TEXT NOT NULL, | ||
command TEXT NOT NULL, | ||
package_inference_root TEXT, | ||
context TEXT NOT NULL, | ||
git_branch TEXT, | ||
git_sha TEXT, | ||
origination_user TEXT NOT NULL, | ||
client_id TEXT NOT NULL, | ||
client_name TEXT NOT NULL, | ||
client_version TEXT NOT NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS tasks ( | ||
id TEXT PRIMARY KEY, | ||
run_id TEXT NOT NULL, | ||
name TEXT NOT NULL, | ||
package TEXT NOT NULL, | ||
hash TEXT NOT NULL, | ||
start_time INTEGER NOT NULL, | ||
end_time INTEGER NOT NULL, | ||
cache_status TEXT NOT NULL, | ||
exit_code INTEGER, | ||
logs TEXT NOT NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS task_dependencies ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
task_id TEXT NOT NULL, | ||
dependency_id TEXT NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use camino::Utf8Path; | ||
use sqlx::{Pool, Sqlite, SqlitePool}; | ||
use thiserror::Error; | ||
use turbopath::{AbsoluteSystemPath, AbsoluteSystemPathBuf}; | ||
use turborepo_api_client::spaces::{CreateSpaceRunPayload, RunStatus, SpaceTaskSummary}; | ||
use uuid::Uuid; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum Error { | ||
#[error("failed to connect to database: {0}")] | ||
Sqlx(#[from] sqlx::Error), | ||
#[error("failed to migrate database: {0}")] | ||
Migrate(#[from] sqlx::migrate::MigrateError), | ||
#[error("failed to serialize")] | ||
Serialize(#[from] serde_json::Error), | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct DatabaseHandle { | ||
pool: Pool<Sqlite>, | ||
} | ||
|
||
impl DatabaseHandle { | ||
pub async fn new(cache_dir: &Utf8Path, repo_root: &AbsoluteSystemPath) -> Result<Self, Error> { | ||
let cache_dir = AbsoluteSystemPathBuf::from_unknown(&repo_root, &cache_dir); | ||
let pool = SqlitePool::connect(&format!( | ||
"sqlite://{}?mode=rwc", | ||
cache_dir.join_component("turbo.db") | ||
)) | ||
.await?; | ||
|
||
sqlx::migrate!().run(&pool).await?; | ||
|
||
Ok(Self { pool }) | ||
} | ||
|
||
pub async fn create_run(&self, payload: &CreateSpaceRunPayload) -> Result<Uuid, Error> { | ||
let id = Uuid::new_v4(); | ||
sqlx::query( | ||
"INSERT INTO runs ( | ||
id, | ||
start_time, | ||
status, | ||
command, | ||
package_inference_root, | ||
context, | ||
git_branch, | ||
git_sha, | ||
origination_user, | ||
client_id, | ||
client_name, | ||
client_version | ||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)", | ||
) | ||
.bind(id.to_string()) | ||
.bind(payload.start_time) | ||
.bind(payload.status.as_ref()) | ||
.bind(&payload.command) | ||
.bind(&payload.package_inference_root) | ||
.bind(payload.run_context) | ||
.bind(&payload.git_branch) | ||
.bind(&payload.git_sha) | ||
.bind(&payload.user) | ||
.bind(payload.client.id) | ||
.bind(payload.client.name) | ||
.bind(&payload.client.version) | ||
.execute(&self.pool) | ||
.await?; | ||
|
||
Ok(id) | ||
} | ||
|
||
pub async fn finish_run(&self, id: Uuid, end_time: i64, exit_code: i32) -> Result<(), Error> { | ||
sqlx::query("UPDATE runs SET status = $1, end_time = $2, exit_code = $3 WHERE id = $4") | ||
.bind(RunStatus::Completed.as_ref()) | ||
.bind(end_time) | ||
.bind(exit_code) | ||
.bind(id.to_string()) | ||
.execute(&self.pool) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn finish_task(&self, id: Uuid, summary: &SpaceTaskSummary) -> Result<(), Error> { | ||
sqlx::query( | ||
"INSERT INTO tasks ( | ||
run_id, | ||
name, | ||
package, | ||
hash, | ||
start_time, | ||
end_time, | ||
cache_status, | ||
exit_code, | ||
logs | ||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)", | ||
) | ||
.bind(id.to_string()) | ||
.bind(&summary.name) | ||
.bind(&summary.workspace) | ||
.bind(&summary.hash) | ||
.bind(summary.start_time) | ||
.bind(summary.end_time) | ||
.bind(serde_json::to_string(&summary.cache)?) | ||
.bind(summary.exit_code) | ||
.bind(&summary.logs) | ||
.execute(&self.pool) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.