Skip to content

Commit 5e711a5

Browse files
committed
fix: Add option to give either postgres_url or values individually
1 parent 676a437 commit 5e711a5

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed

common/src/indexer_service/http/config.rs

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

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

910
#[derive(Clone, Debug, Deserialize, Serialize)]
1011
pub struct DatabaseConfig {
1112
pub postgres_url: String,
1213
}
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+
}
1344

1445
#[derive(Clone, Debug, Deserialize, Serialize)]
1546
pub struct SubgraphConfig {

config/maximal-config-example.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ port = 7300
3333
# that is used by the `indexer-agent`. It is expected that `indexer-agent` will create
3434
# the necessary tables.
3535
postgres_url = "postgres://postgres@postgres:5432/postgres"
36+
# You can also use the following fields to specify the connection details separately.
37+
# either use `postgres_url` or the following fields.
38+
postgres_host = "postgres-host"
39+
postgres_port = 5432
40+
postgres_user = "user"
41+
postgres_password = "pwd"
42+
postgress_db = "postgres"
3643

3744
[graph_node]
3845
# URL to your graph-node's query endpoint

config/minimal-config-example.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ operator_mnemonic = "celery smart tip orange scare van steel radio dragon joy al
1919
# that is used by the `indexer-agent`. It is expected that `indexer-agent` will create
2020
# the necessary tables.
2121
postgres_url = "postgres://postgres@postgres:5432/postgres"
22+
# You can also use the following fields to specify the connection details separately.
23+
# either use `postgres_url` or the following fields.
24+
postgres_host = "postgres-host"
25+
postgres_port = 5432
26+
postgres_user = "user"
27+
postgres_password = "pwd"
28+
postgress_db = "postgres"
2229

2330
[graph_node]
2431
# URL to your graph-node's query endpoint

config/src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ pub struct IndexerConfig {
149149
#[cfg_attr(test, derive(PartialEq))]
150150
pub struct DatabaseConfig {
151151
pub postgres_url: Url,
152+
pub postgres_host: String,
153+
pub postgres_port: String,
154+
pub postgres_user: String,
155+
pub postgres_password: String,
156+
pub postgress_db: String,
152157
}
153158

154159
#[derive(Debug, Deserialize)]

service/src/config.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ 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 {
33-
postgres_url: value.database.postgres_url.into(),
34-
},
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),
37+
Some(value.database.postgres_user),
38+
Some(value.database.postgress_db),
39+
),
3540
graph_node: Some(GraphNodeConfig {
3641
status_url: value.graph_node.status_url.into(),
3742
query_base_url: value.graph_node.query_url.into(),

0 commit comments

Comments
 (0)