Skip to content

Commit

Permalink
Merge pull request #56 from chaindexing/ensure-indices-are-removed-on…
Browse files Browse the repository at this point in the history
…-reset

Ensure indices are cleaned up on reset
  • Loading branch information
Jurshsmith authored Feb 13, 2024
2 parents c10d655 + 1d3c8bf commit 3ab805f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
55 changes: 43 additions & 12 deletions chaindexing/src/contract_states/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,38 @@ pub trait ContractStateMigrations: Send + Sync {
.collect()
}

// TODO: Consider returning reset_migrations along with migrations to reduce compute work
fn get_reset_migrations(&self) -> Vec<String> {
self.get_migrations()
.iter()
.filter(|m| m.starts_with("CREATE TABLE IF NOT EXISTS"))
.map(|create_migration| {
let table_name = extract_table_name(create_migration);
let mut reset_migrations = vec![];
for migration in self.get_migrations().iter() {
if migration.starts_with("CREATE TABLE IF NOT EXISTS") {
let table_name = extract_table_name(migration);
let reset_migration = format!("DROP TABLE IF EXISTS {table_name}");
reset_migrations.push(reset_migration);
} else if migration.starts_with("CREATE UNIQUE INDEX IF NOT EXISTS") {
let unique_index_name = extract_unique_index_name(migration);
let reset_migration = format!("DROP INDEX IF EXISTS {unique_index_name}");
reset_migrations.push(reset_migration);
}
}

format!("DROP TABLE IF EXISTS {table_name}")
})
.collect()
reset_migrations
}
}

fn extract_table_name(migration: &str) -> String {
migration
.replace("CREATE TABLE IF NOT EXISTS", "")
.split('(')
get_first_word_after_this(migration, "CREATE TABLE IF NOT EXISTS")
}

fn extract_unique_index_name(migration: &str) -> String {
get_first_word_after_this(migration, "CREATE UNIQUE INDEX IF NOT EXISTS")
}

fn get_first_word_after_this(words: &str, this: &str) -> String {
words
.replace(this, "")
.replace('(', " ")
.split_ascii_whitespace()
.collect::<Vec<&str>>()
.first()
.unwrap()
Expand Down Expand Up @@ -375,7 +390,23 @@ mod contract_state_migrations_get_migration_test {
let unique_index_migration = migrations.get(2);

assert!(unique_index_migration.is_some());
assert!(unique_index_migration.unwrap().contains("CREATE UNIQUE INDEX IF NOT EXISTS"));
assert!(unique_index_migration.unwrap().contains(
"CREATE UNIQUE INDEX IF NOT EXISTS unique_chaindexing_state_versions_for_nft_states"
));
}

#[test]
fn returns_unique_index_reset_migrations_for_state_versions() {
let contract_state = TestContractState;
let migrations = contract_state.get_reset_migrations();

let unique_index_reset_migration = migrations.get(2);

assert!(unique_index_reset_migration.is_some());
assert_eq!(
unique_index_reset_migration.unwrap(),
"DROP INDEX IF EXISTS unique_chaindexing_state_versions_for_nft_states"
);
}

#[test]
Expand Down
11 changes: 9 additions & 2 deletions chaindexing/src/repos/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ impl SQLikeMigrations {
]
}
pub fn drop_contract_addresses() -> &'static [&'static str] {
&["DROP TABLE IF EXISTS chaindexing_contract_addresses"]
&[
"DROP TABLE IF EXISTS chaindexing_contract_addresses",
"DROP INDEX chaindexing_contract_addresses_chain_address_index",
]
}

pub fn create_events() -> &'static [&'static str] {
Expand Down Expand Up @@ -235,7 +238,11 @@ impl SQLikeMigrations {
]
}
pub fn drop_events() -> &'static [&'static str] {
&["DROP TABLE IF EXISTS chaindexing_events"]
&[
"DROP TABLE IF EXISTS chaindexing_events",
"DROP INDEX chaindexing_events_chain_transaction_hash_log_index",
"DROP INDEX chaindexing_events_abi",
]
}

pub fn create_reorged_blocks() -> &'static [&'static str] {
Expand Down

0 comments on commit 3ab805f

Please sign in to comment.