From 8de10a23eba9cb07a98e5988f8332fbd8fa3be6a Mon Sep 17 00:00:00 2001 From: WillKirkmanM Date: Wed, 11 Sep 2024 23:38:06 +0100 Subject: [PATCH] Fix(database): Ensure Database Folder is Created in Docker --- crates/backend/src/utils/database/database.rs | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/crates/backend/src/utils/database/database.rs b/crates/backend/src/utils/database/database.rs index ab2f5c1e..37b9589e 100644 --- a/crates/backend/src/utils/database/database.rs +++ b/crates/backend/src/utils/database/database.rs @@ -1,5 +1,5 @@ use std::fs; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::sync::Arc; use std::time::Duration; @@ -12,6 +12,8 @@ use dotenvy::dotenv; use diesel::sqlite::SqliteConnection; use std::error::Error; +use crate::utils::config::is_docker; + #[derive(Debug)] pub struct ConnectionOptions { pub enable_wal: bool, @@ -89,18 +91,30 @@ pub fn migrations_ran() -> bool { } pub fn get_database_path() -> PathBuf { - let mut path = dirs::data_local_dir().unwrap_or_else(|| PathBuf::from(".")); - path.push("ParsonLabs"); - path.push("Music"); - path.push("Database"); - if let Err(e) = fs::create_dir_all(&path) { - eprintln!("Failed to create directories: {}", e); + let path = if is_docker() { + Path::new("/ParsonLabsMusic/Database").to_path_buf() + } else { + let mut path = dirs::data_local_dir().unwrap_or_else(|| PathBuf::from(".")); + path.push("ParsonLabs"); + path.push("Music"); + path.push("Database"); + path + }; + + if let Some(parent) = path.parent() { + if let Err(e) = fs::create_dir_all(parent) { + eprintln!("Failed to create directories: {}", e); + } } - path.push("music.db"); - if !path.exists() { - if let Err(e) = fs::File::create(&path) { + + let mut db_path = path.clone(); + db_path.push("music.db"); + + if !db_path.exists() { + if let Err(e) = fs::File::create(&db_path) { eprintln!("Failed to create database file: {}", e); } } - path + + db_path } \ No newline at end of file