Skip to content

Commit

Permalink
graph: Track bytes ids, aggregrations and immutable entities in subgr…
Browse files Browse the repository at this point in the history
…aph features table
  • Loading branch information
incrypto32 committed Jun 20, 2024
1 parent 9a0eb70 commit 224a29a
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 15 deletions.
19 changes: 15 additions & 4 deletions graph/src/data/subgraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,9 @@ pub struct DeploymentFeatures {
pub network: String,
pub handler_kinds: Vec<String>,
pub has_declared_calls: bool,
// pub has_bytes_as_ids: bool,
// pub has_aggregations: bool,
pub has_bytes_as_ids: bool,
pub has_aggregations: bool,
pub immutable_entities: Vec<String>,
}

impl IntoValue for DeploymentFeatures {
Expand All @@ -547,8 +548,9 @@ impl IntoValue for DeploymentFeatures {
handlers: self.handler_kinds,
network: self.network,
hasDeclaredEthCalls: self.has_declared_calls,
// TODO: usesBytesAsIds: self.uses_bytes_as_ids,
// TODO: usesAggregations: self.uses_aggregations,
hasBytesAsIds: self.has_bytes_as_ids,
hasAggregations: self.has_aggregations,
immutableEntities: self.immutable_entities
}
}
}
Expand Down Expand Up @@ -802,6 +804,12 @@ impl<C: Blockchain> SubgraphManifest<C> {
let unified_api_version = self.unified_mapping_api_version().ok();
let network = self.network_name();
let has_declared_calls = self.data_sources.iter().any(|ds| ds.has_declared_calls());
let has_aggregations = self.schema.has_aggregations();
let immutable_entities = self
.schema
.immutable_entities()
.map(|s| s.to_string())
.collect_vec();

let api_version = unified_api_version
.map(|v| v.version().map(|v| v.to_string()))
Expand Down Expand Up @@ -847,6 +855,9 @@ impl<C: Blockchain> SubgraphManifest<C> {
.collect_vec(),
network,
has_declared_calls,
has_bytes_as_ids: self.schema.has_bytes_as_ids(),
immutable_entities,
has_aggregations,
}
}

Expand Down
19 changes: 19 additions & 0 deletions graph/src/schema/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,18 @@ impl InputSchema {
self.inner.enum_map.values(name)
}

pub fn immutable_entities<'a>(&'a self) -> impl Iterator<Item = EntityType> + 'a {
self.inner
.type_infos
.iter()
.filter_map(|ti| match ti {
TypeInfo::Object(obj_type) => Some(obj_type),
TypeInfo::Interface(_) | TypeInfo::Aggregation(_) => None,
})
.filter(|obj_type| obj_type.immutable)
.map(|obj_type| EntityType::new(self.cheap_clone(), obj_type.name))
}

/// Return a list of the entity types defined in the schema, i.e., the
/// types that have a `@entity` annotation. This does not include the
/// type for the PoI
Expand Down Expand Up @@ -1353,6 +1365,13 @@ impl InputSchema {
self.inner.agg_mappings.iter()
}

pub fn has_bytes_as_ids(&self) -> bool {
self.inner
.type_infos
.iter()
.any(|ti| ti.id_type() == Some(store::IdType::Bytes))
}

