From 16de8e3b5316dec269e9b4373ab204ecef8e8c9f Mon Sep 17 00:00:00 2001 From: Dzejkop Date: Tue, 5 Dec 2023 00:26:33 +0100 Subject: [PATCH] Allow parts for db settings --- config.toml | 9 ++++ src/config.rs | 102 ++++++++++++++++++++++++++++++++++++++++---- src/db.rs | 14 +++--- src/tasks/index.rs | 11 +++-- tests/common/mod.rs | 4 +- 5 files changed, 115 insertions(+), 25 deletions(-) diff --git a/config.toml b/config.toml index fa4ed3e..fcb1ac3 100644 --- a/config.toml +++ b/config.toml @@ -6,8 +6,17 @@ host = "127.0.0.1:3000" disable_auth = false [database] +kind = "connection_string" connection_string = "postgres://postgres:postgres@127.0.0.1:5432/database" +# [database] +# kind = "parts" +# host = "127.0.0.1" +# port = "5432" +# username = "postgres" +# password = "postgres" +# database = "database" + [keys] kind = "local" diff --git a/src/config.rs b/src/config.rs index 069923b..e371edb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -28,12 +28,53 @@ pub struct ServerConfig { pub disable_auth: bool, } +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "kind", rename_all = "snake_case")] +pub enum DatabaseConfig { + ConnectionString(DbConnectionString), + Parts(DbParts), +} + +impl DatabaseConfig { + pub fn connection_string(s: impl ToString) -> Self { + Self::ConnectionString(DbConnectionString { + connection_string: s.to_string(), + }) + } + + pub fn to_connection_string(&self) -> String { + match self { + Self::ConnectionString(s) => s.connection_string.clone(), + Self::Parts(parts) => { + format!( + "postgres://{}:{}@{}:{}/{}", + parts.username, + parts.password, + parts.host, + parts.port, + parts.database + ) + } + } + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] -pub struct DatabaseConfig { +pub struct DbConnectionString { pub connection_string: String, } +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub struct DbParts { + pub host: String, + pub port: String, + pub username: String, + pub password: String, + pub database: String, +} + #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "snake_case", tag = "kind")] pub enum KeysConfig { @@ -55,7 +96,7 @@ mod tests { use super::*; - const SAMPLE: &str = indoc! {r#" + const WITH_DB_CONNECTION_STRING: &str = indoc! {r#" [service] escalation_interval = "1h" @@ -64,14 +105,35 @@ mod tests { disable_auth = false [database] + kind = "connection_string" connection_string = "postgres://postgres:postgres@127.0.0.1:52804/database" [keys] kind = "local" "#}; + const WITH_DB_PARTS: &str = indoc! {r#" + [service] + escalation_interval = "1h" + + [server] + host = "127.0.0.1:3000" + disable_auth = false + + [database] + kind = "parts" + host = "host" + port = "5432" + username = "user" + password = "pass" + database = "db" + + [keys] + kind = "local" + "#}; + #[test] - fn sample() { + fn with_db_connection_string() { let config = Config { service: TxSitterConfig { escalation_interval: Duration::from_secs(60 * 60), @@ -80,16 +142,40 @@ mod tests { host: SocketAddr::from(([127, 0, 0, 1], 3000)), disable_auth: false, }, - database: DatabaseConfig { - connection_string: - "postgres://postgres:postgres@127.0.0.1:52804/database" - .to_string(), + database: DatabaseConfig::connection_string( + "postgres://postgres:postgres@127.0.0.1:52804/database" + .to_string(), + ), + keys: KeysConfig::Local(LocalKeysConfig {}), + }; + + let toml = toml::to_string_pretty(&config).unwrap(); + + assert_eq!(toml, WITH_DB_CONNECTION_STRING); + } + + #[test] + fn with_db_parts() { + let config = Config { + service: TxSitterConfig { + escalation_interval: Duration::from_secs(60 * 60), + }, + server: ServerConfig { + host: SocketAddr::from(([127, 0, 0, 1], 3000)), + disable_auth: false, }, + database: DatabaseConfig::Parts(DbParts { + host: "host".to_string(), + port: "5432".to_string(), + username: "user".to_string(), + password: "pass".to_string(), + database: "db".to_string(), + }), keys: KeysConfig::Local(LocalKeysConfig {}), }; let toml = toml::to_string_pretty(&config).unwrap(); - assert_eq!(toml, SAMPLE); + assert_eq!(toml, WITH_DB_PARTS); } } diff --git a/src/db.rs b/src/db.rs index c7f1be3..4b13993 100644 --- a/src/db.rs +++ b/src/db.rs @@ -26,19 +26,20 @@ pub struct Database { impl Database { pub async fn new(config: &DatabaseConfig) -> eyre::Result { + let connection_string = config.to_connection_string(); let pool = loop { - if !Postgres::database_exists(&config.connection_string).await? { - Postgres::create_database(&config.connection_string).await?; + if !Postgres::database_exists(&connection_string).await? { + Postgres::create_database(&connection_string).await?; } - let pool = Pool::connect(&config.connection_string).await?; + let pool = Pool::connect(&connection_string).await?; if let Err(err) = MIGRATOR.run(&pool).await { tracing::error!("{err:?}"); tracing::warn!("Migration mismatch dropping previosu db"); drop(pool); // Drop the DB if it's out of date - ONLY FOR TESTING - Postgres::drop_database(&config.connection_string).await?; + Postgres::drop_database(&connection_string).await?; } else { break pool; } @@ -931,10 +932,7 @@ mod tests { let url = format!("postgres://postgres:postgres@{db_socket_addr}/database"); - let db = Database::new(&DatabaseConfig { - connection_string: url, - }) - .await?; + let db = Database::new(&DatabaseConfig::connection_string(url)).await?; Ok((db, db_container)) } diff --git a/src/tasks/index.rs b/src/tasks/index.rs index c8d1db1..903f99a 100644 --- a/src/tasks/index.rs +++ b/src/tasks/index.rs @@ -66,16 +66,15 @@ pub async fn estimate_gas(app: Arc, chain_id: u64) -> eyre::Result<()> { let rpc = app.http_provider(chain_id).await?; loop { - let latest_block_number = - app.db.get_latest_block_number_without_fee_estimates(chain_id).await?; + let latest_block_number = app + .db + .get_latest_block_number_without_fee_estimates(chain_id) + .await?; let Some(latest_block_number) = latest_block_number else { tracing::info!("No blocks to estimate fees for"); - tokio::time::sleep(Duration::from_secs( - TIME_BETWEEN_FEE_ESTIMATION_SECONDS, - )) - .await; + tokio::time::sleep(Duration::from_secs(2)).await; continue; }; diff --git a/tests/common/mod.rs b/tests/common/mod.rs index dff313b..d233f81 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -149,9 +149,7 @@ pub async fn setup_service( )), disable_auth: true, }, - database: DatabaseConfig { - connection_string: db_connection_url.to_string(), - }, + database: DatabaseConfig::connection_string(db_connection_url), keys: KeysConfig::Local(LocalKeysConfig {}), };