fixing database directory error
This commit is contained in:
parent
d1c8d66cca
commit
79464d3bfb
1 changed files with 9 additions and 2 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
use std::{env, path::Path};
|
use std::{env, fs, path::Path};
|
||||||
|
|
||||||
// Using SQLite
|
// Using SQLite
|
||||||
use rusqlite::{Connection, OpenFlags, Result};
|
use rusqlite::{Connection, OpenFlags, Result};
|
||||||
|
|
@ -7,7 +7,14 @@ pub fn init_db() -> Result<Connection, rusqlite::Error> {
|
||||||
let filename = env::var("DATABASE_PATH").unwrap_or_else(|_| "bot_data.sqlite".to_string());
|
let filename = env::var("DATABASE_PATH").unwrap_or_else(|_| "bot_data.sqlite".to_string());
|
||||||
let filepath = Path::new(&filename);
|
let filepath = Path::new(&filename);
|
||||||
|
|
||||||
let open_flags = OpenFlags::default();
|
if let Some(parent) = filepath.parent() {
|
||||||
|
if !parent.as_os_str().is_empty() {
|
||||||
|
fs::create_dir_all(parent).expect("Failed to create database directory");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let open_flags =
|
||||||
|
OpenFlags::default() | OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_READ_WRITE;
|
||||||
|
|
||||||
Connection::open_with_flags(filepath, open_flags)
|
Connection::open_with_flags(filepath, open_flags)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue