Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 91d8e54

Browse files
committed
store: Metadata updates must overwrite and not just amend
1 parent a1e9315 commit 91d8e54

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

store/postgres/src/entities.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,9 @@ impl Connection {
507507
}
508508
}
509509

510+
/// Overwrite an entity with a new version. The `history_event` indicates
511+
/// at which block the new version becomes valid if it is given. If it is
512+
/// `None`, the entity is treated as unversioned
510513
pub(crate) fn update(
511514
&self,
512515
key: &EntityKey,
@@ -522,7 +525,7 @@ impl Connection {
522525
layout.update(&self.conn, key, entity, block_number(&history_event))
523526
}
524527
None => layout
525-
.update_unversioned(&self.conn, key, &entity)
528+
.overwrite_unversioned(&self.conn, key, entity)
526529
.map(|_| ()),
527530
},
528531
}
@@ -537,7 +540,7 @@ impl Connection {
537540
) -> Result<usize, StoreError> {
538541
match &*self.metadata {
539542
Storage::Json(json) => json.update_metadata(&self.conn, key, entity),
540-
Storage::Relational(layout) => layout.update_unversioned(&self.conn, key, entity),
543+
Storage::Relational(layout) => layout.update_metadata(&self.conn, key, entity),
541544
}
542545
}
543546

store/postgres/src/relational.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use graph::data::schema::SCHEMA_TYPE_NAME;
2525
use graph::prelude::{
2626
format_err, trace, BlockNumber, Entity, EntityChange, EntityChangeOperation, EntityCollection,
2727
EntityFilter, EntityKey, EntityOrder, EntityRange, Logger, QueryExecutionError, StoreError,
28-
StoreEvent, SubgraphDeploymentId, ValueType,
28+
StoreEvent, SubgraphDeploymentId, Value, ValueType,
2929
};
3030

3131
use crate::block_range::{BLOCK_RANGE_COLUMN, BLOCK_UNVERSIONED};
@@ -472,7 +472,7 @@ impl Layout {
472472
Ok(())
473473
}
474474

475-
pub fn update_unversioned(
475+
pub fn update_metadata(
476476
&self,
477477
conn: &PgConnection,
478478
key: &EntityKey,
@@ -483,6 +483,24 @@ impl Layout {
483483
Ok(query.execute(conn)?)
484484
}
485485

486+
pub fn overwrite_unversioned(
487+
&self,
488+
conn: &PgConnection,
489+
key: &EntityKey,
490+
mut entity: Entity,
491+
) -> Result<usize, StoreError> {
492+
let table = self.table_for_entity(&key.entity_type)?;
493+
// Set any attributes not mentioned in the entity to
494+
// their default (NULL)
495+
for column in table.columns.iter() {
496+
if !entity.contains_key(&column.field) {
497+
entity.insert(column.field.clone(), Value::Null);
498+
}
499+
}
500+
let query = UpdateQuery::new(table, key, &entity)?;
501+
Ok(query.execute(conn)?)
502+
}
503+
486504
pub fn delete(
487505
&self,
488506
conn: &PgConnection,

0 commit comments

Comments
 (0)