@@ -12,7 +12,6 @@ use std::sync::Arc;
1212
1313use diesel:: dsl:: update;
1414use diesel:: prelude:: { ExpressionMethods , QueryDsl } ;
15- use diesel:: sql_types:: { BigInt , Binary , Integer , Nullable , Text } ;
1615use diesel_async:: { RunQueryDsl , SimpleAsyncConnection } ;
1716use graph:: blockchain:: BlockHash ;
1817use graph:: data:: subgraph:: schema:: { DeploymentCreate , SubgraphManifestEntity } ;
@@ -23,9 +22,7 @@ use graph::semver::Version;
2322use crate :: catalog;
2423use crate :: deployment:: create_deployment;
2524use crate :: dynds:: DataSourcesTable ;
26- use crate :: parquet:: convert:: {
27- record_batch_to_data_source_rows, record_batch_to_restore_rows, DataSourceRestoreRow ,
28- } ;
25+ use crate :: parquet:: convert:: { record_batch_to_data_source_rows, record_batch_to_restore_rows} ;
2926use crate :: parquet:: reader:: read_batches;
3027use crate :: primary:: Site ;
3128use crate :: relational:: dump:: { Metadata , TableInfo } ;
@@ -133,39 +130,10 @@ async fn import_entity_table(
133130 Ok ( total_inserted)
134131}
135132
136- /// Insert a single data_sources$ row via raw SQL.
137- async fn insert_data_source_row (
138- conn : & mut AsyncPgConnection ,
139- qualified_table : & str ,
140- row : & DataSourceRestoreRow ,
141- ) -> Result < ( ) , StoreError > {
142- let query = format ! (
143- "INSERT INTO {} (vid, block_range, causality_region, manifest_idx, \
144- parent, id, param, context, done_at) \
145- VALUES ($1, int4range($2, $3), $4, $5, $6, $7, $8, $9::jsonb, $10)",
146- qualified_table,
147- ) ;
148- diesel:: sql_query ( & query)
149- . bind :: < BigInt , _ > ( row. vid )
150- . bind :: < Integer , _ > ( row. block_range_start )
151- . bind :: < Nullable < Integer > , _ > ( row. block_range_end )
152- . bind :: < Integer , _ > ( row. causality_region )
153- . bind :: < Integer , _ > ( row. manifest_idx )
154- . bind :: < Nullable < Integer > , _ > ( row. parent )
155- . bind :: < Nullable < Binary > , _ > ( row. id . as_deref ( ) )
156- . bind :: < Nullable < Binary > , _ > ( row. param . as_deref ( ) )
157- . bind :: < Nullable < Text > , _ > ( row. context . as_deref ( ) )
158- . bind :: < Nullable < Integer > , _ > ( row. done_at )
159- . execute ( conn)
160- . await
161- . map_err ( StoreError :: from) ?;
162- Ok ( ( ) )
163- }
164-
165133/// Import the `data_sources$` table from Parquet chunks.
166134async fn import_data_sources (
167135 conn : & mut AsyncPgConnection ,
168- namespace : & str ,
136+ ds_table : & DataSourcesTable ,
169137 table_info : & TableInfo ,
170138 dir : & Path ,
171139 logger : & Logger ,
@@ -174,8 +142,8 @@ async fn import_data_sources(
174142 return Ok ( 0 ) ;
175143 }
176144
177- let qualified = format ! ( " \" {} \" . \" {DATA_SOURCES_TABLE} \" " , namespace ) ;
178- let max_vid_db = current_max_vid ( conn, & qualified) . await ?;
145+ let qualified = ds_table . qualified_name ( ) ;
146+ let max_vid_db = current_max_vid ( conn, qualified) . await ?;
179147 if max_vid_db >= table_info. max_vid {
180148 info ! ( logger, "data_sources$ already fully restored, skipping" ) ;
181149 return Ok ( 0 ) ;
@@ -193,15 +161,13 @@ async fn import_data_sources(
193161
194162 for batch in batches {
195163 let batch = batch?;
196- let rows = record_batch_to_data_source_rows ( & batch) ?;
197-
198- for row in & rows {
199- if max_vid_db >= 0 && row. vid <= max_vid_db {
200- continue ;
201- }
202- insert_data_source_row ( conn, & qualified, row) . await ?;
203- total_inserted += 1 ;
164+ let mut rows = record_batch_to_data_source_rows ( & batch) ?;
165+
166+ if max_vid_db >= 0 {
167+ rows. retain ( |row| row. vid > max_vid_db) ;
204168 }
169+
170+ total_inserted += ds_table. insert_rows ( conn, & rows) . await ?;
205171 }
206172 }
207173
@@ -352,8 +318,8 @@ pub async fn import_data(
352318
353319 // Import data_sources$ if present
354320 if let Some ( ds_info) = metadata. tables . get ( DATA_SOURCES_TABLE ) {
355- let namespace = layout. site . namespace . as_str ( ) ;
356- import_data_sources ( conn, namespace , ds_info, dir, logger) . await ?;
321+ let ds_table = DataSourcesTable :: new ( layout. site . namespace . clone ( ) ) ;
322+ import_data_sources ( conn, & ds_table , ds_info, dir, logger) . await ?;
357323 }
358324
359325 Ok ( ( ) )
0 commit comments