Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add database migration cli commands #381

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 45 additions & 9 deletions components/ordhook-cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
open_blocks_db_with_retry, open_readonly_blocks_db,
};
use ordhook::db::cursor::BlockBytesCursor;
use ordhook::db::migrate_dbs;
use ordhook::db::{migrate_dbs, reset_dbs};
use ordhook::service::Service;
use ordhook::try_info;
use std::collections::HashSet;
Expand All @@ -43,9 +43,29 @@
/// Stream Bitcoin blocks and index ordinals inscriptions and transfers
#[clap(subcommand)]
Service(ServiceCommand),
/// Perform maintenance operations on local databases
/// Perform maintenance operations on local index
#[clap(subcommand)]
Db(OrdhookDbCommand),
Index(IndexCommand),
/// Database operations
#[clap(subcommand)]
Database(DatabaseCommand),
}

#[derive(Subcommand, PartialEq, Clone, Debug)]
enum DatabaseCommand {
/// Migrates database
#[clap(name = "migrate", bin_name = "migrate")]
Migrate(DatabaseMigrateCommand),
/// Resets database to an empty state
#[clap(name = "reset", bin_name = "reset")]
Reset(DatabaseMigrateCommand),
}

#[derive(Parser, PartialEq, Clone, Debug)]
struct DatabaseMigrateCommand {
/// Load config file path
#[clap(long = "config-path")]
pub config_path: Option<String>,
}

#[derive(Subcommand, PartialEq, Clone, Debug)]
Expand Down Expand Up @@ -184,7 +204,7 @@
}

#[derive(Subcommand, PartialEq, Clone, Debug)]
enum OrdhookDbCommand {
enum IndexCommand {
/// Initialize a new ordhook db
#[clap(name = "new", bin_name = "new")]
New(SyncOrdhookDbCommand),
Expand Down Expand Up @@ -324,18 +344,18 @@
println!("Created file Ordhook.toml");
}
},
Command::Db(OrdhookDbCommand::New(cmd)) => {
Command::Index(IndexCommand::New(cmd)) => {

Check warning on line 347 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L347

Added line #L347 was not covered by tests
let config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?;
migrate_dbs(&config, ctx).await?;
open_blocks_db_with_retry(true, &config, ctx);
}
Command::Db(OrdhookDbCommand::Sync(cmd)) => {
Command::Index(IndexCommand::Sync(cmd)) => {

Check warning on line 352 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L352

Added line #L352 was not covered by tests
let config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?;
migrate_dbs(&config, ctx).await?;
let service = Service::new(&config, ctx);
service.catch_up_to_bitcoin_chain_tip().await?;
}
Command::Db(OrdhookDbCommand::Repair(subcmd)) => match subcmd {
Command::Index(IndexCommand::Repair(subcmd)) => match subcmd {

Check warning on line 358 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L358

Added line #L358 was not covered by tests
RepairCommand::Blocks(cmd) => {
let mut config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?;
if let Some(network_threads) = cmd.network_threads {
Expand Down Expand Up @@ -369,7 +389,7 @@
}
}
},
Command::Db(OrdhookDbCommand::Check(cmd)) => {
Command::Index(IndexCommand::Check(cmd)) => {

Check warning on line 392 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L392

Added line #L392 was not covered by tests
let config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?;
{
let blocks_db = open_readonly_blocks_db(&config, ctx)?;
Expand All @@ -379,7 +399,7 @@
println!("{:?}", missing_blocks);
}
}
Command::Db(OrdhookDbCommand::Drop(cmd)) => {
Command::Index(IndexCommand::Drop(cmd)) => {

Check warning on line 402 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L402

Added line #L402 was not covered by tests
let config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?;

let service = Service::new(&config, ctx);
Expand All @@ -401,6 +421,22 @@
service.rollback(&block_heights).await?;
println!("{} blocks dropped", cmd.blocks);
}
Command::Database(DatabaseCommand::Migrate(cmd)) => {
let config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?;
migrate_dbs(&config, ctx).await?;

Check warning on line 426 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L424-L426

Added lines #L424 - L426 were not covered by tests
}
Command::Database(DatabaseCommand::Reset(cmd)) => {
let config = ConfigFile::default(false, false, false, &cmd.config_path, &None)?;
println!(

Check warning on line 430 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L428-L430

Added lines #L428 - L430 were not covered by tests
"WARNING: This operation will delete ALL index data and cannot be undone. Confirm? [Y/n]"
);
let mut buffer = String::new();
std::io::stdin().read_line(&mut buffer).unwrap();
if buffer.to_lowercase().starts_with('n') {
return Err("Aborted".to_string());

Check warning on line 436 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L433-L436

Added lines #L433 - L436 were not covered by tests
}
reset_dbs(&config, ctx).await?;

Check warning on line 438 in components/ordhook-cli/src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-cli/src/cli/mod.rs#L438

Added line #L438 was not covered by tests
}
}
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ mod test {
VerifiedBrc20BalanceData, VerifiedBrc20TokenDeployData, VerifiedBrc20TransferData,
},
},
db::{pg_test_clear_db, pg_test_connection, pg_test_connection_pool},
db::{pg_reset_db, pg_test_connection, pg_test_connection_pool},
};

async fn get_counts_by_operation<T: GenericClient>(client: &T) -> (i32, i32, i32, i32) {
Expand Down Expand Up @@ -1000,7 +1000,7 @@ mod test {
);
}
}
pg_test_clear_db(&mut pg_client).await;
pg_reset_db(&mut pg_client).await;
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ mod test {
VerifiedBrc20TokenDeployData,
},
},
db::{pg_test_clear_db, pg_test_connection, pg_test_connection_pool},
db::{pg_reset_db, pg_test_connection, pg_test_connection_pool},
};

