Move directories rather than keys for Publication Server migration.

This commit is contained in:
Tim Bruijnzeels
2021-05-12 11:26:23 +02:00
parent cbf0eb0abe
commit e32463af31
2 changed files with 21 additions and 4 deletions
+20 -1
View File
@@ -176,6 +176,13 @@ impl KeyValueStore {
}
}
/// Archives the content of a scope to sub-scope in that scope
pub fn scope_archive(&self, scope: &str, sub_scope: &str) -> Result<(), KeyValueError> {
match self {
KeyValueStore::Disk(disk_store) => disk_store.scope_archive(scope, sub_scope),
}
}
/// Returns whether a scope exists
pub fn has_scope(&self, scope: String) -> Result<bool, KeyValueError> {
match self {
@@ -237,7 +244,7 @@ impl KeyValueStoreDiskImpl {
path
}
fn scope_path(&self, scope: Option<&String>) -> PathBuf {
fn scope_path<P: AsRef<Path>>(&self, scope: Option<P>) -> PathBuf {
let mut path = self.base.clone();
if let Some(scope) = scope {
path.push(scope);
@@ -331,6 +338,18 @@ impl KeyValueStoreDiskImpl {
Self::read_dir(&self.base, false, true)
}
fn scope_archive(&self, scope: &str, sub_scope: &str) -> Result<(), KeyValueError> {
let scope_path = self.scope_path(Some(scope));
let tmp_path = self.scope_path(Some(format!(".{}", scope)));
let end_path = self.scope_path(Some(format!("{}/{}", scope, sub_scope)));
fs::rename(&scope_path, &tmp_path)?;
fs::create_dir_all(&scope_path)?;
fs::rename(&tmp_path, &end_path)?;
Ok(())
}
fn keys(&self, scope: Option<String>, matching: &str) -> Result<Vec<KeyStoreKey>, KeyValueError> {
let path = self.scope_path(scope.as_ref());
@@ -113,9 +113,7 @@ impl UpgradeStore for PubdStoreMigration {
// Archive all keys in the scope, then we can write new keys as needed without
// overwriting anything when we renumber.
for key in self.store.keys(Some(scope.to_string()), "")? {
self.archive_to_migration_scope(&key)?;
}
self.store.scope_archive(scope, MIGRATION_SCOPE)?;
let migration_scope = format!("{}/{}", scope, MIGRATION_SCOPE);