Skip to content

Commit

Permalink
Allow parts for db settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed Dec 4, 2023
1 parent 4f77401 commit 16de8e3
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 25 deletions.
9 changes: 9 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@ host = "127.0.0.1:3000"
disable_auth = false

[database]
kind = "connection_string"
connection_string = "postgres://postgres:[email protected]:5432/database"

# [database]
# kind = "parts"
# host = "127.0.0.1"
# port = "5432"
# username = "postgres"
# password = "postgres"
# database = "database"

[keys]
kind = "local"

Expand Down
102 changes: 94 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"
Expand All @@ -64,14 +105,35 @@ mod tests {
disable_auth = false
[database]
kind = "connection_string"
connection_string = "postgres://postgres:[email protected]: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),
Expand All @@ -80,16 +142,40 @@ mod tests {
host: SocketAddr::from(([127, 0, 0, 1], 3000)),
disable_auth: false,
},
database: DatabaseConfig {
connection_string:
"postgres://postgres:[email protected]:52804/database"
.to_string(),
database: DatabaseConfig::connection_string(
"postgres://postgres:[email protected]: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);
}
}
14 changes: 6 additions & 8 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ pub struct Database {

impl Database {
pub async fn new(config: &DatabaseConfig) -> eyre::Result<Self> {
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;
}
Expand Down Expand Up @@ -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))
}
Expand Down
11 changes: 5 additions & 6 deletions src/tasks/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,15 @@ pub async fn estimate_gas(app: Arc<App>, 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;
};
Expand Down
4 changes: 1 addition & 3 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}),
};

Expand Down

0 comments on commit 16de8e3

Please sign in to comment.