From fb87a44d00eeaac944631652a61b501e57f7b4bf Mon Sep 17 00:00:00 2001 From: august kline Date: Thu, 14 Nov 2024 12:06:44 -0500 Subject: [PATCH] Work around sqlx relative path bug, now using env vars for db paths --- Cargo.lock | 1 + Cargo.toml | 1 + src/lib.rs | 26 ++++++++++++-------------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ad503fd..fbae54d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -128,6 +128,7 @@ version = "0.1.0" dependencies = [ "anyhow", "argon2", + "dotenvy", "infer", "mime_guess", "rand", diff --git a/Cargo.toml b/Cargo.toml index d545cda..baffb63 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" [dependencies] anyhow = "1.0.89" argon2 = { version = "0.5.3", features = ["std"] } +dotenvy = "0.15.7" infer = "0.16.0" mime_guess = "2.0.5" rand = "0.8.5" diff --git a/src/lib.rs b/src/lib.rs index ccc2493..94aef84 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,7 @@ use sqlx::{ // #[cfg(not(debug_assertions))] // const CACHE_URL: &str = "sqlite://:memory:"; // #[cfg(debug_assertions)] -const CACHE_URL: &str = "sqlite://cache.db"; +const CACHE_URL: &str = "cache.db"; /// A Sqlite database for all blog data. /// @@ -34,35 +34,33 @@ pub struct BlogDb { impl BlogDb { /// Create a new BlogDb with an initial user. - pub async fn new(username: S, password: String, db_url: T) -> anyhow::Result + pub async fn new(username: S, password: String) -> anyhow::Result where S: AsRef, T: AsRef, { - let db_name = ["./", db_url.as_ref().strip_prefix("sqlite://").unwrap()].concat(); - if File::open(&db_name).is_err() { - let _ = File::create_new(db_name); + let main_db_name = dotenvy::var("DB_MAIN_PATH")?; + let cache_name = dotenvy::var("DB_CACHE_PATH")?; + if File::open(&main_db_name).is_err() { + File::create_new(&main_db_name)?; + } + if File::open(&cache_name).is_err() { + File::create_new(&cache_name)?; } let db = SqlitePoolOptions::new() .connect_with( - SqliteConnectOptions::from_str(db_url.as_ref())? + SqliteConnectOptions::from_str(&["sqlite:///", &main_db_name].concat())? .journal_mode(SqliteJournalMode::Wal), ) .await?; - // #[cfg(debug_assertions)] - // { - let db_name = ["./", CACHE_URL.strip_prefix("sqlite://").unwrap()].concat(); - if File::open(&db_name).is_err() { - let _ = File::create_new(db_name); - } - // } let memory = SqlitePoolOptions::new() .min_connections(1) .connect_with( // #[cfg(not(debug_assertions))] // SqliteConnectOptions::from_str(MEMORY_URL)?.journal_mode(SqliteJournalMode::Memory), // #[cfg(debug_assertions)] - SqliteConnectOptions::from_str(CACHE_URL)?.journal_mode(SqliteJournalMode::Wal), + SqliteConnectOptions::from_str(&["sqlite:///", &cache_name].concat())? + .journal_mode(SqliteJournalMode::Wal), ) .await?; Self::run_main_migrations(&db).await?;