Skip to content

Commit

Permalink
Merge branch 'bump-sqlx' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam committed Oct 28, 2024
2 parents ef0b4b1 + a3c214b commit 6213af1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ serde_bytes = "0.11.8"
serde_json = "1.0.109"
similar-asserts = "1.6.0"
slab = "0.4.9"
sqlx = { version = "0.7.4", default-features = false, features = ["runtime-tokio", "sqlite"] }
sqlx = { version = "0.8.2", default-features = false, features = ["derive", "runtime-tokio", "sqlite"] }
tempfile = "3.13"
test-strategy = "0.4.0"
thiserror = "1.0.65"
Expand Down
5 changes: 5 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ tokio-util = { workspace = true, features = ["codec", "compat"] }
tracing = { workspace = true }
walkdir = "2.5.0"

# HACK: This is only a transitive dependency of `interprocess` but the version they depend on has a
# bug. Forcing the fixed version here.
# TODO: Remove this when we bump `interprocess`.
async-channel = "2.3.1"

[dev-dependencies]
anyhow = { workspace = true }
assert_matches = { workspace = true }
Expand Down
5 changes: 4 additions & 1 deletion lib/src/crypto/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ impl sqlx::Type<Sqlite> for Signature {
}

impl<'q> sqlx::Encode<'q, Sqlite> for &'q Signature {
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> sqlx::encode::IsNull {
fn encode_by_ref(
&self,
args: &mut Vec<SqliteArgumentValue<'q>>,
) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
// It seems there is no way to avoid the allocation here because sqlx doesn't implement
// `Encode` for arrays.
sqlx::Encode::<Sqlite>::encode(self.to_bytes().to_vec(), args)
Expand Down
2 changes: 1 addition & 1 deletion lib/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ macro_rules! derive_sqlx_traits_for_byte_array_wrapper {
fn encode_by_ref(
&self,
args: &mut Vec<sqlx::sqlite::SqliteArgumentValue<'q>>,
) -> sqlx::encode::IsNull {
) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
sqlx::Encode::<sqlx::sqlite::Sqlite>::encode_by_ref(&(*self).as_ref(), args)
}
}
Expand Down
80 changes: 10 additions & 70 deletions lib/src/protocol/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use sqlx::{
Decode, Encode, Sqlite, Type,
};
use std::fmt;
use thiserror::Error;
use xxhash_rust::xxh3::Xxh3Default;

/// Summary info of a snapshot subtree. Contains whether the subtree has been completely downloaded
Expand Down Expand Up @@ -94,7 +93,7 @@ impl Summary {
}
}

#[derive(Copy, Clone, Eq, PartialEq, Debug, Serialize, Deserialize)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Serialize, Deserialize, sqlx::Type)]
#[repr(u8)]
pub enum NodeState {
Incomplete = 0, // Some nodes are missing
Expand All @@ -119,32 +118,6 @@ impl NodeState {
}
}

impl Type<Sqlite> for NodeState {
fn type_info() -> SqliteTypeInfo {
<u8 as Type<Sqlite>>::type_info()
}
}

impl<'q> Encode<'q, Sqlite> for NodeState {
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
Encode::<Sqlite>::encode(*self as u8, args)
}
}

impl<'r> Decode<'r, Sqlite> for NodeState {
fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
let num = <u8 as Decode<Sqlite>>::decode(value)?;

match num {
0 => Ok(Self::Incomplete),
1 => Ok(Self::Complete),
2 => Ok(Self::Approved),
3 => Ok(Self::Rejected),
_ => Err(InvalidValue(num).into()),
}
}
}

#[cfg(test)]
mod test_utils {
use super::NodeState;
Expand All @@ -168,16 +141,13 @@ mod test_utils {
}
}

#[derive(Debug, Error)]
#[error("invalid value: {0}")]
pub(crate) struct InvalidValue(u8);

/// Information about the presence of a single block.
#[derive(Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Copy, Clone, Eq, PartialEq, Serialize, Deserialize, sqlx::Type)]
#[repr(u8)]
pub enum SingleBlockPresence {
Missing,
Present,
Expired,
Missing = 0,
Present = 1,
Expired = 2,
}

impl SingleBlockPresence {
Expand All @@ -190,39 +160,6 @@ impl SingleBlockPresence {
}
}

impl Type<Sqlite> for SingleBlockPresence {
fn type_info() -> SqliteTypeInfo {
<u8 as Type<Sqlite>>::type_info()
}

fn compatible(ty: &SqliteTypeInfo) -> bool {
<u8 as Type<Sqlite>>::compatible(ty)
}
}

impl<'q> Encode<'q, Sqlite> for SingleBlockPresence {
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
let n = match self {
SingleBlockPresence::Missing => 0,
SingleBlockPresence::Present => 1,
SingleBlockPresence::Expired => 2,
};

Encode::<Sqlite>::encode(n, args)
}
}

impl<'r> Decode<'r, Sqlite> for SingleBlockPresence {
fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
match <u8 as Decode<'r, Sqlite>>::decode(value)? {
0 => Ok(SingleBlockPresence::Missing),
1 => Ok(SingleBlockPresence::Present),
2 => Ok(SingleBlockPresence::Expired),
n => Err(InvalidValue(n).into()),
}
}
}

impl fmt::Debug for SingleBlockPresence {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down Expand Up @@ -279,7 +216,10 @@ impl Type<Sqlite> for MultiBlockPresence {
}

impl<'q> Encode<'q, Sqlite> for &'q MultiBlockPresence {
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
fn encode_by_ref(
&self,
args: &mut Vec<SqliteArgumentValue<'q>>,
) -> Result<IsNull, BoxDynError> {
Encode::<Sqlite>::encode(self.checksum(), args)
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/src/version_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ impl Type<Sqlite> for VersionVector {
}

impl<'q> Encode<'q, Sqlite> for VersionVector {
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
fn encode_by_ref(
&self,
args: &mut Vec<SqliteArgumentValue<'q>>,
) -> Result<IsNull, BoxDynError> {
Encode::<Sqlite>::encode_by_ref(
&bincode::serialize(self).expect("failed to serialize VersionVector for db"),
args,
Expand Down

0 comments on commit 6213af1

Please sign in to comment.