@@ -3,7 +3,7 @@ use async_trait::async_trait;
33use diesel:: sql_types:: Text ;
44use diesel:: { insert_into, update, ExpressionMethods , OptionalExtension , QueryDsl } ;
55use diesel_async:: AsyncConnection ;
6- use diesel_async:: { scoped_futures:: ScopedFutureExt , RunQueryDsl } ;
6+ use diesel_async:: { scoped_futures:: ScopedFutureExt , RunQueryDsl , SimpleAsyncConnection } ;
77
88use graph:: components:: store:: ChainHeadStore ;
99use graph:: data:: store:: ethereum:: call;
@@ -12,7 +12,7 @@ use graph::parking_lot::RwLock;
1212use graph:: prelude:: alloy:: primitives:: B256 ;
1313use graph:: prelude:: MetricsRegistry ;
1414use graph:: prometheus:: { CounterVec , GaugeVec } ;
15- use graph:: slog:: { info, o, Logger } ;
15+ use graph:: slog:: { debug , info, o, Logger } ;
1616use graph:: stable_hash:: crypto_stable_hash;
1717use graph:: util:: herd_cache:: HerdCache ;
1818
@@ -2479,6 +2479,55 @@ impl ChainStore {
24792479 . await
24802480 }
24812481
2482+ /// Drop the chain's storage schema (if it exists), reset head
2483+ /// metadata in `ethereum_networks`, and rebuild the schema with
2484+ /// empty tables. If the `ethereum_networks` row is missing, it is
2485+ /// created from the provided `ident`.
2486+ pub ( crate ) async fn rebuild_storage ( & self , ident : & ChainIdentifier ) -> Result < ( ) , Error > {
2487+ use public:: ethereum_networks as n;
2488+
2489+ let nsp = self . storage . to_string ( ) ;
2490+
2491+ debug ! ( & self . logger, "Rebuilding storage for chain" ; "chain" => & self . chain, "namespace" => & nsp) ;
2492+
2493+ let mut conn = self . pool . get_permitted ( ) . await ?;
2494+ conn. transaction ( |conn| {
2495+ 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 ?;
2499+
2500+ debug ! ( & self . logger, "Upserting ethereum_networks row" ; "chain" => & self . chain) ;
2501+ insert_into ( n:: table)
2502+ . values ( (
2503+ n:: name. eq ( & self . chain ) ,
2504+ n:: namespace. eq ( & self . storage ) ,
2505+ n:: net_version. eq ( & ident. net_version ) ,
2506+ n:: genesis_block_hash. eq ( ident. genesis_block_hash . hash_hex ( ) ) ,
2507+ ) )
2508+ . on_conflict ( n:: name)
2509+ . do_update ( )
2510+ . set ( (
2511+ n:: namespace. eq ( & self . storage ) ,
2512+ n:: net_version. eq ( & ident. net_version ) ,
2513+ n:: genesis_block_hash. eq ( ident. genesis_block_hash . hash_hex ( ) ) ,
2514+ n:: head_block_hash. eq ( None :: < String > ) ,
2515+ n:: head_block_number. eq ( None :: < i64 > ) ,
2516+ n:: head_block_cursor. eq ( None :: < String > ) ,
2517+ ) )
2518+ . execute ( conn)
2519+ . await ?;
2520+
2521+ debug ! ( & self . logger, "Creating storage schema and tables" ; "namespace" => & nsp) ;
2522+ self . storage . create ( conn) . await ?;
2523+
2524+ Ok ( ( ) )
2525+ }
2526+ . scope_boxed ( )
2527+ } )
2528+ . await
2529+ }
2530+
24822531 pub async fn chain_head_pointers (
24832532 conn : & mut AsyncPgConnection ,
24842533 ) -> Result < HashMap < String , BlockPtr > , StoreError > {
0 commit comments