Skip to content

Commit 1fbecce

Browse files
committed
feat: Add option to use postgres_url or individual vars to connect
1 parent 15d8c2e commit 1fbecce

File tree

4 files changed

+68
-48
lines changed

4 files changed

+68
-48
lines changed

common/src/indexer_service/http/config.rs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,11 @@ use std::net::SocketAddr;
55

66
use serde::{Deserialize, Serialize};
77
use thegraph_core::{Address, DeploymentId};
8-
use tracing::warn;
98

109
#[derive(Clone, Debug, Deserialize, Serialize)]
1110
pub struct DatabaseConfig {
1211
pub postgres_url: String,
1312
}
14-
impl DatabaseConfig {
15-
pub fn format_db_config(
16-
ps_url: Option<String>,
17-
ps_host: Option<String>,
18-
ps_pwd: Option<String>,
19-
ps_port: Option<String>,
20-
ps_user: Option<String>,
21-
ps_db: Option<String>,
22-
) -> Self {
23-
let db_config = (ps_url, ps_host, ps_pwd, ps_port, ps_user, ps_db);
24-
match db_config {
25-
(Some(url), ..) if !url.is_empty() => DatabaseConfig { postgres_url: url },
26-
(None, Some(host), Some(pwd), Some(port), Some(user), Some(dbname)) => {
27-
let postgres_url =
28-
format!("postgres://{}:{}@{}:{}/{}", user, pwd, host, port, dbname);
29-
DatabaseConfig { postgres_url }
30-
}
31-
_ => {
32-
warn!(
33-
"Some configuration values are missing for database values, please make sure you either \
34-
pass `postgres_url` or pass all the variables to connect to the database
35-
");
36-
// This will eventually fail to connect
37-
DatabaseConfig {
38-
postgres_url: String::new(),
39-
}
40-
}
41-
}
42-
}
43-
}
44-
4513
#[derive(Clone, Debug, Deserialize, Serialize)]
4614
pub struct SubgraphConfig {
4715
#[serde(default)]

config/src/config.rs

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,40 @@ pub struct IndexerConfig {
147147

148148
#[derive(Debug, Deserialize)]
149149
#[cfg_attr(test, derive(PartialEq))]
150-
pub struct DatabaseConfig {
151-
pub postgres_url: Url,
152-
pub postgres_host: String,
153-
pub postgres_port: i32,
154-
pub postgres_user: String,
155-
pub postgres_password: String,
156-
pub postgress_db: String,
150+
#[serde(untagged)]
151+
pub enum DatabaseConfig {
152+
PostgresUrl {
153+
postgres_url: Url,
154+
},
155+
PostgresVars {
156+
host: String,
157+
port: Option<u16>,
158+
user: String,
159+
password: Option<String>,
160+
database: String,
161+
},
162+
}
163+
impl DatabaseConfig {
164+
pub fn get_formated_postgres_url(&self) -> Url {
165+
match self {
166+
DatabaseConfig::PostgresUrl { postgres_url } => postgres_url.clone(),
167+
DatabaseConfig::PostgresVars {
168+
host,
169+
port,
170+
user,
171+
password,
172+
database,
173+
} => {
174+
let postgres_url_str = format!("postgres://{}@{}/{}", user, host, database);
175+
let mut postgres_url = Url::parse(&postgres_url_str).unwrap();
176+
postgres_url
177+
.set_password(password.as_deref())
178+
.expect("url is wrong");
179+
postgres_url.set_port(*port).expect("url is wrong");
180+
postgres_url
181+
}
182+
}
183+
}
157184
}
158185

159186
#[derive(Debug, Deserialize)]
@@ -291,6 +318,8 @@ mod tests {
291318

292319
use crate::{Config, ConfigPrefix};
293320

321+
use super::DatabaseConfig;
322+
294323
#[test]
295324
fn test_minimal_config() {
296325
Config::parse(
@@ -403,4 +432,32 @@ mod tests {
403432
test_value
404433
);
405434
}
435+
#[test]
436+
fn test_url_format() {
437+
let data = DatabaseConfig::PostgresVars {
438+
host: String::from("postgres"),
439+
port: Some(1234),
440+
user: String::from("postgres"),
441+
password: Some(String::from("postgres")),
442+
database: String::from("postgres"),
443+
};
444+
let formated_data = data.get_formated_postgres_url();
445+
assert_eq!(
446+
formated_data.as_str(),
447+
"postgres://postgres:postgres@postgres:1234/postgres"
448+
);
449+
450+
let data = DatabaseConfig::PostgresVars {
451+
host: String::from("postgres"),
452+
port: None,
453+
user: String::from("postgres"),
454+
password: None,
455+
database: String::from("postgres"),
456+
};
457+
let formated_data = data.get_formated_postgres_url();
458+
assert_eq!(
459+
formated_data.as_str(),
460+
"postgres://postgres@postgres/postgres"
461+
);
462+
}
406463
}

service/src/config.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,9 @@ impl From<MainConfig> for Config {
2929
url_prefix: value.service.url_prefix,
3030
free_query_auth_token: value.service.free_query_auth_token,
3131
},
32-
database: DatabaseConfig::format_db_config(
33-
Some(value.database.postgres_url.into()),
34-
Some(value.database.postgres_host),
35-
Some(value.database.postgres_password),
36-
Some(value.database.postgres_port.to_string()),
37-
Some(value.database.postgres_user),
38-
Some(value.database.postgress_db),
39-
),
32+
database: DatabaseConfig {
33+
postgres_url: value.database.get_formated_postgres_url().to_string(),
34+
},
4035
graph_node: Some(GraphNodeConfig {
4136
status_url: value.graph_node.status_url.into(),
4237
query_base_url: value.graph_node.query_url.into(),

tap-agent/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl From<IndexerConfig> for Config {
3737
log_level: None,
3838
},
3939
postgres: Postgres {
40-
postgres_url: value.database.postgres_url,
40+
postgres_url: value.database.get_formated_postgres_url(),
4141
},
4242
network_subgraph: NetworkSubgraph {
4343
network_subgraph_deployment: value.subgraphs.network.config.deployment_id,

0 commit comments

Comments
 (0)