Skip to content

Commit

Permalink
Add migration for #529
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Nov 2, 2022
1 parent bff7bac commit 8beb636
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Db {
pub fn init(path: &std::path::Path, server_url: String) -> AtomicResult<Db> {
let db = sled::open(path).map_err(|e|format!("Failed opening DB at this location: {:?} . Is another instance of Atomic Server running? {}", path, e))?;
let resources = db.open_tree("resources_v1").map_err(|e|format!("Failed building resources. Your DB might be corrupt. Go back to a previous version and export your data. {}", e))?;
let reference_index = db.open_tree("reference_index")?;
let reference_index = db.open_tree("reference_index_v1")?;
let query_index = db.open_tree("members_index")?;
let prop_val_sub_index = db.open_tree("prop_val_sub_index")?;
let watched_queries = db.open_tree("watched_queries")?;
Expand Down
12 changes: 11 additions & 1 deletion lib/src/db/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ Therefore, we need migrations to convert the old schema to the new one.
- Update the Tree key used in [Db::init]
*/

use crate::{errors::AtomicResult, Db};
use crate::{errors::AtomicResult, Db, Storelike};

/// Checks the current version(s) of the internal Store, and performs migrations if needed.
pub fn migrate_maybe(store: &Db) -> AtomicResult<()> {
for tree in store.db.tree_names() {
match String::from_utf8_lossy(&tree).as_ref() {
// Add migrations for outdated Trees to this list
"resources" => v0_to_v1(store)?,
"reference_index" => ref_v0_to_v1(store)?,
_other => {}
}
}
Expand Down Expand Up @@ -71,3 +72,12 @@ fn v0_to_v1(store: &Db) -> AtomicResult<()> {
tracing::warn!("Finished migration of {} resources", count);
Ok(())
}

/// Add `prop_val_sub` index
fn ref_v0_to_v1(store: &Db) -> AtomicResult<()> {
tracing::warn!("Rebuilding indexes...");
store.db.drop_tree("reference_index")?;
store.build_index(true)?;
tracing::warn!("Rebuilding index finished!");
Ok(())
}
8 changes: 7 additions & 1 deletion lib/src/storelike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ impl Query {
sort_by: None,
sort_desc: false,
include_external: false,
include_nested: false,
include_nested: true,
for_agent: None,
}
}
Expand All @@ -399,6 +399,12 @@ impl Query {
}
}

impl Default for Query {
fn default() -> Self {
Self::new()
}
}

pub struct QueryResult {
pub subjects: Vec<String>,
pub resources: Vec<Resource>,
Expand Down

0 comments on commit 8beb636

Please sign in to comment.