Compare commits
3 Commits
853b04e46d
...
05a4e24483
Author | SHA1 | Date | |
---|---|---|---|
05a4e24483 | |||
4b88dfde4b | |||
ae29e83c80 |
@ -8,3 +8,6 @@ repository = "https://git.itsscb.de/itsscb/BefehlsWerk"
|
||||
[dependencies]
|
||||
serde = { version = "1.0.219", features = ["derive", "rc"] }
|
||||
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"] }
|
||||
|
BIN
db_dev.db
Normal file
BIN
db_dev.db
Normal file
Binary file not shown.
@ -13,6 +13,10 @@
|
||||
buildInputs = with pkgs; [
|
||||
powershell
|
||||
];
|
||||
|
||||
shellHook = ''
|
||||
export DATABASE_URL=sqlite://db_dev.db
|
||||
'';
|
||||
};
|
||||
}
|
||||
);
|
||||
|
5
migrations/20250503193112_init.sql
Normal file
5
migrations/20250503193112_init.sql
Normal file
@ -0,0 +1,5 @@
|
||||
CREATE TABLE IF NOT EXISTS scripts (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
path TEXT NOT NULL
|
||||
)
|
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user