use super::Brc20MemoryCache;
Expand Down Expand Up @@ -641,7 +641,7 @@ mod test {
)))
);
}
pg_test_clear_db(&mut pg_client).await;
pg_reset_db(&mut pg_client).await;
Ok(())
}

Expand Down Expand Up @@ -743,7 +743,7 @@ mod test {
)
.await?)
};
pg_test_clear_db(&mut pg_client).await;
pg_reset_db(&mut pg_client).await;
result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ mod test {
VerifiedBrc20BalanceData, VerifiedBrc20Operation, VerifiedBrc20TokenDeployData,
},
},
db::{pg_test_clear_db, pg_test_connection, pg_test_connection_pool},
db::{pg_reset_db, pg_test_connection, pg_test_connection_pool},
};

use super::{verify_brc20_operation, verify_brc20_transfer, VerifiedBrc20TransferData};
Expand Down Expand Up @@ -421,7 +421,7 @@ mod test {
)
.await
};
pg_test_clear_db(&mut pg_client).await;
pg_reset_db(&mut pg_client).await;
result
}

Expand Down Expand Up @@ -534,7 +534,7 @@ mod test {
)
.await
};
pg_test_clear_db(&mut pg_client).await;
pg_reset_db(&mut pg_client).await;
result
}

Expand Down Expand Up @@ -621,7 +621,7 @@ mod test {
)
.await
};
pg_test_clear_db(&mut pg_client).await;
pg_reset_db(&mut pg_client).await;
result
}

Expand Down Expand Up @@ -698,7 +698,7 @@ mod test {
)
.await
};
pg_test_clear_db(&mut pg_client).await;
pg_reset_db(&mut pg_client).await;
result
}

Expand Down Expand Up @@ -778,7 +778,7 @@ mod test {
)
.await
};
pg_test_clear_db(&mut pg_client).await;
pg_reset_db(&mut pg_client).await;
result
}

Expand Down Expand Up @@ -941,7 +941,7 @@ mod test {
)
.await
};
pg_test_clear_db(&mut pg_client).await;
pg_reset_db(&mut pg_client).await;
result
}

Expand Down Expand Up @@ -1067,7 +1067,7 @@ mod test {
).await?;
verify_brc20_transfer(&transfer, &mut cache, &client, &ctx).await
};
pg_test_clear_db(&mut pg_client).await;
pg_reset_db(&mut pg_client).await;
result
}

Expand Down Expand Up @@ -1175,7 +1175,7 @@ mod test {
.await?;
verify_brc20_transfer(&transfer, &mut cache, &client, &ctx).await
};
pg_test_clear_db(&mut pg_client).await;
pg_reset_db(&mut pg_client).await;
result
}
}
4 changes: 2 additions & 2 deletions components/ordhook-core/src/core/protocol/sequence_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ mod test {
core::test_builders::{TestBlockBuilder, TestTransactionBuilder},
db::{
ordinals_pg::{self, insert_block},
pg_test_clear_db, pg_test_connection, pg_test_connection_pool,
pg_reset_db, pg_test_connection, pg_test_connection_pool,
},
};

Expand Down Expand Up @@ -200,7 +200,7 @@ mod test {

(next.classic, next.jubilee)
};
pg_test_clear_db(&mut pg_client).await;
pg_reset_db(&mut pg_client).await;
Ok(result)
}
}
78 changes: 44 additions & 34 deletions components/ordhook-core/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use chainhook_sdk::utils::Context;