pub fn has_aggregations(&self) -> bool {
self.inner
.type_infos
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
ALTER TABLE subgraphs.subgraph_features
DROP COLUMN IF EXISTS has_declared_calls;
DROP COLUMN IF EXISTS has_declared_calls,
DROP COLUMN IF EXISTS has_bytes_as_ids,
DROP COLUMN IF EXISTS has_aggregations,
DROP COLUMN IF EXISTS immutable_entities;
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
ALTER TABLE subgraphs.subgraph_features
ADD COLUMN IF NOT EXISTS has_declared_calls BOOLEAN NOT NULL DEFAULT FALSE;
ADD COLUMN IF NOT EXISTS has_declared_calls BOOLEAN NOT NULL DEFAULT FALSE,
ADD COLUMN IF NOT EXISTS has_bytes_as_ids BOOLEAN NOT NULL DEFAULT FALSE,
ADD COLUMN IF NOT EXISTS has_aggregations BOOLEAN NOT NULL DEFAULT FALSE,
ADD COLUMN IF NOT EXISTS immutable_entities TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[];
36 changes: 28 additions & 8 deletions store/postgres/src/primary.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
//! Utilities for dealing with subgraph metadata that resides in the primary
//! shard. Anything in this module can only be used with a database connection
//! for the primary shard.
use crate::{
block_range::UNVERSIONED_RANGE,
connection_pool::{ConnectionPool, ForeignServer},
detail::DeploymentDetail,
subgraph_store::{unused, Shard, PRIMARY_SHARD},
NotificationSender,
};
use diesel::{
connection::SimpleConnection,
data_types::PgTimestamp,
Expand Down Expand Up @@ -48,14 +55,6 @@ use std::{
time::{SystemTime, UNIX_EPOCH},
};

use crate::{
block_range::UNVERSIONED_RANGE,
connection_pool::{ConnectionPool, ForeignServer},
detail::DeploymentDetail,
subgraph_store::{unused, Shard, PRIMARY_SHARD},
NotificationSender,
};

#[cfg(debug_assertions)]
use std::sync::Mutex;
#[cfg(debug_assertions)]
Expand Down Expand Up @@ -88,6 +87,9 @@ table! {
handlers -> Array<Text>,
network -> Text,
has_declared_calls -> Bool,
has_bytes_as_ids -> Bool,
has_aggregations -> Bool,
immutable_entities -> Array<Text>
}
}

Expand Down Expand Up @@ -1136,6 +1138,9 @@ impl<'a> Connection<'a> {
f::handlers,
f::network,
f::has_declared_calls,
f::has_bytes_as_ids,
f::has_aggregations,
f::immutable_entities,
))
.first::<(
String,
Expand All @@ -1146,6 +1151,9 @@ impl<'a> Connection<'a> {
Vec<String>,
String,
bool,
bool,
bool,
Vec<String>,
)>(conn)
.optional()?;

Expand All @@ -1159,6 +1167,9 @@ impl<'a> Connection<'a> {
handlers,
network,
has_declared_calls,
has_bytes_as_ids,
has_aggregations,
immutable_entities,
)| {
DeploymentFeatures {
id,
Expand All @@ -1169,6 +1180,9 @@ impl<'a> Connection<'a> {
handler_kinds: handlers,
network: network,
has_declared_calls,
has_bytes_as_ids,
has_aggregations,
immutable_entities,
}
},
);
Expand All @@ -1191,6 +1205,9 @@ impl<'a> Connection<'a> {
handler_kinds,
network,
has_declared_calls,
has_bytes_as_ids,
immutable_entities,
has_aggregations,
} = features;

let conn = self.conn.as_mut();
Expand All @@ -1203,6 +1220,9 @@ impl<'a> Connection<'a> {
f::handlers.eq(handler_kinds),
f::network.eq(network),
f::has_declared_calls.eq(has_declared_calls),
f::has_bytes_as_ids.eq(has_bytes_as_ids),
f::immutable_entities.eq(immutable_entities),
f::has_aggregations.eq(has_aggregations),
);

insert_into(f::table)
Expand Down
35 changes: 34 additions & 1 deletion store/test-store/tests/postgres/subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@ const SUBGRAPH_GQL: &str = "
}
";

const SUBGRAPH_FEATURES_GQL: &str = "
type User @entity {
id: ID!,
name: String
}
type User2 @entity(immutable: true) {
id: Bytes!,
name: String
}
type Data @entity(timeseries: true) {
id: Int8!
timestamp: Timestamp!
price: BigDecimal!
}
type Stats @aggregation(intervals: [\"hour\", \"day\"], source: \"Data\") {
id: Int8!
timestamp: Timestamp!
sum: BigDecimal! @aggregate(fn: \"sum\", arg: \"price\")
}
";

fn assigned(deployment: &DeploymentLocator) -> EntityChange {
EntityChange::Assignment {
deployment: deployment.clone(),
Expand Down Expand Up @@ -502,7 +526,7 @@ fn subgraph_features() {

remove_subgraphs();
block_store::set_chain(vec![], NETWORK_NAME).await;
create_test_subgraph_with_features(&id, SUBGRAPH_GQL).await;
create_test_subgraph_with_features(&id, SUBGRAPH_FEATURES_GQL).await;

let DeploymentFeatures {
id: subgraph_id,
Expand All @@ -513,6 +537,9 @@ fn subgraph_features() {
network,
handler_kinds,
has_declared_calls,
has_bytes_as_ids,
immutable_entities,
has_aggregations,
} = get_subgraph_features(id.to_string()).unwrap();

assert_eq!(NAME, subgraph_id.as_str());
Expand All @@ -531,6 +558,12 @@ fn subgraph_features() {
assert!(handler_kinds.contains(&"mock_handler_1".to_string()));
assert!(handler_kinds.contains(&"mock_handler_2".to_string()));
assert_eq!(has_declared_calls, true);
assert_eq!(has_bytes_as_ids, true);
assert_eq!(has_aggregations, true);
assert_eq!(
immutable_entities,
vec!["User2".to_string(), "Data".to_string()]
);

test_store::remove_subgraph(&id);
let features = get_subgraph_features(id.to_string());
Expand Down

0 comments on commit 224a29a

Please sign in to comment.