Skip to content

Commit c8d8103

Browse files
committed
Subgraph composition: spec version
1 parent e61452f commit c8d8103

File tree

13 files changed

+59
-27
lines changed

13 files changed

+59
-27
lines changed

graph/src/components/store/entity_cache.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,25 @@ impl EntityCache {
207207
};
208208

209209
// Always test the cache consistency in debug mode. The test only
210-
// makes sense when we were actually asked to read from the store
210+
// makes sense when we were actually asked to read from the store.
211+
// We need to remove the VID as the one from the DB might come from
212+
// a legacy subgraph that has VID autoincremented while this trait
213+
// always creates it in a new style.
211214
debug_assert!(match scope {
212-
GetScope::Store => entity == self.store.get(key).unwrap().map(Arc::new),
215+
GetScope::Store => {
216+
// Release build will never call this function and hence it's OK
217+
// when that implementation is not correct.
218+
fn remove_vid(entity: Option<Arc<Entity>>) -> Option<Entity> {
219+
entity.map(|e| {
220+
#[allow(unused_mut)]
221+
let mut entity = (*e).clone();
222+
#[cfg(debug_assertions)]
223+
entity.remove("vid");
224+
entity
225+
})
226+
}
227+
remove_vid(entity.clone()) == remove_vid(self.store.get(key).unwrap().map(Arc::new))
228+
}
213229
GetScope::InBlock => true,
214230
});
215231

graph/src/schema/entity_type.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ impl EntityType {
150150
pub fn is_object_type(&self) -> bool {
151151
self.schema.is_object_type(self.atom)
152152
}
153+
154+
// Changes the way the VID field is generated. It used to be autoincrement. Now its
155+
// based on block number and the order of the entities in a block. The latter
156+
// represents the write order across all entity types in the subgraph.
157+
pub fn strict_vid_order(&self) -> bool {
158+
// Currently the agregations entities don't have VIDs in insertion order
159+
self.schema.strict_vid_order() && self.is_object_type()
160+
}
153161
}
154162

155163
impl fmt::Display for EntityType {

graph/src/schema/input/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::data::graphql::{DirectiveExt, DocumentExt, ObjectTypeExt, TypeExt, Va
1717
use crate::data::store::{
1818
self, EntityValidationError, IdType, IntoEntityIterator, TryIntoEntityIterator, ValueType, ID,
1919
};
20+
use crate::data::subgraph::SPEC_VERSION_1_3_0;
2021
use crate::data::value::Word;
2122
use crate::derive::CheapClone;
2223
use crate::prelude::q::Value;
@@ -955,6 +956,7 @@ pub struct Inner {
955956
pool: Arc<AtomPool>,
956957
/// A list of all timeseries types by interval
957958
agg_mappings: Box<[AggregationMapping]>,
959+
spec_version: Version,
958960
}
959961

960962
impl InputSchema {
@@ -1042,6 +1044,7 @@ impl InputSchema {
10421044
enum_map,
10431045
pool,
10441046
agg_mappings,
1047+
spec_version: spec_version.clone(),
10451048
}),
10461049
})
10471050
}
@@ -1585,6 +1588,10 @@ impl InputSchema {
15851588
}?;
15861589
Some(EntityType::new(self.cheap_clone(), obj_type.name))
15871590
}
1591+
1592+
pub fn strict_vid_order(&self) -> bool {
1593+
self.inner.spec_version >= SPEC_VERSION_1_3_0
1594+
}
15881595
}
15891596

15901597
/// Create a new pool that contains the names of all the types defined

