chore: fix vault/db path semantics, previews in db path, `--clean` param

feat/type-attributes
Tomáš Mládek 2022-09-16 16:49:25 +02:00
parent 5704be7975
commit d671640c04
5 changed files with 41 additions and 43 deletions

View File

@ -320,7 +320,7 @@ mod tests {
fn test_path_manipulation() {
// Initialize database
let temp_dir = TempDir::new().unwrap();
let open_result = UpEndDatabase::open(&temp_dir, None, true).unwrap();
let open_result = UpEndDatabase::open(&temp_dir, true).unwrap();
let connection = open_result.db.connection().unwrap();
let foo_result = fetch_or_create_dir(&connection, None, UNode("foo".to_string()), true);

View File

@ -77,23 +77,19 @@ pub struct OpenResult {
}
pub struct UpEndDatabase {
pub path: PathBuf,
pool: Arc<DbPool>,
lock: Arc<RwLock<()>>,
vault_path: Arc<PathBuf>
}
pub const UPEND_SUBDIR: &str = ".upend";
pub const DATABASE_FILENAME: &str = "upend.sqlite3";
impl UpEndDatabase {
pub fn open<P: AsRef<Path>>(
dirpath: P,
db_path: Option<PathBuf>,
reinitialize: bool,
) -> Result<OpenResult> {
pub fn open<P: AsRef<Path>>(dirpath: P, reinitialize: bool) -> Result<OpenResult> {
embed_migrations!("./migrations/upend/");
let upend_path = db_path.unwrap_or_else(|| dirpath.as_ref().join(UPEND_SUBDIR));
let upend_path = dirpath.as_ref().join(UPEND_SUBDIR);
if reinitialize {
debug!("Reinitializing - removing previous database...");
@ -119,9 +115,9 @@ impl UpEndDatabase {
trace!("Pool created.");
let db = UpEndDatabase {
path: upend_path,
pool: Arc::new(pool),
lock: Arc::new(RwLock::new(())),
vault_path: Arc::new(dirpath.as_ref().canonicalize()?),
};
let connection = db.connection().unwrap();
@ -380,17 +376,17 @@ mod test {
fn test_open() {
let tempdir = TempDir::new().unwrap();
let result = UpEndDatabase::open(&tempdir, None, false);
let result = UpEndDatabase::open(&tempdir, false);
assert!(result.is_ok());
assert!(result.unwrap().new);
// Not new
let result = UpEndDatabase::open(&tempdir, None, false);
let result = UpEndDatabase::open(&tempdir, false);
assert!(result.is_ok());
assert!(!result.unwrap().new);
// reinitialize true, new again
let result = UpEndDatabase::open(&tempdir, None, true);
let result = UpEndDatabase::open(&tempdir, true);
assert!(result.is_ok());
assert!(result.unwrap().new);
}
@ -398,7 +394,7 @@ mod test {
#[test]
fn test_query() {
let tempdir = TempDir::new().unwrap();
let result = UpEndDatabase::open(&tempdir, None, false).unwrap();
let result = UpEndDatabase::open(&tempdir, false).unwrap();
let db = result.db;
let connection = db.connection().unwrap();

View File

@ -159,8 +159,8 @@ impl FsStore {
// Walk through the vault, find all paths
debug!("Traversing vault directory");
let absolute_dir_path = fs::canonicalize(&*db.vault_path)?;
let path_entries: Vec<PathBuf> = WalkDir::new(&*db.vault_path)
let absolute_dir_path = fs::canonicalize(&*self.path)?;
let path_entries: Vec<PathBuf> = WalkDir::new(&*self.path)
.follow_links(true)
.into_iter()
.filter_map(|e| e.ok())
@ -266,7 +266,7 @@ impl FsStore {
info!(
"Finished updating {:?} ({} created, {} deleted, {} left unchanged). Took {}s.",
db.vault_path,
self.path,
created,
deleted,
unchanged,
@ -780,7 +780,7 @@ mod test {
File::create(file_path).unwrap();
// Initialize database
let open_result = UpEndDatabase::open(&temp_dir, None, true).unwrap();
let open_result = UpEndDatabase::open(&temp_dir, true).unwrap();
let store = FsStore::from_path(&temp_dir).unwrap();
let job_container = JobContainer::new();
@ -817,7 +817,7 @@ mod test {
File::create(file_path).unwrap();
// Initialize database
let open_result = UpEndDatabase::open(&temp_dir, None, true).unwrap();
let open_result = UpEndDatabase::open(&temp_dir, true).unwrap();
let store = FsStore::from_path(&temp_dir).unwrap();
let mut job_container = JobContainer::new();

View File

@ -95,7 +95,7 @@ mod test {
#[test]
fn test_extract() -> Result<()> {
let temp_dir = TempDir::new().unwrap();
let open_result = crate::database::UpEndDatabase::open(&temp_dir, None, true)?;
let open_result = crate::database::UpEndDatabase::open(&temp_dir, true)?;
let connection = open_result.db.connection()?;
let store =
Arc::new(Box::new(FsStore::from_path(&temp_dir)?) as Box<dyn UpStore + Sync + Send>);

View File

@ -12,7 +12,7 @@ use actix_cors::Cors;
use actix_web::{middleware, App, HttpServer};
use anyhow::Result;
use clap::{App as ClapApp, Arg};
use log::{info, warn};
use log::{debug, info, warn};
use rand::{thread_rng, Rng};
use std::sync::Arc;
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
@ -55,10 +55,10 @@ fn main() -> Result<()> {
.required(true),
)
.arg(
Arg::with_name("DB_PATH")
.long("db-path")
Arg::with_name("STORE_PATH")
.long("store")
.takes_value(true)
.help(r#"path to sqlite db file ("$VAULT_PATH/.upend" by default)"#),
.help(r#"path to store ($VAULT_PATH by default)"#),
)
.arg(
Arg::with_name("NO_BROWSER")
@ -120,17 +120,19 @@ fn main() -> Result<()> {
let vault_path = PathBuf::from(matches.value_of("DIRECTORY").unwrap());
let open_result = UpEndDatabase::open(
&vault_path,
matches.value_of("DB_PATH").map(PathBuf::from),
matches.is_present("REINITIALIZE"),
)
.expect("failed to open database!");
let open_result = UpEndDatabase::open(&vault_path, matches.is_present("REINITIALIZE"))
.expect("failed to open database!");
let upend = Arc::new(open_result.db);
let store =
Arc::new(Box::new(FsStore::from_path(vault_path.clone()).unwrap())
as Box<dyn UpStore + Send + Sync>);
let store = Arc::new(Box::new(
FsStore::from_path(
matches
.value_of("STORE_PATH")
.map(PathBuf::from)
.unwrap_or_else(|| vault_path.clone()),
)
.unwrap(),
) as Box<dyn UpStore + Send + Sync>);
let ui_path = get_static_dir("webui");
if ui_path.is_err() {
@ -143,22 +145,22 @@ fn main() -> Result<()> {
let ui_enabled = ui_path.is_ok() && !matches.is_present("NO_UI");
let browser_enabled = desktop_enabled && !matches.is_present("NO_BROWSER");
let preview_dir = tempfile::tempdir().unwrap();
let preview_path = upend.path.join("previews");
#[cfg(feature = "previews")]
let preview_store = Some(Arc::new(crate::previews::PreviewStore::new(
preview_dir.path(),
preview_path.clone(),
store.clone(),
)));
// if matches.is_present("CLEAN") {
// info!("Cleaning temporary directories...");
// if preview_path.exists() {
// std::fs::remove_dir_all(&preview_path).unwrap();
// debug!("Removed {preview_path:?}");
// } else {
// debug!("No preview path exists, continuing...");
// }
// }
if matches.is_present("CLEAN") {
info!("Cleaning temporary directories...");
if preview_path.exists() {
std::fs::remove_dir_all(&preview_path).unwrap();
debug!("Removed {preview_path:?}");
} else {
debug!("No preview path exists, continuing...");
}
}
#[cfg(not(feature = "previews"))]
let preview_store = None;