Skip to content

Commit

Permalink
[Indexer] Add --skip-migrations option to --reset-database (#19945)
Browse files Browse the repository at this point in the history
## Description 

This is useful when I need to manually run migrations. 

## Test plan 

CI

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
lxfind authored Oct 22, 2024
1 parent 4092045 commit 5a346e6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
4 changes: 4 additions & 0 deletions crates/sui-indexer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ pub enum Command {
ResetDatabase {
#[clap(long)]
force: bool,
/// If true, only drop all tables but do not run the migrations.
/// That is, no tables will exist in the DB after the reset.
#[clap(long, default_value_t = false)]
skip_migrations: bool,
},
/// Run through the migration scripts.
RunMigrations,
Expand Down
25 changes: 12 additions & 13 deletions crates/sui-indexer/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,14 @@ pub mod setup_postgres {

pub async fn reset_database(mut conn: Connection<'static>) -> Result<(), anyhow::Error> {
info!("Resetting PG database ...");
clear_database(&mut conn).await?;
run_migrations(conn).await?;
info!("Reset database complete.");
Ok(())
}

pub async fn clear_database(conn: &mut Connection<'static>) -> Result<(), anyhow::Error> {
info!("Clearing the database...");
let drop_all_tables = "
DO $$ DECLARE
r RECORD;
Expand All @@ -206,9 +213,7 @@ pub mod setup_postgres {
EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE';
END LOOP;
END $$;";
diesel::sql_query(drop_all_tables)
.execute(&mut conn)
.await?;
diesel::sql_query(drop_all_tables).execute(conn).await?;
info!("Dropped all tables.");

let drop_all_procedures = "
Expand All @@ -222,9 +227,7 @@ pub mod setup_postgres {
EXECUTE 'DROP PROCEDURE IF EXISTS ' || quote_ident(r.proname) || '(' || r.argtypes || ') CASCADE';
END LOOP;
END $$;";
diesel::sql_query(drop_all_procedures)
.execute(&mut conn)
.await?;
diesel::sql_query(drop_all_procedures).execute(conn).await?;
info!("Dropped all procedures.");

let drop_all_functions = "
Expand All @@ -238,17 +241,13 @@ pub mod setup_postgres {
EXECUTE 'DROP FUNCTION IF EXISTS ' || quote_ident(r.proname) || '(' || r.argtypes || ') CASCADE';
END LOOP;
END $$;";
diesel::sql_query(drop_all_functions)
.execute(&mut conn)
.await?;
info!("Dropped all functions.");

run_migrations(conn).await?;
info!("Reset database complete.");
diesel::sql_query(drop_all_functions).execute(conn).await?;
info!("Database cleared.");
Ok(())
}

pub async fn run_migrations(conn: Connection<'static>) -> Result<(), anyhow::Error> {
info!("Running migrations ...");
conn.run_pending_migrations(MIGRATIONS)
.await
.map_err(|e| anyhow!("Failed to run migrations {e}"))?;
Expand Down
12 changes: 10 additions & 2 deletions crates/sui-indexer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sui_indexer::backfill::backfill_runner::BackfillRunner;
use sui_indexer::benchmark::run_indexer_benchmark;
use sui_indexer::config::{Command, UploadOptions};
use sui_indexer::database::ConnectionPool;
use sui_indexer::db::setup_postgres::clear_database;
use sui_indexer::db::{
check_db_migration_consistency, check_prunable_tables_valid, reset_database, run_migrations,
};
Expand Down Expand Up @@ -72,14 +73,21 @@ async fn main() -> anyhow::Result<()> {
Indexer::start_reader(&json_rpc_config, &registry, pool, CancellationToken::new())
.await?;
}
Command::ResetDatabase { force } => {
Command::ResetDatabase {
force,
skip_migrations,
} => {
if !force {
return Err(anyhow::anyhow!(
"Resetting the DB requires use of the `--force` flag",
));
}

reset_database(pool.dedicated_connection().await?).await?;
if skip_migrations {
clear_database(&mut pool.dedicated_connection().await?).await?;
} else {
reset_database(pool.dedicated_connection().await?).await?;
}
}
Command::RunMigrations => {
run_migrations(pool.dedicated_connection().await?).await?;
Expand Down

0 comments on commit 5a346e6

Please sign in to comment.