Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion common/src/indexer_service/http/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use thegraph_core::{Address, DeploymentId};
pub struct DatabaseConfig {
pub postgres_url: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SubgraphConfig {
#[serde(default)]
Expand Down
7 changes: 7 additions & 0 deletions config/maximal-config-example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ port = 7300
# that is used by the `indexer-agent`. It is expected that `indexer-agent` will create
# the necessary tables.
postgres_url = "postgres://postgres@postgres:5432/postgres"
# You can also use the following fields to specify the connection details separately.
# either use `postgres_url` or the following fields.
host = "postgres-host"
port = 5432
user = "user"
password = "pwd"
database = "postgres"

[graph_node]
# URL to your graph-node's query endpoint
Expand Down
7 changes: 7 additions & 0 deletions config/minimal-config-example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ operator_mnemonic = "celery smart tip orange scare van steel radio dragon joy al
# that is used by the `indexer-agent`. It is expected that `indexer-agent` will create
# the necessary tables.
postgres_url = "postgres://postgres@postgres:5432/postgres"
# You can also use the following fields to specify the connection details separately.
# either use `postgres_url` or the following fields.
host = "postgres-host"
port = 5432
user = "user"
password = "pwd"
database = "postgres"

[graph_node]
# URL to your graph-node's query endpoint
Expand Down
69 changes: 67 additions & 2 deletions config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,43 @@ pub struct IndexerConfig {

#[derive(Debug, Deserialize)]
#[cfg_attr(test, derive(PartialEq))]
pub struct DatabaseConfig {
pub postgres_url: Url,
#[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,
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).expect("Failed to parse database_url");
postgres_url
.set_password(password.as_deref())
.expect("Failed to set password for database");
postgres_url
.set_port(port)
.expect("Failed to set port for database");
postgres_url
}
}
}
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -286,6 +321,8 @@ mod tests {

use crate::{Config, ConfigPrefix};

use super::DatabaseConfig;

#[test]
fn test_minimal_config() {
Config::parse(
Expand Down Expand Up @@ -398,4 +435,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"
);
}
}
2 changes: 1 addition & 1 deletion service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl From<MainConfig> for Config {
free_query_auth_token: value.service.free_query_auth_token,
},
database: DatabaseConfig {
postgres_url: value.database.postgres_url.into(),
postgres_url: value.database.get_formated_postgres_url().to_string(),
},
graph_node: Some(GraphNodeConfig {
status_url: value.graph_node.status_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
Loading