Refactor some of the work with Project to create them from the config

Forgot to commit this earlier before wrapping up the stream
This commit is contained in:
R Tyler Croy 2023-01-29 20:50:59 -08:00
parent c4df9ad3af
commit fcdf259bb9
No known key found for this signature in database
GPG Key ID: E5C92681BEF6CEA2
2 changed files with 56 additions and 12 deletions

View File

@ -3,11 +3,12 @@
*/
use chrono::{DateTime, NaiveDateTime, Utc};
use sqlx::{SqlitePool};
use sqlx::sqlite::SqliteQueryResult;
use sqlx::{Sqlite, SqlitePool, Transaction};
use uuid::Uuid;
#[derive(Clone, Debug)]
struct Project {
pub struct Project {
uuid: String,
name: String,
created_at: NaiveDateTime,
@ -23,6 +24,36 @@ impl Default for Project {
}
}
impl Project {
pub fn new(name: &str) -> Self {
Self {
uuid: Uuid::new_v4().hyphenated().to_string(),
name: name.into(),
created_at: Utc::now().naive_utc(),
}
}
pub async fn by_name(name: &str, pool: &SqlitePool) -> Result<Project, sqlx::Error> {
sqlx::query_as!(Project, "SELECT * FROM projects WHERE name = ?", name)
.fetch_one(pool)
.await
}
pub async fn create(
project: &Project,
tx: &SqlitePool,
) -> Result<SqliteQueryResult, sqlx::Error> {
sqlx::query!(
r#"INSERT INTO projects (uuid, name, created_at) VALUES (?, ?, ?)"#,
project.uuid,
project.name,
project.created_at,
)
.execute(tx)
.await
}
}
#[derive(Clone, Debug)]
struct Run {
run: RunRow,
@ -38,7 +69,6 @@ impl Run {
*/
async fn create(run: &Run, pool: &SqlitePool) -> Result<(), sqlx::Error> {
let mut tx = pool.begin().await?;
sqlx::query!(
r#"INSERT INTO scm_info (uuid, git_url, ref, created_at) VALUES (?, ?, ?, ?)"#,
run.scm_info.uuid,
@ -49,13 +79,6 @@ impl Run {
.execute(&mut tx)
.await?;
sqlx::query!(
r#"INSERT INTO projects (uuid, name, created_at) VALUES (?, ?, ?)"#,
run.project.uuid,
run.project.name,
run.project.created_at,
).execute(&mut tx).await?;
sqlx::query!(
r#"INSERT INTO run_definition (uuid, definition, created_at) VALUES (?, ?, ?)"#,
run.definition.uuid,
@ -226,7 +249,11 @@ mod tests {
async fn test_create_a_run() {
pretty_env_logger::try_init();
let pool = setup_database().await;
let run = Run::default();
let project = Project::new("test");
Project::create(&project, &pool).await.unwrap();
let mut run = Run::default();
run.project = project;
let result = Run::create(&run, &pool).await.unwrap();
let fetched_run = Run::find_by(&run.run.uuid, &pool).await.unwrap();
assert_eq!(run.run.uuid, fetched_run.run.uuid);

View File

@ -169,7 +169,24 @@ async fn main() -> Result<(), tide::Error> {
if database_url == ":memory:" {
sqlx::migrate!().run(&pool).await?;
}
let mut state = AppState::new(pool, config.clone());
let mut state = AppState::new(pool.clone(), config.clone());
/*
* Make sure the database has all the projects configured
*/
for name in config.projects.keys() {
match dao::Project::by_name(&name, &pool).await {
Ok(_) => {}
Err(sqlx::Error::RowNotFound) => {
debug!("Project not found in database, creating: {}", name);
dao::Project::create(&dao::Project::new(&name), &pool).await?;
}
Err(e) => {
return Err(e.into());
}
}
}
for url in &config.agents {
debug!("Requesting capabilities from agent: {}", url);