Work around sqlx relative path bug, now using env vars for db paths
This commit is contained in:
parent
77ad326fd2
commit
fb87a44d00
|
@ -128,6 +128,7 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"anyhow",
|
||||
"argon2",
|
||||
"dotenvy",
|
||||
"infer",
|
||||
"mime_guess",
|
||||
"rand",
|
||||
|
|
|
@ -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"
|
||||
|
|
26
src/lib.rs
26
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<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
|
||||
S: AsRef<str>,
|
||||
T: AsRef<str>,
|
||||
{
|
||||
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?;
|
||||
|
|
Loading…
Reference in New Issue