Skip to content

Commit 6d3de7e

Browse files
store, node: Pass drop_schema flag into rebuild_storage
Rather than always running DROP SCHEMA IF EXISTS unconditionally, the command layer passes the result of has_namespace as a drop_schema bool down through BlockStore::rebuild_chain_storage into ChainStore::rebuild_storage. When true, the existing Storage::drop_storage is reused; when false, the drop is skipped entirely.
1 parent df3a38b commit 6d3de7e

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

node/src/manager/commands/chain.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ pub async fn rebuild_storage(
362362
let shard = &chain.shard;
363363
let ident = chain.network_identifier()?;
364364

365-
if store.has_namespace(&chain).await? {
365+
let drop_schema = store.has_namespace(&chain).await?;
366+
if drop_schema {
366367
let prompt = format!(
367368
"Storage {namespace} for chain {name} already exists on shard {shard}.\n\
368369
This will drop and rebuild chain storage. All cached blocks and call cache \
@@ -376,7 +377,7 @@ pub async fn rebuild_storage(
376377
}
377378

378379
store
379-
.rebuild_chain_storage(&name, &ident)
380+
.rebuild_chain_storage(&name, &ident, drop_schema)
380381
.await
381382
.with_context(|| format!("Failed to rebuild storage {namespace} for chain {name}"))?;
382383

store/postgres/src/block_store.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,13 +498,14 @@ impl BlockStore {
498498
&self,
499499
chain: &str,
500500
ident: &ChainIdentifier,
501+
drop_schema: bool,
501502
) -> Result<(), StoreError> {
502503
let chain_store = self
503504
.store(chain)
504505
.await
505506
.ok_or_else(|| internal_error!("No chain store found for {}", chain))?;
506507

507-
Ok(chain_store.rebuild_storage(ident).await?)
508+
Ok(chain_store.rebuild_storage(ident, drop_schema).await?)
508509
}
509510

510511
// Helper to clone the list of chain stores to avoid holding the lock

store/postgres/src/chain_store.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use async_trait::async_trait;
33
use diesel::sql_types::Text;
44
use diesel::{insert_into, update, ExpressionMethods, OptionalExtension, QueryDsl};
55
use diesel_async::AsyncConnection;
6-
use diesel_async::{scoped_futures::ScopedFutureExt, RunQueryDsl, SimpleAsyncConnection};
6+
use diesel_async::{scoped_futures::ScopedFutureExt, RunQueryDsl};
77

88
use graph::components::store::ChainHeadStore;
99
use graph::data::store::ethereum::call;
@@ -2483,7 +2483,11 @@ impl ChainStore {
24832483
/// metadata in `ethereum_networks`, and rebuild the schema with
24842484
/// empty tables. If the `ethereum_networks` row is missing, it is
24852485
/// created from the provided `ident`.
2486-
pub(crate) async fn rebuild_storage(&self, ident: &ChainIdentifier) -> Result<(), Error> {
2486+
pub(crate) async fn rebuild_storage(
2487+
&self,
2488+
ident: &ChainIdentifier,
2489+
drop_schema: bool,
2490+
) -> Result<(), Error> {
24872491
use public::ethereum_networks as n;
24882492

24892493
let nsp = self.storage.to_string();
@@ -2493,9 +2497,10 @@ impl ChainStore {
24932497
let mut conn = self.pool.get_permitted().await?;
24942498
conn.transaction(|conn| {
24952499
async {
2496-
debug!(&self.logger, "Dropping existing schema if present"; "namespace" => &nsp);
2497-
conn.batch_execute(&format!("DROP SCHEMA IF EXISTS {nsp} CASCADE"))
2498-
.await?;
2500+
if drop_schema {
2501+
debug!(&self.logger, "Dropping existing schema"; "namespace" => &nsp);
2502+
self.storage.drop_storage(conn, &self.chain).await?;
2503+
}
24992504

25002505
debug!(&self.logger, "Upserting ethereum_networks row"; "chain" => &self.chain);
25012506
insert_into(n::table)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ fn rebuild_storage_with_existing_namespace() {
712712

713713
// Rebuild storage (should drop and recreate)
714714
block_store
715-
.rebuild_chain_storage(NETWORK_NAME, &ident)
715+
.rebuild_chain_storage(NETWORK_NAME, &ident, true)
716716
.await
717717
.expect("rebuild_chain_storage succeeds");
718718

@@ -775,7 +775,7 @@ fn rebuild_storage_with_missing_namespace() {
775775

776776
// Rebuild should recreate the missing namespace
777777
block_store
778-
.rebuild_chain_storage(NETWORK_NAME, &ident)
778+
.rebuild_chain_storage(NETWORK_NAME, &ident, false)
779779
.await
780780
.expect("rebuild_chain_storage succeeds on missing namespace");
781781

@@ -829,7 +829,7 @@ fn rebuild_storage_repairs_ethereum_networks_row() {
829829
// Rebuild should recreate both the namespace and the
830830
// ethereum_networks row via upsert
831831
block_store
832-
.rebuild_chain_storage(NETWORK_NAME, &ident)
832+
.rebuild_chain_storage(NETWORK_NAME, &ident, false)
833833
.await
834834
.expect("rebuild_chain_storage succeeds with missing ethereum_networks row");
835835

@@ -880,7 +880,7 @@ fn has_namespace_returns_false_for_missing_schema() {
880880
// Rebuild to leave things clean for other tests
881881
let ident = chain.network_identifier().unwrap();
882882
block_store
883-
.rebuild_chain_storage(NETWORK_NAME, &ident)
883+
.rebuild_chain_storage(NETWORK_NAME, &ident, false)
884884
.await
885885
.unwrap();
886886
});

0 commit comments

Comments
 (0)