Work around sqlx relative path bug, now using env vars for db paths

This commit is contained in:
august kline 2024-11-14 12:06:44 -05:00
parent 77ad326fd2
commit fb87a44d00
3 changed files with 14 additions and 14 deletions

1
Cargo.lock generated
View File

@ -128,6 +128,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"argon2", "argon2",
"dotenvy",
"infer", "infer",
"mime_guess", "mime_guess",
"rand", "rand",

View File

@ -6,6 +6,7 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.89" anyhow = "1.0.89"
argon2 = { version = "0.5.3", features = ["std"] } argon2 = { version = "0.5.3", features = ["std"] }
dotenvy = "0.15.7"
infer = "0.16.0" infer = "0.16.0"
mime_guess = "2.0.5" mime_guess = "2.0.5"
rand = "0.8.5" rand = "0.8.5"

View File

@ -20,7 +20,7 @@ use sqlx::{
// #[cfg(not(debug_assertions))] // #[cfg(not(debug_assertions))]
// const CACHE_URL: &str = "sqlite://:memory:"; // const CACHE_URL: &str = "sqlite://:memory:";
// #[cfg(debug_assertions)] // #[cfg(debug_assertions)]
const CACHE_URL: &str = "sqlite://cache.db"; const CACHE_URL: &str = "cache.db";
/// A Sqlite database for all blog data. /// A Sqlite database for all blog data.
/// ///
@ -34,35 +34,33 @@ pub struct BlogDb {
impl BlogDb { impl BlogDb {
/// Create a new BlogDb with an initial user. /// Create a new BlogDb with an initial user.
pub async fn new<S, T>(username: S, password: String, db_url: T) -> anyhow::Result<Self> pub async fn new<S, T>(username: S, password: String) -> anyhow::Result<Self>
where where
S: AsRef<str>, S: AsRef<str>,
T: AsRef<str>, T: AsRef<str>,
{ {
let db_name = ["./", db_url.as_ref().strip_prefix("sqlite://").unwrap()].concat(); let main_db_name = dotenvy::var("DB_MAIN_PATH")?;
if File::open(&db_name).is_err() { let cache_name = dotenvy::var("DB_CACHE_PATH")?;
let _ = File::create_new(db_name); 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() let db = SqlitePoolOptions::new()
.connect_with( .connect_with(
SqliteConnectOptions::from_str(db_url.as_ref())? SqliteConnectOptions::from_str(&["sqlite:///", &main_db_name].concat())?
.journal_mode(SqliteJournalMode::Wal), .journal_mode(SqliteJournalMode::Wal),
) )
.await?; .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() let memory = SqlitePoolOptions::new()
.min_connections(1) .min_connections(1)
.connect_with( .connect_with(
// #[cfg(not(debug_assertions))] // #[cfg(not(debug_assertions))]
// SqliteConnectOptions::from_str(MEMORY_URL)?.journal_mode(SqliteJournalMode::Memory), // SqliteConnectOptions::from_str(MEMORY_URL)?.journal_mode(SqliteJournalMode::Memory),
// #[cfg(debug_assertions)] // #[cfg(debug_assertions)]
SqliteConnectOptions::from_str(CACHE_URL)?.journal_mode(SqliteJournalMode::Wal), SqliteConnectOptions::from_str(&["sqlite:///", &cache_name].concat())?
.journal_mode(SqliteJournalMode::Wal),
) )
.await?; .await?;
Self::run_main_migrations(&db).await?; Self::run_main_migrations(&db).await?;