use crate::{config::Config, core::meta_protocols::brc20::brc20_pg, try_info};
use crate::{config::Config, core::meta_protocols::brc20::brc20_pg, try_info, try_warn};

pub async fn migrate_dbs(config: &Config, ctx: &Context) -> Result<(), String> {
{
Expand All @@ -23,32 +23,24 @@
Ok(())
}

#[cfg(test)]
pub fn pg_test_config() -> chainhook_postgres::PgConnectionConfig {
chainhook_postgres::PgConnectionConfig {
dbname: "postgres".to_string(),
host: "localhost".to_string(),
port: 5432,
user: "postgres".to_string(),
password: Some("postgres".to_string()),
search_path: None,
pool_max_size: None,
pub async fn reset_dbs(config: &Config, ctx: &Context) -> Result<(), String> {

Check warning on line 26 in components/ordhook-core/src/db/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-core/src/db/mod.rs#L26

Added line #L26 was not covered by tests
{
try_warn!(ctx, "Resetting ordinals DB");
let mut pg_client = pg_connect_with_retry(&config.ordinals_db).await;
pg_reset_db(&mut pg_client).await?;

Check warning on line 30 in components/ordhook-core/src/db/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-core/src/db/mod.rs#L28-L30

Added lines #L28 - L30 were not covered by tests
}
if let (Some(brc20_db), true) = (&config.brc20_db, config.meta_protocols.brc20) {
try_warn!(ctx, "Resetting brc20 DB");
let mut pg_client = pg_connect_with_retry(&brc20_db).await;
pg_reset_db(&mut pg_client).await?;

Check warning on line 35 in components/ordhook-core/src/db/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-core/src/db/mod.rs#L32-L35

Added lines #L32 - L35 were not covered by tests
}
Ok(())

Check warning on line 37 in components/ordhook-core/src/db/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-core/src/db/mod.rs#L37

Added line #L37 was not covered by tests
}

#[cfg(test)]
pub fn pg_test_connection_pool() -> chainhook_postgres::deadpool_postgres::Pool {
chainhook_postgres::pg_pool(&pg_test_config()).unwrap()
}

#[cfg(test)]
pub async fn pg_test_connection() -> chainhook_postgres::tokio_postgres::Client {
chainhook_postgres::pg_connect(&pg_test_config()).await.unwrap()
}

#[cfg(test)]
pub async fn pg_test_clear_db(pg_client: &mut chainhook_postgres::tokio_postgres::Client) {
match pg_client
pub async fn pg_reset_db(
pg_client: &mut chainhook_postgres::tokio_postgres::Client,
) -> Result<(), String> {
pg_client
.batch_execute(
"
DO $$ DECLARE
Expand All @@ -66,16 +58,34 @@
END LOOP;
END $$;",
)
.await {
Ok(rows) => rows,
Err(e) => {
println!(
"error rolling back test migrations: {}",
e.to_string()
);
std::process::exit(1);
}
};
.await
.map_err(|e| format!("unable to reset db: {e}"))?;

Check warning on line 62 in components/ordhook-core/src/db/mod.rs

View check run for this annotation

Codecov / codecov/patch

components/ordhook-core/src/db/mod.rs#L62

Added line #L62 was not covered by tests
Ok(())
}

#[cfg(test)]
pub fn pg_test_config() -> chainhook_postgres::PgConnectionConfig {
chainhook_postgres::PgConnectionConfig {
dbname: "postgres".to_string(),
host: "localhost".to_string(),
port: 5432,
user: "postgres".to_string(),
password: Some("postgres".to_string()),
search_path: None,
pool_max_size: None,
}
}

#[cfg(test)]
pub fn pg_test_connection_pool() -> chainhook_postgres::deadpool_postgres::Pool {
chainhook_postgres::pg_pool(&pg_test_config()).unwrap()
}

#[cfg(test)]
pub async fn pg_test_connection() -> chainhook_postgres::tokio_postgres::Client {
chainhook_postgres::pg_connect(&pg_test_config())
.await
.unwrap()
}

/// Drops DB files in a test environment.
Expand Down
4 changes: 2 additions & 2 deletions components/ordhook-core/src/db/ordinals_pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ mod test {
self, get_chain_tip_block_height, get_inscriptions_at_block, insert_block,
rollback_block,
},
pg_test_clear_db, pg_test_connection, pg_test_connection_pool,
pg_reset_db, pg_test_connection, pg_test_connection_pool,
},
};

Expand Down Expand Up @@ -1401,7 +1401,7 @@ mod test {
assert_eq!(Some(799999), get_chain_tip_block_height(&client).await?);
}
}
pg_test_clear_db(&mut pg_client).await;
pg_reset_db(&mut pg_client).await;
Ok(())
}
}
Loading