Skip to content

Commit

Permalink
refactor!: Query and Migration
Browse files Browse the repository at this point in the history
- fix: missed DataType::Numeric
- fix: migration/rollback SchemaVersion
- feat: response::Query provides iterator for results
- feat: some new converters for SchemaVersion
- test: request integration tests moved to tests-directory
- test: added some unit tests for Request
- test_rqlited is now a sub-crate with dep-dependency
  * tries to connect and use default database, before starting
- test: cleanedup integration test
- test: added locks for DB modifications:
  * is needed because there is only a single DB and schema state and
    tests need a requested state without becoming it changed by
    another test
  * migrations for migration_test starting with 90, schema before is
    not touched
  • Loading branch information
kolbma committed Oct 12, 2023
1 parent 63333df commit 9db073e
Show file tree
Hide file tree
Showing 46 changed files with 769 additions and 535 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ tracing = { version = "0.1.37", optional = true }
url = { version = "2.4.1", optional = true }

[dev-dependencies]
test_rqlited = { path = "./test_rqlited", features = ["url"] }
time = { version = "0.3.29", features = ["formatting"] }

[build-dependencies]
Expand Down
13 changes: 8 additions & 5 deletions src/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub enum DataType {
Boolean,
/// Integer
Integer,
/// Numeric
Numeric,
/// Float, Double, Real
Real,
/// Text
Expand All @@ -22,11 +24,12 @@ pub enum DataType {
impl std::fmt::Display for DataType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DataType::Blob => f.write_str("blob"),
DataType::Boolean => f.write_str("boolean"),
DataType::Integer => f.write_str("integer"),
DataType::Real => f.write_str("real"),
DataType::Text => f.write_str("text"),
Self::Blob => f.write_str("blob"),
Self::Boolean => f.write_str("boolean"),
Self::Integer => f.write_str("integer"),
Self::Numeric => f.write_str("numeric"),
Self::Real => f.write_str("real"),
Self::Text => f.write_str("text"),
}
}
}
8 changes: 2 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ use lazy_static as _;
#[cfg(feature = "percent_encoding")]
use percent_encoding as _;
#[cfg(test)]
#[cfg(not(feature = "ureq"))]
use test_rqlited as _;
#[cfg(test)]
use time as _;

pub use buildtime::BUILD_TIME;
Expand Down Expand Up @@ -307,9 +308,4 @@ mod query;
mod request;
mod request_builder;
pub mod response;
#[cfg(test)]
mod test_rqlited;
pub(crate) mod tracing;

#[cfg(test)]
mod tests {}
29 changes: 18 additions & 11 deletions src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
pub use downgrade::Downgrade;
#[allow(clippy::module_name_repetitions)]
pub use error::Error as MigrationError;
pub use schema_version::SchemaVersion;
pub use schema_version::{SchemaVersion, MAX as SCHEMA_VERSION_MAX};
pub(crate) use sql::Sql;
pub use upgrade::Upgrade;

Expand Down Expand Up @@ -157,6 +157,12 @@ where
}
}

/// Maximum of available [`SchemaVersion`]
///
pub fn max(&self) -> SchemaVersion {
self.migrations.len().into()
}

/// Migrate provided `Migration`
///
/// # Return
Expand Down Expand Up @@ -215,7 +221,7 @@ where
let mut version = SchemaVersion::default();

for (upgrade, _) in self.migrations.iter().map(Mtuple::from) {
if let Some(to_version) = to_version.filter(|v| &version > *v) {
if let Some(to_version) = to_version.filter(|v| &version >= *v) {
let _ = to_version;
log::trace!("db_version: {db_version} - migrated to version {to_version}");
tracing::trace!("db_version: {db_version} - migrated to version {to_version}");
Expand All @@ -237,7 +243,6 @@ where
}

if let Some(to_version) = to_version {
version = version.checked_sub(1).unwrap_or_default();
if version != *to_version {
return Err(MigrationError::DataMalformat(format!(
"no migration {to_version}"
Expand Down Expand Up @@ -359,8 +364,8 @@ where
let mut version = db_version;

for (_, downgrade) in self.migrations.iter().rev().map(Mtuple::from) {
if let Some(downgrade) = downgrade {
if version > *to_version {
if version > *to_version {
if let Some(downgrade) = downgrade {
version = version.checked_sub(1).unwrap_or_default();

log::debug!("db_version: {db_version} rollback: {version}");
Expand All @@ -369,14 +374,16 @@ where
for line in downgrade.lines() {
query = query.push_sql(Value::from(line));
}
}
} else {
version = version.checked_sub(1).unwrap_or_default();
} else {
version = version.checked_sub(1).unwrap_or_default();

log::debug!("db_version: {db_version} rollback: {version}");
tracing::debug!("db_version: {db_version} rollback: {version}");
log::debug!("db_version: {db_version} rollback: {version}");
tracing::debug!("db_version: {db_version} rollback: {version}");

continue;
continue;
}
} else {
break;
}
}

Expand Down
59 changes: 59 additions & 0 deletions src/migration/schema_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, PartialOrd)]
pub struct SchemaVersion(pub u64);

/// Maximum [`SchemaVersion`]
pub const MAX: SchemaVersion = SchemaVersion(u64::MAX);

impl SchemaVersion {
/// Checked addition with a signed integer. Computes self + `rhs`, returning `None` if overflow occurred.
#[inline]
Expand All @@ -22,6 +25,13 @@ impl SchemaVersion {
}
.map(Self)
}

/// Maximum [`SchemaVersion`]
#[must_use]
#[inline]
pub const fn max() -> SchemaVersion {
MAX
}
}

impl From<&SchemaVersion> for u64 {
Expand All @@ -36,6 +46,43 @@ impl From<SchemaVersion> for u64 {
}
}

impl From<u64> for SchemaVersion {
fn from(value: u64) -> Self {
Self(value)
}
}

impl From<i32> for SchemaVersion {
fn from(value: i32) -> Self {
if value < 0 {
Self(0_u64)
} else {
0_u64
.checked_add_signed(i64::from(value))
.expect("unsigned conversion fail")
.into()
}
}
}

impl From<&SchemaVersion> for usize {
fn from(value: &SchemaVersion) -> Self {
Self::try_from(u64::from(value)).unwrap_or(Self::MAX)
}
}

impl From<SchemaVersion> for usize {
fn from(value: SchemaVersion) -> Self {
Self::try_from(u64::from(value)).unwrap_or(Self::MAX)
}
}

impl From<usize> for SchemaVersion {
fn from(value: usize) -> Self {
Self(u64::try_from(value).unwrap_or(u64::MAX))
}
}

impl std::fmt::Display for SchemaVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("{}", self.0))
Expand Down Expand Up @@ -97,3 +144,15 @@ impl std::ops::AddAssign<i32> for SchemaVersion {
self.0 += u64::try_from(rhs).expect("unsigned conversion fail");
}
}

#[cfg(test)]
mod tests {
use super::SchemaVersion;

#[test]
fn from_i32_test() {
assert_eq!(SchemaVersion::from(i32::MIN), 0.into());
assert_eq!(SchemaVersion::from(0), 0.into());
assert_eq!(SchemaVersion::from(i32::MAX), SchemaVersion(2_147_483_647));
}
}
Loading

0 comments on commit 9db073e

Please sign in to comment.