Skip to content

Commit

Permalink
fix: still generate internal tables for rindexer even if creating new…
Browse files Browse the repository at this point in the history
… event tables is disabled
  • Loading branch information
joshstevens19 committed Sep 17, 2024
1 parent 566e087 commit c4fd173
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
32 changes: 20 additions & 12 deletions core/src/database/postgres/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ fn find_clashing_event_names(
pub fn generate_tables_for_indexer_sql(
project_path: &Path,
indexer: &Indexer,
disable_event_tables: bool,
) -> Result<Code, GenerateTablesForIndexerSqlError> {
let mut sql = "CREATE SCHEMA IF NOT EXISTS rindexer_internal;".to_string();

Expand All @@ -167,20 +168,27 @@ pub fn generate_tables_for_indexer_sql(
let abi_items = ABIItem::read_abi_items(project_path, contract)?;
let event_names = ABIItem::extract_event_names_and_signatures_from_abi(abi_items)?;
let schema_name = generate_indexer_contract_schema_name(&indexer.name, &contract_name);
sql.push_str(format!("CREATE SCHEMA IF NOT EXISTS {};", schema_name).as_str());
info!("Creating schema if not exists: {}", schema_name);

let networks: Vec<&str> = contract.details.iter().map(|d| d.network.as_str()).collect();

let event_matching_name_on_other =
find_clashing_event_names(project_path, contract, &indexer.contracts, &event_names)?;

sql.push_str(&generate_event_table_sql_with_comments(
&event_names,
&contract.name,
&schema_name,
event_matching_name_on_other,
));
if !disable_event_tables {
sql.push_str(format!("CREATE SCHEMA IF NOT EXISTS {};", schema_name).as_str());
info!("Creating schema if not exists: {}", schema_name);

let event_matching_name_on_other = find_clashing_event_names(
project_path,
contract,
&indexer.contracts,
&event_names,
)?;

sql.push_str(&generate_event_table_sql_with_comments(
&event_names,
&contract.name,
&schema_name,
event_matching_name_on_other,
));
}
// we still need to create the internal tables for the contract
sql.push_str(&generate_internal_event_table_sql(&event_names, &schema_name, networks));
}

Expand Down
42 changes: 25 additions & 17 deletions core/src/database/postgres/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,34 @@ pub async fn setup_postgres(
info!("Setting up postgres");
let client = PostgresClient::new().await?;

// No-code will ignore this as it must have tables if postgres used
if !manifest.storage.postgres_disable_create_tables() ||
manifest.project_type == ProjectType::NoCode
{
// check if we need to drop each run
if manifest.storage.postgres_drop_each_run() {
info!(
"`drop_each_run` enabled so dropping all data for {} before starting",
&manifest.name
);
let sql = drop_tables_for_indexer_sql(project_path, &manifest.to_indexer());
client.batch_execute(sql.as_str()).await?;
info!("Dropped all data for {}", manifest.name);
}
let disable_event_tables = manifest.storage.postgres_disable_create_tables();

if manifest.storage.postgres_drop_each_run() {
info!(
"`drop_each_run` enabled so dropping all data for {} before starting",
&manifest.name
);
let sql = drop_tables_for_indexer_sql(project_path, &manifest.to_indexer());
client.batch_execute(sql.as_str()).await?;
info!("Dropped all data for {}", manifest.name);
}

if !disable_event_tables {
info!("Creating tables for {}", manifest.name);
let sql = generate_tables_for_indexer_sql(project_path, &manifest.to_indexer())?;
debug!("{}", sql);
client.batch_execute(sql.as_str()).await?;
} else {
info!("Creating internal rindexer tables for {}", manifest.name);
}
let sql = generate_tables_for_indexer_sql(
project_path,
&manifest.to_indexer(),
disable_event_tables,
)?;
debug!("{}", sql);
client.batch_execute(sql.as_str()).await?;
if !disable_event_tables {
info!("Created tables for {}", manifest.name);
} else {
info!("Created internal rindexer tables for {}", manifest.name);
}

Ok(client)
Expand Down
1 change: 1 addition & 0 deletions documentation/docs/pages/docs/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
-------------------------------------------------
- fix: pascal case still has some edge cases on parsing
- fix: allow #![allow(non_snake_case)] in indexer code
- fix: still generate internal tables for rindexer even if creating new event tables is disabled

### Breaking changes
-------------------------------------------------
Expand Down

0 comments on commit c4fd173

Please sign in to comment.