Fix dataset file getting cleared on startup

This commit is contained in:
Kiana Sheibani 2023-09-26 21:35:14 -04:00
parent 5d0fbcd107
commit da6b4a9220
Signed by: toki
GPG key ID: 6CB106C25E86A9F7

View file

@ -1,20 +1,25 @@
use crate::queries::*; use crate::queries::*;
use sqlite::*; use sqlite::*;
use std::fs::{self, File}; use std::fs::{self, OpenOptions};
use std::io;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
/// Return the path to a dataset. /// Return the path to a dataset.
pub fn dataset_path(config_dir: &Path, dataset: &str) -> PathBuf { pub fn dataset_path(config_dir: &Path, dataset: &str) -> io::Result<PathBuf> {
// $config_dir/datasets/$dataset.sqlite // $config_dir/datasets/$dataset.sqlite
let mut path = config_dir.to_owned(); let mut path = config_dir.to_owned();
path.push("datasets"); path.push("datasets");
// Create datasets path if it doesn't exist // Create datasets path if it doesn't exist
fs::create_dir_all(&path).unwrap(); fs::create_dir_all(&path)?;
path.push(dataset); path.push(dataset);
path.set_extension("db"); path.set_extension("db");
path
// Create dataset file if it doesn't exist
OpenOptions::new().write(true).create(true).open(&path)?;
Ok(path)
} }
pub fn open_dataset(dataset: &Path) -> sqlite::Result<Connection> { pub fn open_dataset(dataset: &Path) -> sqlite::Result<Connection> {
@ -27,13 +32,6 @@ pub fn open_dataset(dataset: &Path) -> sqlite::Result<Connection> {
) STRICT; ) STRICT;
"; ";
File::create(dataset).map_err(|e| Error {
code: {
println!("{:?}", e);
None
},
message: Some("unable to open database file".to_owned()),
})?;
let connection = sqlite::open(dataset)?; let connection = sqlite::open(dataset)?;
connection.execute(query)?; connection.execute(query)?;
Ok(connection) Ok(connection)