Skip to content

Commit 18e72c9

Browse files
committed
run the migrations from the data observatory and not the base chain scraper
1 parent fd05154 commit 18e72c9

File tree

8 files changed

+43
-16
lines changed

8 files changed

+43
-16
lines changed

common/nyxd-scraper-psql/src/storage/block_storage.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use nyxd_scraper_shared::storage::helpers::log_db_operation_time;
1313
use nyxd_scraper_shared::storage::{NyxdScraperStorage, NyxdScraperStorageError};
1414
use sqlx::types::time::{OffsetDateTime, PrimitiveDateTime};
1515
use tokio::time::Instant;
16-
use tracing::{debug, error, info, instrument, warn};
16+
use tracing::{debug, error, info, instrument};
1717

1818
#[derive(Clone)]
1919
pub struct PostgresScraperStorage {
@@ -22,7 +22,10 @@ pub struct PostgresScraperStorage {
2222

2323
impl PostgresScraperStorage {
2424
#[instrument]
25-
pub async fn init(connection_string: &str) -> Result<Self, PostgresScraperError> {
25+
pub async fn init(
26+
connection_string: &str,
27+
run_migrations: &bool,
28+
) -> Result<Self, PostgresScraperError> {
2629
debug!("initialising scraper database with '{connection_string}'",);
2730

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

36-
if let Err(err) = sqlx::migrate!("./sql_migrations")
37-
.run(&connection_pool)
38-
.await
39-
{
40-
warn!("Failed to initialize SQLx database: {err}");
41-
// return Err(err.into());
39+
if *run_migrations {
40+
if let Err(err) = sqlx::migrate!("./sql_migrations")
41+
.run(&connection_pool)
42+
.await
43+
{
44+
return Err(err.into());
45+
}
4246
}
4347

4448
info!("Database migration finished!");
@@ -192,8 +196,11 @@ impl PostgresScraperStorage {
192196
impl NyxdScraperStorage for PostgresScraperStorage {
193197
type StorageTransaction = PostgresStorageTransaction;
194198

195-
async fn initialise(storage: &str) -> Result<Self, NyxdScraperStorageError> {
196-
PostgresScraperStorage::init(storage)
199+
async fn initialise(
200+
storage: &str,
201+
run_migrations: &bool,
202+
) -> Result<Self, NyxdScraperStorageError> {
203+
PostgresScraperStorage::init(storage, run_migrations)
197204
.await
198205
.map_err(NyxdScraperStorageError::from)
199206
}

common/nyxd-scraper-shared/src/scraper/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ pub struct Config {
4848
pub store_precommits: bool,
4949

5050
pub start_block: StartingBlockOpts,
51+
52+
pub run_migrations: bool,
5153
}
5254

5355
pub struct NyxdScraperBuilder<S> {
@@ -161,7 +163,7 @@ where
161163

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

167169
Ok(NyxdScraper {

common/nyxd-scraper-shared/src/storage/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ pub trait NyxdScraperStorage: Clone + Sized {
3333
type StorageTransaction: NyxdScraperTransaction;
3434

3535
/// Either connection string (postgres) or storage path (sqlite)
36-
async fn initialise(storage: &str) -> Result<Self, NyxdScraperStorageError>;
36+
async fn initialise(
37+
storage: &str,
38+
run_migrations: &bool,
39+
) -> Result<Self, NyxdScraperStorageError>;
3740

3841
async fn begin_processing_tx(
3942
&self,

common/nyxd-scraper-sqlite/src/storage/block_storage.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ impl SqliteScraperStorage {
207207
impl NyxdScraperStorage for SqliteScraperStorage {
208208
type StorageTransaction = SqliteStorageTransaction;
209209

210-
async fn initialise(storage: &str) -> Result<Self, NyxdScraperStorageError> {
210+
async fn initialise(
211+
storage: &str,
212+
_run_migrations: &bool,
213+
) -> Result<Self, NyxdScraperStorageError> {
211214
SqliteScraperStorage::init(storage)
212215
.await
213216
.map_err(NyxdScraperStorageError::from)

nym-data-observatory/src/chain_scraper/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::cli::commands::run::Args;
22
use crate::db::DbPool;
33
use nyxd_scraper_psql::{PostgresNyxdScraper, PruningOptions};
4-
use std::fs;
5-
use tracing::{info, warn};
4+
use tracing::info;
65

76
pub(crate) mod webhook;
87

@@ -29,6 +28,7 @@ pub(crate) async fn run_chain_scraper(
2928
start_block_height: args.start_block_height,
3029
use_best_effort_start_height,
3130
},
31+
run_migrations: false, // ignore the base migrations
3232
})
3333
.with_msg_module(crate::modules::wasm::WasmModule::new(connection_pool))
3434
.with_tx_module(webhook::WebhookModule::new(config.clone())?);

nym-data-observatory/src/db/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
use anyhow::{Result, anyhow};
2-
use sqlx::{Postgres, postgres::PgConnectOptions};
2+
use sqlx::{Postgres, migrate::Migrator, postgres::PgConnectOptions};
33
use std::str::FromStr;
4+
use tracing::info;
45

56
pub(crate) mod models;
67
pub(crate) mod queries {
78
pub mod price;
89
pub mod wasm;
910
}
1011

12+
static MIGRATOR: Migrator = sqlx::migrate!("./migrations");
13+
1114
pub(crate) type DbPool = sqlx::Pool<Postgres>;
1215

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

28+
MIGRATOR
29+
.run(&pool)
30+
.await
31+
.map_err(|err| anyhow!("Failed to run migrations: {}", err))?;
32+
33+
info!("✅ Successfully migrated the database");
34+
2535
Ok(Storage { pool })
2636
}
2737

nym-validator-rewarder/src/config/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ impl Config {
135135
start_block_height: None,
136136
use_best_effort_start_height: true,
137137
},
138+
run_migrations: true,
138139
})
139140
}
140141

nyx-chain-watcher/src/chain_scraper/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub(crate) async fn run_chain_scraper(
6060
start_block_height,
6161
use_best_effort_start_height,
6262
},
63+
run_migrations: true,
6364
})
6465
.with_msg_module(BankScraperModule::new(
6566
db_pool,

0 commit comments

Comments
 (0)