From 1fbecce731326934d2b03ea47cc8a09d0ad4d193 Mon Sep 17 00:00:00 2001 From: Carlos V Date: Thu, 3 Oct 2024 20:50:49 +0000 Subject: [PATCH] feat: Add option to use postgres_url or individual vars to connect --- common/src/indexer_service/http/config.rs | 32 ---------- config/src/config.rs | 71 ++++++++++++++++++++--- service/src/config.rs | 11 +--- tap-agent/src/config.rs | 2 +- 4 files changed, 68 insertions(+), 48 deletions(-) diff --git a/common/src/indexer_service/http/config.rs b/common/src/indexer_service/http/config.rs index 35a2e5e3..909a3ba3 100644 --- a/common/src/indexer_service/http/config.rs +++ b/common/src/indexer_service/http/config.rs @@ -5,43 +5,11 @@ use std::net::SocketAddr; use serde::{Deserialize, Serialize}; use thegraph_core::{Address, DeploymentId}; -use tracing::warn; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct DatabaseConfig { pub postgres_url: String, } -impl DatabaseConfig { - pub fn format_db_config( - ps_url: Option, - ps_host: Option, - ps_pwd: Option, - ps_port: Option, - ps_user: Option, - ps_db: Option, - ) -> Self { - let db_config = (ps_url, ps_host, ps_pwd, ps_port, ps_user, ps_db); - match db_config { - (Some(url), ..) if !url.is_empty() => DatabaseConfig { postgres_url: url }, - (None, Some(host), Some(pwd), Some(port), Some(user), Some(dbname)) => { - let postgres_url = - format!("postgres://{}:{}@{}:{}/{}", user, pwd, host, port, dbname); - DatabaseConfig { postgres_url } - } - _ => { - warn!( - "Some configuration values are missing for database values, please make sure you either \ - pass `postgres_url` or pass all the variables to connect to the database - "); - // This will eventually fail to connect - DatabaseConfig { - postgres_url: String::new(), - } - } - } - } -} - #[derive(Clone, Debug, Deserialize, Serialize)] pub struct SubgraphConfig { #[serde(default)] diff --git a/config/src/config.rs b/config/src/config.rs index 5d2b863c..74cb4d72 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -147,13 +147,40 @@ pub struct IndexerConfig { #[derive(Debug, Deserialize)] #[cfg_attr(test, derive(PartialEq))] -pub struct DatabaseConfig { - pub postgres_url: Url, - pub postgres_host: String, - pub postgres_port: i32, - pub postgres_user: String, - pub postgres_password: String, - pub postgress_db: String, +#[serde(untagged)] +pub enum DatabaseConfig { + PostgresUrl { + postgres_url: Url, + }, + PostgresVars { + host: String, + port: Option, + user: String, + password: Option, + database: String, + }, +} +impl DatabaseConfig { + pub fn get_formated_postgres_url(&self) -> Url { + match self { + DatabaseConfig::PostgresUrl { postgres_url } => postgres_url.clone(), + DatabaseConfig::PostgresVars { + host, + port, + user, + password, + database, + } => { + let postgres_url_str = format!("postgres://{}@{}/{}", user, host, database); + let mut postgres_url = Url::parse(&postgres_url_str).unwrap(); + postgres_url + .set_password(password.as_deref()) + .expect("url is wrong"); + postgres_url.set_port(*port).expect("url is wrong"); + postgres_url + } + } + } } #[derive(Debug, Deserialize)] @@ -291,6 +318,8 @@ mod tests { use crate::{Config, ConfigPrefix}; + use super::DatabaseConfig; + #[test] fn test_minimal_config() { Config::parse( @@ -403,4 +432,32 @@ mod tests { test_value ); } + #[test] + fn test_url_format() { + let data = DatabaseConfig::PostgresVars { + host: String::from("postgres"), + port: Some(1234), + user: String::from("postgres"), + password: Some(String::from("postgres")), + database: String::from("postgres"), + }; + let formated_data = data.get_formated_postgres_url(); + assert_eq!( + formated_data.as_str(), + "postgres://postgres:postgres@postgres:1234/postgres" + ); + + let data = DatabaseConfig::PostgresVars { + host: String::from("postgres"), + port: None, + user: String::from("postgres"), + password: None, + database: String::from("postgres"), + }; + let formated_data = data.get_formated_postgres_url(); + assert_eq!( + formated_data.as_str(), + "postgres://postgres@postgres/postgres" + ); + } } diff --git a/service/src/config.rs b/service/src/config.rs index b7a7bacb..b904bd40 100644 --- a/service/src/config.rs +++ b/service/src/config.rs @@ -29,14 +29,9 @@ impl From for Config { url_prefix: value.service.url_prefix, free_query_auth_token: value.service.free_query_auth_token, }, - database: DatabaseConfig::format_db_config( - Some(value.database.postgres_url.into()), - Some(value.database.postgres_host), - Some(value.database.postgres_password), - Some(value.database.postgres_port.to_string()), - Some(value.database.postgres_user), - Some(value.database.postgress_db), - ), + database: DatabaseConfig { + postgres_url: value.database.get_formated_postgres_url().to_string(), + }, graph_node: Some(GraphNodeConfig { status_url: value.graph_node.status_url.into(), query_base_url: value.graph_node.query_url.into(), diff --git a/tap-agent/src/config.rs b/tap-agent/src/config.rs index 830708e7..2379de83 100644 --- a/tap-agent/src/config.rs +++ b/tap-agent/src/config.rs @@ -37,7 +37,7 @@ impl From for Config { log_level: None, }, postgres: Postgres { - postgres_url: value.database.postgres_url, + postgres_url: value.database.get_formated_postgres_url(), }, network_subgraph: NetworkSubgraph { network_subgraph_deployment: value.subgraphs.network.config.deployment_id,