Skip to content

Commit

Permalink
adapt code for diesel2 library
Browse files Browse the repository at this point in the history
  • Loading branch information
zorancv committed Feb 13, 2024
1 parent 3e88969 commit bb600ca
Show file tree
Hide file tree
Showing 54 changed files with 2,626 additions and 1,938 deletions.
118 changes: 67 additions & 51 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ repository = "https://github.com/graphprotocol/graph-node"
license = "MIT OR Apache-2.0"

[workspace.dependencies]
diesel = { version = "2.1.3", features = ["postgres", "serde_json", "numeric", "r2d2", "chrono"] }
diesel-derive-enum = { version = "2.1.0", features = ["postgres"] }
diesel_derives = "2.1.2"
diesel-dynamic-schema = "0.2.1"
diesel_migrations = "2.1.0"
prost = "0.11.9"
prost-types = "0.11.9"
tonic = { version = "0.8.3", features = ["tls-roots", "gzip"] }
Expand Down
2 changes: 1 addition & 1 deletion chain/arweave/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ graph-runtime-wasm = { path = "../../runtime/wasm" }
graph-runtime-derive = { path = "../../runtime/derive" }

[dev-dependencies]
diesel = { version = "1.4.7", features = ["postgres", "serde_json", "numeric", "r2d2"] }
diesel = { workspace = true }
2 changes: 1 addition & 1 deletion chain/near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ graph-runtime-wasm = { path = "../../runtime/wasm" }
graph-runtime-derive = { path = "../../runtime/derive" }

[dev-dependencies]
diesel = { version = "1.4.7", features = ["postgres", "serde_json", "numeric", "r2d2"] }
diesel = { workspace = true }
trigger-filters.path = "../../substreams/trigger-filters"
8 changes: 4 additions & 4 deletions graph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ anyhow = "1.0"
async-trait = "0.1.50"
async-stream = "0.3"
atomic_refcell = "0.1.13"
bigdecimal = { version = "0.1.0", features = ["serde"] }
bigdecimal = { version = "0.3.1", features = ["serde"] }
bytes = "1.0.1"
cid = "0.11.0"
defer = "0.1"
diesel = { version = "1.4.8", features = ["postgres", "serde_json", "numeric", "r2d2", "chrono"] }
diesel_derives = "1.4"
diesel = { workspace = true }
diesel_derives = { workspace = true }
chrono = "0.4.31"
envconfig = "0.10.0"
Inflector = "0.11.3"
Expand All @@ -26,7 +26,7 @@ futures = "0.1.21"
graphql-parser = "0.4.0"
humantime = "2.1.0"
lazy_static = "1.4.0"
num-bigint = { version = "^0.2.6", features = ["serde"] }
num-bigint = { version = "0.4.4", features = ["serde"] }
num_cpus = "1.16.0"
num-traits = "=0.2.17"
rand = "0.8.4"
Expand Down
18 changes: 8 additions & 10 deletions graph/src/blockchain/types.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use anyhow::anyhow;
use chrono::{DateTime, Utc};
use diesel::deserialize::FromSql;
use diesel::pg::Pg;
use diesel::serialize::Output;
use diesel::serialize::{Output, ToSql};
use diesel::sql_types::Timestamptz;
use diesel::sql_types::{Bytea, Nullable, Text};
use diesel::types::FromSql;
use diesel::types::ToSql;
use diesel_derives::{AsExpression, FromSqlRow};
use diesel_derives::FromSqlRow;
use std::convert::TryFrom;
use std::io::Write;
use std::time::Duration;
use std::{fmt, str::FromStr};
use web3::types::{Block, H256};
Expand All @@ -20,7 +18,7 @@ use crate::util::stable_hash_glue::{impl_stable_hash, AsBytes};
use crate::{cheap_clone::CheapClone, components::store::BlockNumber};

/// A simple marker for byte arrays that are really block hashes
#[derive(Clone, Default, PartialEq, Eq, Hash, AsExpression, FromSqlRow)]
#[derive(Clone, Default, PartialEq, Eq, Hash, FromSqlRow)]
pub struct BlockHash(pub Box<[u8]>);

impl_stable_hash!(BlockHash(transparent: AsBytes));
Expand Down Expand Up @@ -95,23 +93,23 @@ impl FromStr for BlockHash {
}

impl FromSql<Nullable<Text>, Pg> for BlockHash {
fn from_sql(bytes: Option<&[u8]>) -> diesel::deserialize::Result<Self> {
fn from_sql(bytes: diesel::pg::PgValue) -> diesel::deserialize::Result<Self> {
let s = <String as FromSql<Text, Pg>>::from_sql(bytes)?;
BlockHash::try_from(s.as_str())
.map_err(|e| format!("invalid block hash `{}`: {}", s, e).into())
}
}

impl FromSql<Text, Pg> for BlockHash {
fn from_sql(bytes: Option<&[u8]>) -> diesel::deserialize::Result<Self> {
fn from_sql(bytes: diesel::pg::PgValue) -> diesel::deserialize::Result<Self> {
let s = <String as FromSql<Text, Pg>>::from_sql(bytes)?;
BlockHash::try_from(s.as_str())
.map_err(|e| format!("invalid block hash `{}`: {}", s, e).into())
}
}

impl FromSql<Bytea, Pg> for BlockHash {
fn from_sql(bytes: Option<&[u8]>) -> diesel::deserialize::Result<Self> {
fn from_sql(bytes: diesel::pg::PgValue) -> diesel::deserialize::Result<Self> {
let bytes = <Vec<u8> as FromSql<Bytea, Pg>>::from_sql(bytes)?;
Ok(BlockHash::from(bytes))
}
Expand Down Expand Up @@ -390,7 +388,7 @@ impl TryFrom<&Value> for BlockTime {
}

impl ToSql<Timestamptz, Pg> for BlockTime {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> diesel::serialize::Result {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> diesel::serialize::Result {
<DateTime<Utc> as ToSql<Timestamptz, Pg>>::to_sql(&self.0, out)
}
}
Loading

0 comments on commit bb600ca

Please sign in to comment.