-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82ebe53
commit 56f4990
Showing
7 changed files
with
256 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod custom_type; | ||
mod macro_def; | ||
|
||
mod metadata; | ||
mod migrate; | ||
mod primary_drain; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use native_db::*; | ||
use native_model::{native_model, Model}; | ||
use redb::ReadableTable; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
const TABLE: redb::TableDefinition<Key, Key> = redb::TableDefinition::new("2_1_name"); | ||
|
||
#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug)] | ||
#[native_model(id = 1, version = 1)] | ||
#[native_db] | ||
struct Item1 { | ||
#[primary_key] | ||
id: u32, | ||
#[secondary_key(unique)] | ||
name: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug)] | ||
#[native_model(id = 2, version = 1)] | ||
#[native_db] | ||
struct Item2 { | ||
#[primary_key] | ||
id: u32, | ||
#[secondary_key(optional)] | ||
id2: Option<u32>, | ||
#[secondary_key] | ||
name: String, | ||
} | ||
|
||
#[test] | ||
fn upgrade_from_0_7_x_to_0_8_x() { | ||
use std::path::PathBuf; | ||
#[cfg(any(target_os = "android", target_os = "ios"))] | ||
let database_path = { dinghy_test::test_project_path().join("tests/data/db_0_7_1") }; | ||
|
||
#[cfg(not(any(target_os = "android", target_os = "ios")))] | ||
let database_path = { | ||
let root_project_path = env!("CARGO_MANIFEST_DIR"); | ||
PathBuf::from(format!("{}/tests/data/db_0_7_1", root_project_path)) | ||
}; | ||
|
||
use shortcut_assert_fs::TmpFs; | ||
let tmp = TmpFs::new().unwrap(); | ||
|
||
// Copy the legacy database to the temporary directory. | ||
let tmp_database_path = tmp.path("db_0_7_1"); | ||
std::fs::copy(&database_path, &tmp_database_path).unwrap(); | ||
|
||
// Check before refresh the number of bytes of the secondary table. | ||
// We check that the delimiter is not included in the secondary table. | ||
{ | ||
let db = redb::Database::open(&tmp_database_path).unwrap(); | ||
let rx = db.begin_read().unwrap(); | ||
let table = rx.open_table(TABLE).unwrap(); | ||
let (key, _) = table.first().unwrap().unwrap(); | ||
assert_eq!( | ||
key.value(), | ||
Key::new(vec![105, 116, 101, 109, 50, 95, 48, 0, 0, 0, 0]) | ||
); | ||
} | ||
|
||
// Refresh the database | ||
let mut models = Models::new(); | ||
models.define::<Item1>().unwrap(); | ||
models.define::<Item2>().unwrap(); | ||
let db = Builder::new().open(&models, &tmp_database_path).unwrap(); | ||
if db.upgrading_from_version("<0.8.0").unwrap() { | ||
// Refresh the database | ||
let rw = db.rw_transaction().unwrap(); | ||
// Refresh the Item2 because there is a non-unique secondary key | ||
rw.refresh::<Item2>().unwrap(); | ||
rw.commit().unwrap(); | ||
} | ||
drop(db); | ||
|
||
// Check after refresh the number of bytes of the secondary table. | ||
// We check that the delimiter is not included in the secondary table. | ||
let db = redb::Database::open(&tmp_database_path).unwrap(); | ||
let rx = db.begin_read().unwrap(); | ||
let table = rx.open_table(TABLE).unwrap(); | ||
let (key, _) = table.first().unwrap().unwrap(); | ||
// The delimiter is a "0" byte included between the secondary key and the primary key | ||
// Ignore fmt | ||
#[rustfmt::skip] | ||
assert_eq!( | ||
key.value(), | ||
// | secondary key | delimiter | primary key | | ||
Key::new(vec![105, 116, 101, 109, 50, 95, 48, 0, 0, 0, 0, 0]) | ||
); | ||
drop(rx); | ||
drop(db); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mod from_0_5_x_to_0_6_x; | ||
mod from_0_7_x_to_0_8_x; |