Skip to content

Commit e2c8e98

Browse files
authored
node, store: Update db_version to version 3 at startup
Adds a startup operation to check if `db_version` is less than 3. If not, performs full cleanup on all block cache tables (using TRUNCATE TABLE) and then set the `db_version` to 3.
1 parent eb5e78f commit e2c8e98

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

node/src/store_builder.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,14 @@ impl StoreBuilder {
166166
)
167167
.expect("Creating the BlockStore works"),
168168
);
169+
block_store
170+
.update_db_version()
171+
.expect("Updating `db_version` works");
169172

170173
Arc::new(DieselStore::new(subgraph_store, block_store))
171174
}
172175

173-
/// Create a connection pool for the main database of hte primary shard
176+
/// Create a connection pool for the main database of the primary shard
174177
/// without connecting to all the other configured databases
175178
pub fn main_pool(
176179
logger: &Logger,

store/postgres/src/block_store.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use graph::{
2121

2222
use crate::{
2323
chain_head_listener::ChainHeadUpdateSender, connection_pool::ConnectionPool,
24-
primary::Mirror as PrimaryMirror, ChainStore, NotificationSender, Shard,
24+
primary::Mirror as PrimaryMirror, ChainStore, NotificationSender, Shard, PRIMARY_SHARD,
2525
};
2626

2727
#[cfg(debug_assertions)]
@@ -462,6 +462,29 @@ impl BlockStore {
462462

463463
Ok(())
464464
}
465+
466+
fn truncate_block_caches(&self) -> Result<(), StoreError> {
467+
for (_chain, store) in &*self.stores.read().unwrap() {
468+
store.truncate_block_cache()?
469+
}
470+
Ok(())
471+
}
472+
473+
pub fn update_db_version(&self) -> Result<(), StoreError> {
474+
use crate::primary::db_version as dbv;
475+
use diesel::prelude::*;
476+
477+
let primary_pool = self.pools.get(&*PRIMARY_SHARD).unwrap();
478+
let connection = primary_pool.get()?;
479+
let version: i64 = dbv::table.select(dbv::version).get_result(&connection)?;
480+
if version < 3 {
481+
self.truncate_block_caches()?;
482+
diesel::update(dbv::table)
483+
.set(dbv::version.eq(3))
484+
.execute(&connection)?;
485+
};
486+
Ok(())
487+
}
465488
}
466489

467490
impl BlockStoreTrait for BlockStore {

store/postgres/src/chain_store.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,15 @@ mod data {
390390
}
391391
}
392392

393+
pub(super) fn truncate_block_cache(&self, conn: &PgConnection) -> Result<(), StoreError> {
394+
let table_name = match &self {
395+
Storage::Shared => ETHEREUM_BLOCKS_TABLE_NAME,
396+
Storage::Private(Schema { blocks, .. }) => &blocks.qname,
397+
};
398+
conn.batch_execute(&format!("truncate table {} restart identity", table_name))?;
399+
Ok(())
400+
}
401+
393402
/// Insert a block. If the table already contains a block with the
394403
/// same hash, then overwrite that block since it may be adding
395404
/// transaction receipts. If `overwrite` is `true`, overwrite a
@@ -1264,6 +1273,12 @@ impl ChainStore {
12641273
self.storage
12651274
.set_chain(&conn, &self.chain, genesis_hash, chain);
12661275
}
1276+
1277+
pub fn truncate_block_cache(&self) -> Result<(), StoreError> {
1278+
let conn = self.get_conn()?;
1279+
self.storage.truncate_block_cache(&conn)?;
1280+
Ok(())
1281+
}
12671282
}
12681283

12691284
#[async_trait]

store/postgres/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub use self::chain_store::ChainStore;
6666
pub use self::detail::DeploymentDetail;
6767
pub use self::jobs::register as register_jobs;
6868
pub use self::notification_listener::NotificationSender;
69-
pub use self::primary::UnusedDeployment;
69+
pub use self::primary::{db_version, UnusedDeployment};
7070
pub use self::store::Store;
7171
pub use self::store_events::SubscriptionManager;
7272
pub use self::subgraph_store::{unused, DeploymentPlacer, Shard, SubgraphStore, PRIMARY_SHARD};

store/postgres/src/primary.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ table! {
171171
}
172172
}
173173

174+
table! {
175+
public.db_version(version) {
176+
#[sql_name = "db_version"]
177+
version -> BigInt,
178+
}
179+
}
180+
174181
allow_tables_to_appear_in_same_query!(
175182
subgraph,
176183
subgraph_version,

0 commit comments

Comments
 (0)