Rework database fallback for v0.3 (#47)

Support migration from old database
This commit is contained in:
Cole Helbling 2020-03-29 13:42:51 -07:00 committed by Ajeet D'Souza
parent d4c8297f9b
commit 9af0251bd6
4 changed files with 32 additions and 17 deletions

View File

@ -116,9 +116,12 @@ NOTE: There is no PWD hook provided for POSIX shells.
### Environment variables
- `$_ZO_DATA`: sets the location of the database (default: `~/.zo`)
- `$_ZO_DATA_DIR`: directory where `zoxide` will store its data files (default:
platform-specific; see the [`dirs` documentation] for more information)
- `$_ZO_ECHO`: `z` will print the matched directory before navigating to it
- `$_ZO_EXCLUDE_DIRS`: list of directories separated by platform-specific
characters (`:` on Linux and macOS, and `;` on Windows) to be excluded from
the database
- `$_ZO_MAXAGE`: sets the maximum total rank after which entries start getting deleted
[`dirs` documentation]: https://docs.rs/dirs/latest/dirs/fn.data_local_dir.html

View File

@ -8,27 +8,26 @@ use std::fs;
use std::path::PathBuf;
pub const DB_MAX_SIZE: u64 = 8 * 1024 * 1024; // 8 MiB
pub const DB_VERSION: DBVersion = 3;
pub fn zo_data() -> Result<PathBuf> {
let path = match env::var_os("_ZO_DATA") {
pub fn zo_data_dir() -> Result<PathBuf> {
let data_dir = match env::var_os("_ZO_DATA_DIR") {
Some(data_osstr) => PathBuf::from(data_osstr),
None => {
if let Some(mut cache_dir) = dirs::cache_dir() {
cache_dir.push("zoxide");
cache_dir
} else if let Some(mut home_dir) = dirs::home_dir() {
home_dir.push(".zoxide");
home_dir
if let Some(mut data_dir) = dirs::data_local_dir() {
data_dir.push("zoxide");
data_dir
} else {
bail!("could not generate default directory, please set _ZO_DATA manually");
bail!("could not find database directory, please set _ZO_DATA_DIR manually");
}
}
};
fs::create_dir_all(&path).context("could not create _ZO_DATA directory")?;
Ok(path)
// This will fail when `data_dir` points to a file or a broken symlink, but
// will no-op on a valid symlink (to a directory), or an actual directory.
fs::create_dir_all(&data_dir).context("could not create data directory")?;
Ok(data_dir)
}
pub fn zo_exclude_dirs() -> Vec<PathBuf> {
@ -45,6 +44,7 @@ pub fn zo_maxage() -> Result<Rank> {
let maxage = maxage_str
.parse::<i64>()
.context("unable to parse _ZO_MAXAGE as integer")?;
Ok(maxage as Rank)
}
None => bail!("invalid Unicode in _ZO_MAXAGE"),

View File

@ -48,7 +48,7 @@ impl DB {
})
}
pub fn open_old<P1, P2>(path_old: P1, path: P2) -> Result<DB>
pub fn open_and_migrate<P1, P2>(path_old: P1, path: P2) -> Result<DB>
where
P1: AsRef<Path>,
P2: AsRef<Path>,

View File

@ -22,9 +22,21 @@ pub fn path_to_bytes<P: AsRef<Path>>(path: &P) -> Option<&[u8]> {
}
pub fn get_db() -> Result<DB> {
let mut path = config::zo_data()?;
path.push("db.zo");
DB::open(path)
let mut db_path = config::zo_data_dir()?;
db_path.push("db.zo");
// FIXME: fallback to old database location; remove in next breaking version
if !db_path.is_file() {
if let Some(mut old_db_path) = dirs::home_dir() {
old_db_path.push(".zo");
if old_db_path.is_file() {
return DB::open_and_migrate(old_db_path, db_path);
}
}
}
DB::open(db_path)
}
pub fn get_current_time() -> Result<Epoch> {