Skip to content
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.

27 changes: 17 additions & 10 deletions common/nyxd-scraper-psql/src/storage/block_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use nyxd_scraper_shared::storage::helpers::log_db_operation_time;
use nyxd_scraper_shared::storage::{NyxdScraperStorage, NyxdScraperStorageError};
use sqlx::types::time::{OffsetDateTime, PrimitiveDateTime};
use tokio::time::Instant;
use tracing::{debug, error, info, instrument, warn};
use tracing::{debug, error, info, instrument};

#[derive(Clone)]
pub struct PostgresScraperStorage {
Expand All @@ -22,7 +22,10 @@ pub struct PostgresScraperStorage {

impl PostgresScraperStorage {
#[instrument]
pub async fn init(connection_string: &str) -> Result<Self, PostgresScraperError> {
pub async fn init(
connection_string: &str,
run_migrations: &bool,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why pass bool as a reference? I'm surprised clippy hasn't complained

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me see what it says if I change it...

) -> Result<Self, PostgresScraperError> {
debug!("initialising scraper database with '{connection_string}'",);

let connection_pool = match sqlx::PgPool::connect(connection_string).await {
Expand All @@ -33,12 +36,13 @@ impl PostgresScraperStorage {
}
};

if let Err(err) = sqlx::migrate!("./sql_migrations")
.run(&connection_pool)
.await
{
warn!("Failed to initialize SQLx database: {err}");
// return Err(err.into());
if *run_migrations {
if let Err(err) = sqlx::migrate!("./sql_migrations")
.run(&connection_pool)
.await
{
return Err(err.into());
}
}

info!("Database migration finished!");
Expand Down Expand Up @@ -192,8 +196,11 @@ impl PostgresScraperStorage {
impl NyxdScraperStorage for PostgresScraperStorage {
type StorageTransaction = PostgresStorageTransaction;

async fn initialise(storage: &str) -> Result<Self, NyxdScraperStorageError> {
PostgresScraperStorage::init(storage)
async fn initialise(
storage: &str,
run_migrations: &bool,
) -> Result<Self, NyxdScraperStorageError> {
PostgresScraperStorage::init(storage, run_migrations)
.await
.map_err(NyxdScraperStorageError::from)
}
Expand Down
4 changes: 3 additions & 1 deletion common/nyxd-scraper-shared/src/scraper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub struct Config {
pub store_precommits: bool,

pub start_block: StartingBlockOpts,

pub run_migrations: bool,
}

pub struct NyxdScraperBuilder<S> {
Expand Down Expand Up @@ -161,7 +163,7 @@ where

pub async fn new(config: Config) -> Result<Self, ScraperError> {
config.pruning_options.validate()?;
let storage = S::initialise(&config.database_storage).await?;
let storage = S::initialise(&config.database_storage, &config.run_migrations).await?;
let rpc_client = RpcClient::new(&config.rpc_url)?;

Ok(NyxdScraper {
Expand Down
5 changes: 4 additions & 1 deletion common/nyxd-scraper-shared/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ pub trait NyxdScraperStorage: Clone + Sized {
type StorageTransaction: NyxdScraperTransaction;

/// Either connection string (postgres) or storage path (sqlite)
async fn initialise(storage: &str) -> Result<Self, NyxdScraperStorageError>;
async fn initialise(
storage: &str,
run_migrations: &bool,
) -> Result<Self, NyxdScraperStorageError>;

async fn begin_processing_tx(
&self,
Expand Down
5 changes: 4 additions & 1 deletion common/nyxd-scraper-sqlite/src/storage/block_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ impl SqliteScraperStorage {
impl NyxdScraperStorage for SqliteScraperStorage {
type StorageTransaction = SqliteStorageTransaction;

async fn initialise(storage: &str) -> Result<Self, NyxdScraperStorageError> {
async fn initialise(
storage: &str,
_run_migrations: &bool,
) -> Result<Self, NyxdScraperStorageError> {
SqliteScraperStorage::init(storage)
.await
.map_err(NyxdScraperStorageError::from)
Expand Down
2 changes: 1 addition & 1 deletion nym-data-observatory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[package]
name = "nym-data-observatory"
version = "1.0.0"
version = "1.0.1"
authors.workspace = true
repository.workspace = true
homepage.workspace = true
Expand Down
9 changes: 2 additions & 7 deletions nym-data-observatory/src/chain_scraper/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::cli::commands::run::Args;
use crate::db::DbPool;
use nyxd_scraper_psql::{PostgresNyxdScraper, PruningOptions};
use std::fs;
use tracing::{info, warn};
use tracing::info;

pub(crate) mod webhook;

Expand All @@ -13,11 +12,6 @@ pub(crate) async fn run_chain_scraper(
) -> anyhow::Result<PostgresNyxdScraper> {
let use_best_effort_start_height = args.start_block_height.is_some();

if args.nuke_db {
warn!("☢️☢️☢️ NUKING THE SCRAPER DATABASE");
fs::remove_file(config.chain_scraper_connection_string())?;
}

let database_storage = config
.chain_scraper_connection_string
.clone()
Expand All @@ -34,6 +28,7 @@ pub(crate) async fn run_chain_scraper(
start_block_height: args.start_block_height,
use_best_effort_start_height,
},
run_migrations: false, // ignore the base migrations
})
.with_msg_module(crate::modules::wasm::WasmModule::new(connection_pool))
.with_tx_module(webhook::WebhookModule::new(config.clone())?);
Expand Down
3 changes: 0 additions & 3 deletions nym-data-observatory/src/cli/commands/run/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ pub(crate) struct Args {
#[arg(long, env = NYXD_SCRAPER_START_HEIGHT)]
pub(crate) start_block_height: Option<u32>,

#[arg(long, env = NYXD_SCRAPER_UNSAFE_NUKE_DB, default_value = "false")]
pub(crate) nuke_db: bool,

/// (Override) Postgres connection string for chain scraper history
#[arg(long, env = NYM_DATA_OBSERVATORY_DB_URL, alias = "db_url")]
pub(crate) db_connection_string: Option<String>,
Expand Down
1 change: 0 additions & 1 deletion nym-data-observatory/src/cli/commands/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ pub(crate) async fn execute(args: Args, http_port: u16) -> Result<(), NymDataObs
w.watch_for_chain_message_types
);
}
info!("nuke_db: {}", args.nuke_db);

let storage = db::Storage::init(db_connection_string).await?;
let watcher_pool = storage.pool_owned();
Expand Down
12 changes: 11 additions & 1 deletion nym-data-observatory/src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use anyhow::{Result, anyhow};
use sqlx::{Postgres, postgres::PgConnectOptions};
use sqlx::{Postgres, migrate::Migrator, postgres::PgConnectOptions};
use std::str::FromStr;
use tracing::info;

pub(crate) mod models;
pub(crate) mod queries {
pub mod price;
pub mod wasm;
}

static MIGRATOR: Migrator = sqlx::migrate!("./migrations");

pub(crate) type DbPool = sqlx::Pool<Postgres>;

pub(crate) struct Storage {
Expand All @@ -22,6 +25,13 @@ impl Storage {
.await
.map_err(|err| anyhow!("Failed to connect to {}: {}", &connection_url, err))?;

MIGRATOR
.run(&pool)
.await
.map_err(|err| anyhow!("Failed to run migrations: {}", err))?;

info!("✅ Successfully migrated the database");

Ok(Storage { pool })
}

Expand Down
2 changes: 0 additions & 2 deletions nym-data-observatory/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ pub mod vars {
pub const NYXD_SCRAPER_USE_BEST_EFFORT_START_HEIGHT: &str =
"NYXD_SCRAPER_USE_BEST_EFFORT_START_HEIGHT";

pub const NYXD_SCRAPER_UNSAFE_NUKE_DB: &str = "NYXD_SCRAPER_UNSAFE_NUKE_DB";

pub const NYM_DATA_OBSERVATORY_ID_ARG: &str = "NYM_DATA_OBSERVATORY_ID";
pub const NYM_DATA_OBSERVATORY_OUTPUT_ARG: &str = "NYM_DATA_OBSERVATORY_OUTPUT";

Expand Down
1 change: 1 addition & 0 deletions nym-validator-rewarder/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl Config {
start_block_height: None,
use_best_effort_start_height: true,
},
run_migrations: true,
})
}

Expand Down
1 change: 1 addition & 0 deletions nyx-chain-watcher/src/chain_scraper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub(crate) async fn run_chain_scraper(
start_block_height,
use_best_effort_start_height,
},
run_migrations: true,
})
.with_msg_module(BankScraperModule::new(
db_pool,
Expand Down
Loading