feat: add initial db funcs
This commit is contained in:
parent
4b88dfde4b
commit
05a4e24483
83
src/db.rs
Normal file
83
src/db.rs
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
use sqlx::{SqlitePool, migrate::Migrator};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::script::Script;
|
||||||
|
|
||||||
|
static MIGRATOR: Migrator = sqlx::migrate!("./migrations");
|
||||||
|
|
||||||
|
// TODO: Add Custom Error Type
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub async fn init_db(pool: &SqlitePool) -> Result<(), String> {
|
||||||
|
MIGRATOR.run(pool).await.map_err(|e| e.to_string())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Write the whole Script into the DB
|
||||||
|
// TODO: Add Custom Error type
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub async fn save_script(pool: &SqlitePool, script: &Script) -> Result<(), String> {
|
||||||
|
let id = Uuid::new_v4().to_string();
|
||||||
|
let name = script.name();
|
||||||
|
let path = script.path();
|
||||||
|
sqlx::query!(
|
||||||
|
r#"INSERT INTO scripts (id, name, path) VALUES (?, ?, ?)"#,
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
path,
|
||||||
|
)
|
||||||
|
.execute(pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Return Vec<Script> instead of String-Tuple
|
||||||
|
// TODO: Add Custom Error Type
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub async fn list_scripts(pool: &SqlitePool) -> Result<Vec<(String, String)>, String> {
|
||||||
|
struct Payload {
|
||||||
|
pub name: String,
|
||||||
|
pub path: String,
|
||||||
|
}
|
||||||
|
sqlx::query_as!(Payload, r#"SELECT name, path FROM scripts"#)
|
||||||
|
.fetch_all(pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| e.to_string())
|
||||||
|
.map(|v| {
|
||||||
|
Ok(v.into_iter()
|
||||||
|
.map(|i| (i.name, i.path))
|
||||||
|
.collect::<Vec<(String, String)>>())
|
||||||
|
})?
|
||||||
|
}
|
||||||
|
|
||||||
|
mod test {
|
||||||
|
#![allow(unused_imports, clippy::unwrap_used, clippy::expect_used)]
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_insert_and_list() {
|
||||||
|
let db_name = Uuid::new_v4().to_string();
|
||||||
|
let db_path = format!("./{db_name}");
|
||||||
|
let db_url = format!("sqlite://{db_name}");
|
||||||
|
|
||||||
|
std::fs::File::create_new(&db_path).unwrap();
|
||||||
|
|
||||||
|
let pool = SqlitePool::connect(&db_url).await.unwrap();
|
||||||
|
|
||||||
|
assert!(init_db(&pool).await.is_ok());
|
||||||
|
|
||||||
|
let script = Script::from_file("powershell/test-script.ps1").unwrap();
|
||||||
|
|
||||||
|
assert!(save_script(&pool, &script).await.is_ok());
|
||||||
|
|
||||||
|
let want = (script.name(), script.path());
|
||||||
|
|
||||||
|
let got = list_scripts(&pool).await.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(got.len(), 1);
|
||||||
|
assert_eq!(got[0].0, want.0);
|
||||||
|
assert_eq!(got[0].1, want.1);
|
||||||
|
std::fs::remove_file(&db_path).unwrap();
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1,2 @@
|
|||||||
|
mod db;
|
||||||
mod script;
|
mod script;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user