Skip to content

Commit

Permalink
store: Move has_causality_region to manifest, rename to `entities_w…
Browse files Browse the repository at this point in the history
…ith_causality_region`
  • Loading branch information
leoyvens committed Nov 28, 2022
1 parent faaf8f4 commit 02d0d12
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 38 deletions.
2 changes: 1 addition & 1 deletion core/src/subgraph/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ async fn create_subgraph_version<C: Blockchain, S: SubgraphStore>(
let deployment = DeploymentCreate::new(raw_string, &manifest, start_block)
.graft(base_block)
.debug(debug_fork)
.has_causality_region(needs_causality_region);
.entities_with_causality_region(needs_causality_region);

deployment_store
.create_subgraph_deployment(
Expand Down
21 changes: 14 additions & 7 deletions graph/src/data/subgraph/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ pub struct DeploymentCreate {
pub graft_base: Option<DeploymentHash>,
pub graft_block: Option<BlockPtr>,
pub debug_fork: Option<DeploymentHash>,
pub has_causality_region: BTreeSet<EntityType>,
}

impl DeploymentCreate {
Expand All @@ -117,12 +116,11 @@ impl DeploymentCreate {
start_block: Option<BlockPtr>,
) -> Self {
Self {
manifest: SubgraphManifestEntity::new(raw_manifest, source_manifest),
manifest: SubgraphManifestEntity::new(raw_manifest, source_manifest, Vec::new()),
start_block: start_block.cheap_clone(),
graft_base: None,
graft_block: None,
debug_fork: None,
has_causality_region: BTreeSet::new(),
}
}

Expand All @@ -139,8 +137,12 @@ impl DeploymentCreate {
self
}

pub fn has_causality_region(mut self, has_causality_region: BTreeSet<EntityType>) -> Self {
self.has_causality_region = has_causality_region;
pub fn entities_with_causality_region(
mut self,
entities_with_causality_region: BTreeSet<EntityType>,
) -> Self {
self.manifest.entities_with_causality_region =
entities_with_causality_region.into_iter().collect();
self
}
}
Expand All @@ -166,7 +168,6 @@ pub struct SubgraphDeploymentEntity {
pub reorg_count: i32,
pub current_reorg_depth: i32,
pub max_reorg_depth: i32,
pub has_causality_region: Vec<EntityType>,
}

#[derive(Debug)]
Expand All @@ -177,17 +178,23 @@ pub struct SubgraphManifestEntity {
pub features: Vec<String>,
pub schema: String,
pub raw_yaml: Option<String>,
pub entities_with_causality_region: Vec<EntityType>,
}

impl SubgraphManifestEntity {
pub fn new(raw_yaml: String, manifest: &super::SubgraphManifest<impl Blockchain>) -> Self {
pub fn new(
raw_yaml: String,
manifest: &super::SubgraphManifest<impl Blockchain>,
entities_with_causality_region: Vec<EntityType>,
) -> Self {
Self {
spec_version: manifest.spec_version.to_string(),
description: manifest.description.clone(),
repository: manifest.repository.clone(),
features: manifest.features.iter().map(|f| f.to_string()).collect(),
schema: manifest.schema.document.clone().to_string(),
raw_yaml: Some(raw_yaml),
entities_with_causality_region,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
alter table subgraphs.subgraph_deployment drop column has_causality_region;
alter table subgraphs.subgraph_manifest drop column entities_with_causality_region;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
alter table subgraphs.subgraph_deployment add column has_causality_region text[] not null default array[]::text[];
alter table subgraphs.subgraph_manifest add column entities_with_causality_region text[] not null default array[]::text[];
17 changes: 10 additions & 7 deletions store/postgres/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ pub struct Catalog {
pub use_bytea_prefix: bool,

/// Set of tables which have an explicit causality region column.
pub(crate) has_causality_region: BTreeSet<EntityType>,
pub(crate) entities_with_causality_region: BTreeSet<EntityType>,
}

impl Catalog {
Expand All @@ -195,7 +195,7 @@ impl Catalog {
conn: &PgConnection,
site: Arc<Site>,
use_bytea_prefix: bool,
has_causality_region: Vec<EntityType>,
entities_with_causality_region: Vec<EntityType>,
) -> Result<Self, StoreError> {
let text_columns = get_text_columns(conn, &site.namespace)?;
let use_poi = supports_proof_of_indexing(conn, &site.namespace)?;
Expand All @@ -204,12 +204,15 @@ impl Catalog {
text_columns,
use_poi,
use_bytea_prefix,
has_causality_region: has_causality_region.into_iter().collect(),
entities_with_causality_region: entities_with_causality_region.into_iter().collect(),
})
}

/// Return a new catalog suitable for creating a new subgraph
pub fn for_creation(site: Arc<Site>, has_causality_region: BTreeSet<EntityType>) -> Self {
pub fn for_creation(
site: Arc<Site>,
entities_with_causality_region: BTreeSet<EntityType>,
) -> Self {
Catalog {
site,
text_columns: HashMap::default(),
Expand All @@ -218,7 +221,7 @@ impl Catalog {
// DDL generation creates indexes for prefixes of bytes columns
// see: attr-bytea-prefix
use_bytea_prefix: true,
has_causality_region,
entities_with_causality_region,
}
}

Expand All @@ -227,14 +230,14 @@ impl Catalog {
/// connection is definitely not available, such as in unit tests
pub fn for_tests(
site: Arc<Site>,
has_causality_region: BTreeSet<EntityType>,
entities_with_causality_region: BTreeSet<EntityType>,
) -> Result<Self, StoreError> {
Ok(Catalog {
site,
text_columns: HashMap::default(),
use_poi: false,
use_bytea_prefix: true,
has_causality_region,
entities_with_causality_region,
})
}

Expand Down
24 changes: 12 additions & 12 deletions store/postgres/src/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ table! {
current_reorg_depth -> Integer,
max_reorg_depth -> Integer,
firehose_cursor -> Nullable<Text>,

// Entity types that have a `causality_region` column.
// Names stored as present in the schema, not in snake case.
has_causality_region -> Array<Text>,
}
}

Expand Down Expand Up @@ -121,6 +117,10 @@ table! {
start_block_number -> Nullable<Integer>,
start_block_hash -> Nullable<Binary>,
raw_yaml -> Nullable<Text>,

// Entity types that have a `causality_region` column.
// Names stored as present in the schema, not in snake case.
entities_with_causality_region -> Array<Text>,
}
}

Expand Down Expand Up @@ -776,15 +776,15 @@ pub(crate) fn health(conn: &PgConnection, id: DeploymentId) -> Result<SubgraphHe
.map_err(|e| e.into())
}

pub(crate) fn has_causality_region(
pub(crate) fn entities_with_causality_region(
conn: &PgConnection,
id: DeploymentId,
) -> Result<Vec<EntityType>, StoreError> {
use subgraph_deployment as d;
use subgraph_manifest as sm;

d::table
.filter(d::id.eq(id))
.select(d::has_causality_region)
sm::table
.filter(sm::id.eq(id))
.select(sm::entities_with_causality_region)
.get_result(conn)
.map_err(|e| e.into())
}
Expand Down Expand Up @@ -931,15 +931,15 @@ pub fn create_deployment(
features,
schema,
raw_yaml,
entities_with_causality_region,
},
start_block,
graft_base,
graft_block,
debug_fork,
has_causality_region,
} = deployment;
let earliest_block_number = start_block.as_ref().map(|ptr| ptr.number).unwrap_or(0);
let has_causality_region = Vec::from_iter(has_causality_region.into_iter());
let entities_with_causality_region = Vec::from_iter(entities_with_causality_region.into_iter());

let deployment_values = (
d::id.eq(site.id),
Expand All @@ -957,7 +957,6 @@ pub fn create_deployment(
d::graft_block_hash.eq(b(&graft_block)),
d::graft_block_number.eq(n(&graft_block)),
d::debug_fork.eq(debug_fork.as_ref().map(|s| s.as_str())),
d::has_causality_region.eq(has_causality_region),
);

let graph_node_version_id = GraphNodeVersion::create_or_get(conn)?;
Expand All @@ -976,6 +975,7 @@ pub fn create_deployment(
m::start_block_hash.eq(b(&start_block)),
m::start_block_number.eq(start_block.as_ref().map(|ptr| ptr.number)),
m::raw_yaml.eq(raw_yaml),
m::entities_with_causality_region.eq(entities_with_causality_region),
);

if exists && replace {
Expand Down
5 changes: 3 additions & 2 deletions store/postgres/src/deployment_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ impl DeploymentStore {
let exists = deployment::exists(&conn, &site)?;

// Create (or update) the metadata. Update only happens in tests
let has_causality_region = deployment.has_causality_region.clone();
let entities_with_causality_region =
deployment.manifest.entities_with_causality_region.clone();
if replace || !exists {
deployment::create_deployment(&conn, &site, deployment, exists, replace)?;
};
Expand All @@ -188,7 +189,7 @@ impl DeploymentStore {
&conn,
site.clone(),
schema,
has_causality_region,
entities_with_causality_region.into_iter().collect(),
)?;
// See if we are grafting and check that the graft is permissible
if let Some(base) = graft_base {
Expand Down
4 changes: 2 additions & 2 deletions store/postgres/src/detail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ pub struct DeploymentDetail {
current_reorg_depth: i32,
max_reorg_depth: i32,
firehose_cursor: Option<String>,
has_causality_region: Vec<EntityType>,
}

#[derive(Queryable, QueryableByName)]
Expand Down Expand Up @@ -340,6 +339,7 @@ struct StoredSubgraphManifest {
start_block_number: Option<i32>,
start_block_hash: Option<Bytes>,
raw_yaml: Option<String>,
entities_with_causality_region: Vec<EntityType>,
}

impl From<StoredSubgraphManifest> for SubgraphManifestEntity {
Expand All @@ -351,6 +351,7 @@ impl From<StoredSubgraphManifest> for SubgraphManifestEntity {
features: value.features,
schema: value.schema,
raw_yaml: value.raw_yaml,
entities_with_causality_region: value.entities_with_causality_region,
}
}
}
Expand Down Expand Up @@ -415,7 +416,6 @@ impl TryFrom<StoredDeploymentEntity> for SubgraphDeploymentEntity {
reorg_count: detail.reorg_count,
current_reorg_depth: detail.current_reorg_depth,
max_reorg_depth: detail.max_reorg_depth,
has_causality_region: detail.has_causality_region,
})
}
}
Expand Down
8 changes: 4 additions & 4 deletions store/postgres/src/relational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl Layout {
&id_types,
i as u32,
catalog
.has_causality_region
.entities_with_causality_region
.contains(&EntityType::from(obj_type.clone())),
)
})
Expand Down Expand Up @@ -409,9 +409,9 @@ impl Layout {
conn: &PgConnection,
site: Arc<Site>,
schema: &Schema,
has_causality_region: BTreeSet<EntityType>,
entities_with_causality_region: BTreeSet<EntityType>,
) -> Result<Layout, StoreError> {
let catalog = Catalog::for_creation(site.cheap_clone(), has_causality_region);
let catalog = Catalog::for_creation(site.cheap_clone(), entities_with_causality_region);
let layout = Self::new(site, schema, catalog)?;
let sql = layout
.as_ddl()
Expand Down Expand Up @@ -1389,7 +1389,7 @@ impl LayoutCache {

fn load(conn: &PgConnection, site: Arc<Site>) -> Result<Arc<Layout>, StoreError> {
let (subgraph_schema, use_bytea_prefix) = deployment::schema(conn, site.as_ref())?;
let has_causality_region = deployment::has_causality_region(conn, site.id)?;
let has_causality_region = deployment::entities_with_causality_region(conn, site.id)?;
let catalog = Catalog::load(conn, site.clone(), use_bytea_prefix, has_causality_region)?;
let layout = Arc::new(Layout::new(site.clone(), &subgraph_schema, catalog)?);
layout.refresh(conn, site)
Expand Down
1 change: 0 additions & 1 deletion store/postgres/src/subgraph_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,6 @@ impl SubgraphStoreInner {
graft_base: Some(src.deployment.clone()),
graft_block: Some(block),
debug_fork: deployment.debug_fork,
has_causality_region: deployment.has_causality_region.into_iter().collect(),
};

let graft_base = self.layout(&src.deployment)?;
Expand Down

0 comments on commit 02d0d12

Please sign in to comment.