Skip to content

Commit

Permalink
feat: Add option to use postgres_url or individual vars to connect
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosvdr committed Oct 3, 2024
1 parent 15d8c2e commit 1fbecce
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 48 deletions.
32 changes: 0 additions & 32 deletions common/src/indexer_service/http/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
ps_host: Option<String>,
ps_pwd: Option<String>,
ps_port: Option<String>,
ps_user: Option<String>,
ps_db: Option<String>,
) -> 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)]
Expand Down
71 changes: 64 additions & 7 deletions config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16>,
user: String,
password: Option<String>,
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)]
Expand Down Expand Up @@ -291,6 +318,8 @@ mod tests {

use crate::{Config, ConfigPrefix};

use super::DatabaseConfig;

#[test]
fn test_minimal_config() {
Config::parse(
Expand Down Expand Up @@ -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"
);
}
}
11 changes: 3 additions & 8 deletions service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,9 @@ impl From<MainConfig> 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(),
Expand Down
2 changes: 1 addition & 1 deletion tap-agent/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl From<IndexerConfig> 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,
Expand Down

0 comments on commit 1fbecce

Please sign in to comment.