Skip to content

Commit

Permalink
#102 migrations working
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Apr 14, 2022
1 parent 0d62584 commit ff2acd5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ By far most changes relate to `atomic-server`, so if not specified, assume the c

## UNRELEASED

- **Warning**: Various default directories have moved (see #331). Most notably the `data` directory. The location depends on your OS. Run `show-config` to see where it will be stored now. If you have data in `~/.config/atomic/db`, move it to this new directory.
- **Warning**: Search index will have to be rebuilt. Start with `--rebuild-index`.
- **Warning**: Various default directories have moved (see #331). Most notably the `data` directory. The location depends on your OS. Run `show-config` to see where it will be stored now. If you have data in `~/.config/atomic/db`, move it to this new directory. Also, the search index will have to be rebuilt. Start with `--rebuild-index`.
- Updated various dependencies, and made `cargo.toml` less restrictive.
- Remove `async-std` calls from `upload.rs`
- Added `reset` and `show-config` commands to `atomic-server`.
- Added `data-dir` flag
- Replaced `awc` with `ureq` #374
- Get rid of `.unwrap` calls in `commit_monitor` #345
- Make process management optional #324 #334
- Internal migration logic for inter-version compatibility of the database. Makes upgrading trivial. #102

## [v0.31.1] - 2022-03-29

Expand Down
4 changes: 0 additions & 4 deletions lib/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ pub struct Db {
/// A list of all the Collections currently being used. Is used to update `members_index`.
/// See [collections_index]
watched_queries: sled::Tree,
/// Contains settings such as schema versions.
internal: sled::Tree,
/// The address where the db will be hosted, e.g. http://localhost/
server_url: String,
/// Endpoints are checked whenever a resource is requested. They calculate (some properties of) the resource and return it.
Expand All @@ -79,7 +77,6 @@ impl Db {
let reference_index = db.open_tree("reference_index")?;
let members_index = db.open_tree("members_index")?;
let watched_queries = db.open_tree("watched_queries")?;
let internal = db.open_tree("internal")?;
let store = Db {
db,
default_agent: Arc::new(Mutex::new(None)),
Expand All @@ -89,7 +86,6 @@ impl Db {
server_url,
watched_queries,
endpoints: default_endpoints(),
internal,
};
migrate_maybe(&store)?;
crate::populate::populate_base_models(&store)
Expand Down
23 changes: 17 additions & 6 deletions lib/src/db/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Therefore, we need migrations to convert the old schema to the new one.
## Adding a Migration
- Write a function called `v{OLD}_to_v{NEW} that takes a [Db].
- Write a function called `v{OLD}_to_v{NEW} that takes a [Db]. Make sure it removed the old `Tree`. Use [assert] to check if the process worked.
- In [migrate_maybe] add the key of the outdated Tree
- Add the function to the [migrate_maybe] `match` statement, select the older version of the Tree
- Update the Tree key used in [Db::init]
Expand All @@ -30,14 +30,19 @@ pub fn migrate_maybe(store: &Db) -> AtomicResult<()> {
fn v0_to_v1(store: &Db) -> AtomicResult<()> {
tracing::warn!("Migrating resources schema from v0 to v1...");
let new = store.db.open_tree("resources_v1")?;
let old_key = "resources";
let old = store.db.open_tree(old_key)?;
let mut count = 0;

let config = sled::Config::new().temporary(true);
let db = config.open().unwrap();

use sled::Transactional;
for item in old.into_iter() {
let (subject, resource_bin) = item.expect("Unable to convert into iterable");
let subject: String =
bincode::deserialize(&subject).expect("Unable to deserialize subject");
new.insert(subject.as_bytes(), resource_bin)?;
count += 1;
}

// TODO: Let this compile!:
// TODO: Prefer transactional approach, but issue preventing me from compiling:
// https://github.com/spacejam/sled/issues/1406
// (&store.resources, &new)
// .transaction(|(old, new)| {
Expand All @@ -57,6 +62,12 @@ fn v0_to_v1(store: &Db) -> AtomicResult<()> {
store.resources.len(),
"Not all resources were migrated."
);

assert!(
store.db.drop_tree(old_key)?,
"Old resources tree not properly removed."
);

tracing::warn!("Finished migration of {} resources", count);
Ok(())
}

0 comments on commit ff2acd5

Please sign in to comment.