Skip to content

Commit

Permalink
fix: migrate from redb1 with new models
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-herlemont committed Jul 15, 2024
1 parent b2e3bf9 commit b5a0d4c
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 61 deletions.
6 changes: 3 additions & 3 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct Database<'a> {
impl Database<'_> {
/// Creates a new read-write transaction.
/// This transaction allows you to read and write data.
///
///
/// - Write operations:
/// - [`insert`](crate::transaction::RwTransaction::insert) - Insert a item.
/// - [`update`](crate::transaction::RwTransaction::update) - Update a item.
Expand All @@ -69,7 +69,7 @@ impl Database<'_> {

/// Creates a new read-only transaction.
/// This transaction allows you to read data.
///
///
/// - Read operations:
/// - [`get`](crate::transaction::RTransaction::get) - Get a item.
/// - [`scan`](crate::transaction::RTransaction::scan) - Scan items.
Expand All @@ -88,7 +88,7 @@ impl Database<'_> {

impl Database<'_> {
/// Watch queries.
///
///
/// - [`get`](crate::watch::query::Watch::get) - Watch a item.
/// - [`scan`](crate::watch::query::Watch::scan) - Watch items.
pub fn watch(&self) -> Watch {
Expand Down
22 changes: 11 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
//! - [Insert a model in the database](#insert-a-model-in-the-database)
//! - [Update a model](#update-a-model)
//! - [Migration](#migration)
//!
//!
//! # Api
//!
//!
//! - [`Models`] - Collection of models. *Equivalent to a schema in a traditional database*.
//! - [`new`](crate::Models::new) - Create a new collection of models.
//! - [`define`](crate::Models::define) - Define a model.
Expand Down Expand Up @@ -57,12 +57,12 @@
//! - [`all`](crate::watch::query::WatchScanSecondary::all) - Watch items with a given secondary key.
//! - [`start_with`](crate::watch::query::WatchScanSecondary::start_with) - Watch items with a secondary key starting with a key.
//! - [`range`](crate::watch::query::WatchScanSecondary::range) - Watch items with a secondary key in a given range.
//!
//!
//!
//!
//! # Quick Start
//!
//! We will create a simple example to show how to use the library.
//!
//!
//! ## Create a model
//!
//! > 👉 Unlike the usual database where there is a difference between *schema* and *model*, here, as we can directly use Rust types that are serialized in the database, we do not have the concept of *schema*, only that of the *model*.
Expand Down Expand Up @@ -124,7 +124,7 @@
//! # }
//! use native_db::*;
//! use once_cell::sync::Lazy;
//!
//!
//! // Define the models
//! // The lifetime of the models needs to be longer or equal to the lifetime of the database.
//! // In many cases, it is simpler to use a static variable but it is not mandatory.
Expand All @@ -134,7 +134,7 @@
//! models.define::<data::v1::Person>().unwrap();
//! models
//! });
//!
//!
//! fn main() -> Result<(), db_type::Error> {
//! // Create the database
//! let db = Builder::new().create_in_memory(&MODELS)?;
Expand Down Expand Up @@ -169,7 +169,7 @@
//! # }
//! use native_db::*;
//! use once_cell::sync::Lazy;
//! #
//! #
//! # static MODELS: Lazy<Models> = Lazy::new(|| {
//! # let mut models = Models::new();
//! # models.define::<data::v1::Person>().unwrap();
Expand Down Expand Up @@ -315,7 +315,7 @@
//! # }
//! use native_db::*;
//! use once_cell::sync::Lazy;
//!
//!
//! static MODELS: Lazy<Models> = Lazy::new(|| {
//! let mut models = Models::new();
//! // Define the models by specifying the version
Expand All @@ -332,9 +332,9 @@
//! let rw = db.rw_transaction()?;
//! rw.migrate::<data::Person>()?;
//! rw.commit()?;
//!
//!
//! // Now we can insert a person with the new field age ...
//!
//!
//! Ok(())
//! }
//! ```
Expand Down
13 changes: 6 additions & 7 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ use crate::{db_type::Result, table_definition::NativeModelOptions, ModelBuilder,

/// A collection of [`Model`](crate::Model) used by the [`Models`](crate::Models) to
/// [define](Self::define) models.
///
///
/// This collection allows you to manage multiple models efficiently, facilitating the process
/// of defining and manipulating them within your application.
///
///
/// # Note
/// Usually, there is little point in creating models at runtime. In some cases, it is necessary to define them with a `'static` lifetime, for example, to address compatibility issues with certain asynchronous libraries such as [Axum](https://github.com/tokio-rs/axum).
/// There are multiple ways to achieve this, including the [`once_cell::sync::Lazy`](https://docs.rs/once_cell/1.19.0/once_cell/sync/struct.Lazy.html) crate,
/// or the [`LazyLock`](https://doc.rust-lang.org/std/sync/struct.LazyLock.html) from the standard library, which is available when the relevant Rust feature is enabled.
///
///
/// ## Example using `once_cell::sync::Lazy`
///
///
/// ```rust
/// # pub mod data {
/// # use native_db::{native_db, ToKey};
Expand All @@ -37,7 +37,7 @@ use crate::{db_type::Result, table_definition::NativeModelOptions, ModelBuilder,
/// # }
/// use native_db::*;
/// use once_cell::sync::Lazy;
///
///
/// // The lifetime of the models needs to be longer or equal to the lifetime of the database.
/// // In many cases, it is simpler to use a static variable but it is not mandatory.
/// static MODELS: Lazy<Models> = Lazy::new(|| {
Expand All @@ -46,7 +46,7 @@ use crate::{db_type::Result, table_definition::NativeModelOptions, ModelBuilder,
/// models.define::<data::v1::Person>().unwrap();
/// models
/// });
///
///
/// fn main() -> Result<(), db_type::Error> {
/// // Initialize the database with the models
/// let db = Builder::new().create_in_memory(&MODELS)?;
Expand All @@ -59,7 +59,6 @@ pub struct Models {
}

impl Models {

/// Create a new collection of Models.
pub fn new() -> Self {
Self {
Expand Down
4 changes: 2 additions & 2 deletions src/transaction/query/drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct RwDrain<'db, 'txn> {

impl<'db, 'txn> RwDrain<'db, 'txn> {
/// Drain all items.
///
///
/// **TODO: needs to be improved, so don't use it yet.**
pub fn primary<T: ToInput>(&self) -> Result<Vec<T>> {
let model = T::native_db_model();
Expand All @@ -20,7 +20,7 @@ impl<'db, 'txn> RwDrain<'db, 'txn> {
}

/// Drain all items with a given secondary key.
///
///
/// **TODO: needs to be implemented**
pub fn secondary<T: ToInput>(&self, _key_def: impl ToKeyDefinition<KeyOptions>) -> () {
todo!()
Expand Down
13 changes: 5 additions & 8 deletions src/transaction/query/len.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ impl RLen<'_, '_> {
///
/// If the secondary key is [`optional`](struct.Builder.html#optional) you will
/// get all values that have the secondary key set.
///
///
/// # Example
/// ```rust
/// use native_db::*;
/// use native_model::{native_model, Model};
/// use serde::{Deserialize, Serialize};
///
///
/// #[derive(Serialize, Deserialize)]
/// #[native_model(id=1, version=1)]
/// #[native_db]
Expand All @@ -66,8 +66,8 @@ impl RLen<'_, '_> {
/// #[secondary_key(optional)]
/// name: Option<String>,
/// }
///
///
///
///
/// fn main() -> Result<(), db_type::Error> {
/// let mut models = Models::new();
/// models.define::<Data>()?;
Expand All @@ -81,10 +81,7 @@ impl RLen<'_, '_> {
/// Ok(())
/// }
/// ```
pub fn secondary<T: ToInput>(
&self,
key_def: impl ToKeyDefinition<KeyOptions>,
) -> Result<u64> {
pub fn secondary<T: ToInput>(&self, key_def: impl ToKeyDefinition<KeyOptions>) -> Result<u64> {
let model = T::native_db_model();
let result = self.internal.secondary_len(model, key_def)?;
Ok(result)
Expand Down
9 changes: 4 additions & 5 deletions src/transaction/query/scan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct RScan<'db, 'txn> {

impl<'txn> RScan<'_, 'txn> {
/// Get a values from the database by primary key.
///
///
/// - [`all`](crate::transaction::query::PrimaryScan::all) - Scan all items.
/// - [`start_with`](crate::transaction::query::PrimaryScan::start_with) - Scan items with a primary key starting with a key.
/// - [`range`](crate::transaction::query::PrimaryScan::range) - Scan items with a primary key in a given range.
Expand All @@ -30,7 +30,7 @@ impl<'txn> RScan<'_, 'txn> {
}

/// Get a values from the database by secondary key.
///
///
/// - [`all`](crate::transaction::query::PrimaryScan::all) - Scan all items.
/// - [`start_with`](crate::transaction::query::PrimaryScan::start_with) - Scan items with a primary key starting with a key.
/// - [`range`](crate::transaction::query::PrimaryScan::range) - Scan items with a primary key in a given range.
Expand All @@ -57,9 +57,8 @@ impl<'db, 'txn> RwScan<'db, 'txn>
where
'txn: 'db,
{

/// Get a values from the database by primary key.
///
///
/// - [`all`](crate::transaction::query::PrimaryScan::all) - Scan all items.
/// - [`start_with`](crate::transaction::query::PrimaryScan::start_with) - Scan items with a primary key starting with a key.
/// - [`range`](crate::transaction::query::PrimaryScan::range) - Scan items with a primary key in a given range.
Expand All @@ -73,7 +72,7 @@ where
}

/// Get a values from the database by secondary key.
///
///
/// - [`all`](crate::transaction::query::PrimaryScan::all) - Scan all items.
/// - [`start_with`](crate::transaction::query::PrimaryScan::start_with) - Scan items with a primary key starting with a key.
/// - [`range`](crate::transaction::query::PrimaryScan::range) - Scan items with a primary key in a given range.
Expand Down
6 changes: 3 additions & 3 deletions src/transaction/r_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct RTransaction<'db> {

impl<'db> RTransaction<'db> {
/// Get a value from the database.
///
///
/// - [`primary`](crate::transaction::query::RGet::primary) - Get a item by primary key.
/// - [`secondary`](crate::transaction::query::RGet::secondary) - Get a item by secondary key.
pub fn get<'txn>(&'txn self) -> RGet<'db, 'txn> {
Expand All @@ -19,7 +19,7 @@ impl<'db> RTransaction<'db> {
}

/// Get values from the database.
///
///
/// - [`primary`](crate::transaction::query::RScan::primary) - Scan items by primary key.
/// - [`secondary`](crate::transaction::query::RScan::secondary) - Scan items by secondary key.
pub fn scan<'txn>(&'txn self) -> RScan<'db, 'txn> {
Expand All @@ -29,7 +29,7 @@ impl<'db> RTransaction<'db> {
}

/// Get the number of values in the database.
///
///
/// - [`primary`](crate::transaction::query::RLen::primary) - Get the number of items by primary key.
/// - [`secondary`](crate::transaction::query::RLen::secondary) - Get the number of items by secondary key.
pub fn len<'txn>(&'txn self) -> RLen<'db, 'txn> {
Expand Down
2 changes: 1 addition & 1 deletion src/transaction/rw_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<'db> RwTransaction<'db> {
}

/// Drain values from the database.
///
///
/// **TODO: needs to be improved, so don't use it yet.**
pub fn drain<'txn>(&'txn self) -> RwDrain<'db, 'txn> {
RwDrain {
Expand Down
18 changes: 12 additions & 6 deletions src/upgrade/redb1_to_redb2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn upgrade_primary_table(
table_name: &str,
db1: &redb1::Database,
db2: &redb2::Database,
) -> Result<()> {
) -> Result<bool> {
let redb1_primary_table_definition: Redb1PrimaryTableDefinition =
redb1::TableDefinition::new(table_name);
let redb2_primary_table_definition: Redb2PrimaryTableDefinition =
Expand All @@ -30,9 +30,12 @@ fn upgrade_primary_table(
let redb2_write_txn = db2.begin_write()?;

{
let redb1_table = redb1_read_txn
.open_table(redb1_primary_table_definition)
.unwrap();
let redb1_table =
if let Ok(redb1_table) = redb1_read_txn.open_table(redb1_primary_table_definition) {
redb1_table
} else {
return Ok(false);
};
let mut redb2_table = redb2_write_txn.open_table(redb2_primary_table_definition)?;

use redb1::ReadableTable;
Expand All @@ -45,7 +48,7 @@ fn upgrade_primary_table(

redb2_write_txn.commit()?;

Ok(())
Ok(true)
}

fn upgrade_secondary_table(
Expand Down Expand Up @@ -99,11 +102,14 @@ pub(crate) fn upgrade_redb1_to_redb2(
let mut db2 = redb2_builder.create(&redb2_path)?;

for (_, model_builder) in model_builder {
upgrade_primary_table(
let exist = upgrade_primary_table(
model_builder.model.primary_key.unique_table_name.as_str(),
&db1,
&db2,
)?;
if !exist {
continue;
}

for secondary_key in model_builder.model.secondary_keys.iter() {
let secondary_table_name = secondary_key.unique_table_name.as_str();
Expand Down
4 changes: 2 additions & 2 deletions src/watch/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Watch<'db> {

impl<'db> Watch<'db> {
/// Watch only one value.
///
///
/// - [`primary`](crate::watch::query::WatchGet::primary) - Watch a item by primary key.
/// - [`secondary`](crate::watch::query::WatchGet::secondary) - Watch a item by secondary key.
pub fn get<'w>(&'w self) -> WatchGet<'db, 'w> {
Expand All @@ -22,7 +22,7 @@ impl<'db> Watch<'db> {
}
}
/// Watch multiple values.
///
///
/// - [`primary`](crate::watch::query::WatchScan::primary) - Watch items by primary key.
/// - [`secondary`](crate::watch::query::WatchScan::secondary) - Watch items by secondary key.
pub fn scan<'w>(&'w self) -> WatchScan<'db, 'w> {
Expand Down
4 changes: 2 additions & 2 deletions src/watch/query/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct WatchScan<'db, 'w> {
/// Watch multiple values.
impl WatchScan<'_, '_> {
/// Watch all values.
///
///
/// - [`all`](crate::watch::query::WatchScanPrimary::all) - Watch all items.
/// - [`start_with`](crate::watch::query::WatchScanPrimary::start_with) - Watch items with a primary key starting with a key.
/// - [`range`](crate::watch::query::WatchScanPrimary::range) - Watch items with a primary key in a given range.
Expand All @@ -23,7 +23,7 @@ impl WatchScan<'_, '_> {
}

/// Watch all values by secondary key.
///
///
/// - [`all`](crate::watch::query::WatchScanSecondary::all) - Watch items with a given secondary key.
/// - [`start_with`](crate::watch::query::WatchScanSecondary::start_with) - Watch items with a secondary key starting with a key.
/// - [`range`](crate::watch::query::WatchScanSecondary::range) - Watch items with a secondary key in a given range.
Expand Down
4 changes: 3 additions & 1 deletion tests/custom_type/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ fn insert_get() {
let tf = TmpFs::new().unwrap();
let mut models = Models::new();
models.define::<Item>().unwrap();
let db = Builder::new().create(&models, tf.path("test").as_std_path()).unwrap();
let db = Builder::new()
.create(&models, tf.path("test").as_std_path())
.unwrap();

let rw = db.rw_transaction().unwrap();
rw.insert(item.clone()).unwrap();
Expand Down
4 changes: 3 additions & 1 deletion tests/custom_type/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ fn insert_get() {
let tf = TmpFs::new().unwrap();
let mut models = Models::new();
models.define::<Item>().unwrap();
let db = Builder::new().create(&models, tf.path("test").as_std_path()).unwrap();
let db = Builder::new()
.create(&models, tf.path("test").as_std_path())
.unwrap();

let rw = db.rw_transaction().unwrap();
rw.insert(item.clone()).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion tests/deserialization_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn create_local_database_for_tests() {
let tmp = TmpFs::new().unwrap();
tmp.copy_assets(&PROJECT_DIR).unwrap();
tmp.display_dir_entries();
let database_path = tmp.path("db_0_6_0").to_path_buf();
let database_path = tmp.path("db_0_6_0").to_path_buf();

let mut models = Models::new();
models.define::<Item1>().unwrap();
Expand Down
Loading

0 comments on commit b5a0d4c

Please sign in to comment.