store/postgres/src/deployment.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use diesel::{
1313
sql_query,
1414
sql_types::{Nullable, Text},
1515
};
16+
use graph::semver::Version;
1617
use graph::{
1718
blockchain::block_stream::FirehoseCursor,
1819
data::subgraph::schema::SubgraphError,
@@ -305,11 +306,13 @@ pub fn debug_fork(
305306

306307
pub fn schema(conn: &mut PgConnection, site: &Site) -> Result<(InputSchema, bool), StoreError> {
307308
use subgraph_manifest as sm;
308-
let (s, use_bytea_prefix) = sm::table
309-
.select((sm::schema, sm::use_bytea_prefix))
309+
let (s, spec_ver, use_bytea_prefix) = sm::table
310+
.select((sm::schema, sm::spec_version, sm::use_bytea_prefix))
310311
.filter(sm::id.eq(site.id))
311-
.first::<(String, bool)>(conn)?;
312-
InputSchema::parse_latest(s.as_str(), site.deployment.clone())
312+
.first::<(String, String, bool)>(conn)?;
313+
let spec_version =
314+
Version::parse(spec_ver.as_str()).map_err(|err| StoreError::Unknown(err.into()))?;
315+
InputSchema::parse(&spec_version, s.as_str(), site.deployment.clone())
313316
.map_err(StoreError::Unknown)
314317
.map(|schema| (schema, use_bytea_prefix))
315318
}

store/postgres/src/relational/ddl.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ impl Table {
116116
Ok(cols)
117117
}
118118

119-
// Currently the agregations entities don't have VIDs in insertion order
120-
let vid_type = if self.object.is_object_type() {
119+
let vid_type = if self.object.strict_vid_order() {
121120
"bigint"
122121
} else {
123122
"bigserial"

store/postgres/src/relational/prune.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ impl TablePair {
243243

244244
let vid_seq = format!("{}_{VID_COLUMN}_seq", self.src.name);
245245

246-
let old_vid_form = !self.src.object.is_object_type();
247246
let mut query = String::new();
248247

249248
// What we are about to do would get blocked by autovacuum on our
@@ -253,9 +252,9 @@ impl TablePair {
253252
"src" => src_nsp.as_str(), "error" => e.to_string());
254253
}
255254

256-
// Make sure the vid sequence
257-
// continues from where it was
258-
if old_vid_form {
255+
// Make sure the vid sequence continues from where it was in case
256+
// that we use autoincrementing order of the DB
257+
if !self.src.object.strict_vid_order() {
259258
writeln!(
260259
query,
261260
"select setval('{dst_nsp}.{vid_seq}', nextval('{src_nsp}.{vid_seq}'));"

store/postgres/src/relational_queries.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,7 +2377,7 @@ impl<'a> QueryFragment<Pg> for InsertQuery<'a> {
23772377
let out = &mut out;
23782378
out.unsafe_to_cache_prepared();
23792379

2380-
let new_vid_form = self.table.object.is_object_type();
2380+
let strict_vid_order = self.table.object.strict_vid_order();
23812381

23822382
// Construct a query
23832383
// insert into schema.table(column, ...)
@@ -2404,7 +2404,7 @@ impl<'a> QueryFragment<Pg> for InsertQuery<'a> {
24042404
out.push_sql(CAUSALITY_REGION_COLUMN);
24052405
};
24062406

2407-
if new_vid_form {
2407+
if strict_vid_order {
24082408
out.push_sql(", vid");
24092409
}
24102410
out.push_sql(") values\n");
@@ -2424,7 +2424,7 @@ impl<'a> QueryFragment<Pg> for InsertQuery<'a> {
24242424
out.push_sql(", ");
24252425
out.push_bind_param::<Integer, _>(&row.causality_region)?;
24262426
};
2427-
if new_vid_form {
2427+
if strict_vid_order {
24282428
out.push_sql(", ");
24292429
out.push_bind_param::<BigInt, _>(&row.vid)?;
24302430
}
@@ -4827,7 +4827,7 @@ impl<'a> QueryFragment<Pg> for CopyEntityBatchQuery<'a> {
48274827
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> {
48284828
out.unsafe_to_cache_prepared();
48294829

4830-
let new_vid_form = self.src.object.is_object_type();
4830+
let strict_vid_order = self.src.object.strict_vid_order();
48314831

48324832
// Construct a query
48334833
// insert into {dst}({columns})
@@ -4849,7 +4849,7 @@ impl<'a> QueryFragment<Pg> for CopyEntityBatchQuery<'a> {
48494849
out.push_sql(", ");
48504850
out.push_sql(CAUSALITY_REGION_COLUMN);
48514851
};
4852-
if new_vid_form {
4852+
if strict_vid_order {
48534853
out.push_sql(", vid");
48544854
}
48554855

@@ -4917,7 +4917,7 @@ impl<'a> QueryFragment<Pg> for CopyEntityBatchQuery<'a> {
49174917
));
49184918
}
49194919
}
4920-
if new_vid_form {
4920+
if strict_vid_order {
49214921
out.push_sql(", vid");
49224922
}
49234923

store/test-store/src/store.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub async fn create_subgraph(
163163

164164
let manifest = SubgraphManifest::<graph::blockchain::mock::MockBlockchain> {
165165
id: subgraph_id.clone(),
166-
spec_version: Version::new(1, 0, 0),
166+
spec_version: Version::new(1, 3, 0),
167167
features: BTreeSet::new(),
168168
description: Some(format!("manifest for {}", subgraph_id)),
169169
repository: Some(format!("repo for {}", subgraph_id)),
@@ -227,7 +227,7 @@ pub async fn create_test_subgraph_with_features(
227227

228228
let manifest = SubgraphManifest::<graph::blockchain::mock::MockBlockchain> {
229229
id: subgraph_id.clone(),
230-
spec_version: Version::new(1, 0, 0),
230+
spec_version: Version::new(1, 3, 0),
231231
features,
232232
description: Some(format!("manifest for {}", subgraph_id)),
233233
repository: Some(format!("repo for {}", subgraph_id)),

store/test-store/tests/graph/entity_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ where
448448
async fn insert_test_data(store: Arc<DieselSubgraphStore>) -> DeploymentLocator {
449449
let manifest = SubgraphManifest::<graph_chain_ethereum::Chain> {
450450
id: LOAD_RELATED_ID.clone(),
451-
spec_version: Version::new(1, 0, 0),
451+
spec_version: Version::new(1, 3, 0),
452452
features: Default::default(),
453453
description: None,
454454
repository: None,

store/test-store/tests/postgres/graft.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ where
136136
async fn insert_test_data(store: Arc<DieselSubgraphStore>) -> DeploymentLocator {
137137
let manifest = SubgraphManifest::<graph_chain_ethereum::Chain> {
138138
id: TEST_SUBGRAPH_ID.clone(),
139-
spec_version: Version::new(1, 0, 0),
139+
spec_version: Version::new(1, 3, 0),
140140
features: Default::default(),
141141
description: None,
142142
repository: None,

0 commit comments

Comments
 (0)