Compare commits
No commits in common. "05a4e24483d15347630c495980cb9563de7acae7" and "853b04e46ddbc2f5902b4dc54063906ecc2f17c9" have entirely different histories.
05a4e24483
...
853b04e46d
@ -8,6 +8,3 @@ repository = "https://git.itsscb.de/itsscb/BefehlsWerk"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
serde = { version = "1.0.219", features = ["derive", "rc"] }
|
serde = { version = "1.0.219", features = ["derive", "rc"] }
|
||||||
serde_json = "1.0.140"
|
serde_json = "1.0.140"
|
||||||
sqlx = { version = "0.8.5", features = ["macros", "runtime-tokio", "sqlite", "uuid"] }
|
|
||||||
tokio = { version = "1.44.2", features = ["full"] }
|
|
||||||
uuid = { version = "1.16.0", features = ["v4"] }
|
|
||||||
|
@ -13,10 +13,6 @@
|
|||||||
buildInputs = with pkgs; [
|
buildInputs = with pkgs; [
|
||||||
powershell
|
powershell
|
||||||
];
|
];
|
||||||
|
|
||||||
shellHook = ''
|
|
||||||
export DATABASE_URL=sqlite://db_dev.db
|
|
||||||
'';
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
CREATE TABLE IF NOT EXISTS scripts (
|
|
||||||
id TEXT PRIMARY KEY,
|
|
||||||
name TEXT NOT NULL,
|
|
||||||
path TEXT NOT NULL
|
|
||||||
)
|
|
83
src/db.rs
83
src/db.rs
@ -1,83 +0,0 @@
|
|||||||
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,2 +1 @@
|
|||||||
mod db;
|
|
||||||
mod script;
|
mod script;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user