Skip to content

Commit 9d68dfe

Browse files
committed
feat: Fix so that if given both types of vars for database to fail and added a test
1 parent 0545a72 commit 9d68dfe

File tree

4 files changed

+69
-10
lines changed

4 files changed

+69
-10
lines changed

common/src/indexer_service/http/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use thegraph_core::{Address, DeploymentId};
1010
pub struct DatabaseConfig {
1111
pub postgres_url: String,
1212
}
13+
1314
#[derive(Clone, Debug, Deserialize, Serialize)]
1415
pub struct SubgraphConfig {
1516
#[serde(default)]

config/maximal-config-example.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ port = 7300
3535
postgres_url = "postgres://postgres@postgres:5432/postgres"
3636
# You can also use the following fields to specify the connection details separately.
3737
# either use `postgres_url` or the following fields.
38-
host = "postgres-host"
39-
port = 5432
40-
user = "user"
41-
password = "pwd"
42-
database = "postgres"
38+
# host = "postgres-host"
39+
# port = 5432
40+
# user = "user"
41+
# password = "pwd"
42+
# database = "postgres"
4343

4444
[graph_node]
4545
# URL to your graph-node's query endpoint

config/minimal-config-example.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ operator_mnemonic = "celery smart tip orange scare van steel radio dragon joy al
2121
postgres_url = "postgres://postgres@postgres:5432/postgres"
2222
# You can also use the following fields to specify the connection details separately.
2323
# either use `postgres_url` or the following fields.
24-
host = "postgres-host"
25-
port = 5432
26-
user = "user"
27-
password = "pwd"
28-
database = "postgres"
24+
# host = "postgres-host"
25+
# port = 5432
26+
# user = "user"
27+
# password = "pwd"
28+
# database = "postgres"
2929

3030
[graph_node]
3131
# URL to your graph-node's query endpoint

config/src/config.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ pub struct IndexerConfig {
148148
#[derive(Debug, Deserialize)]
149149
#[cfg_attr(test, derive(PartialEq))]
150150
#[serde(untagged)]
151+
#[serde(deny_unknown_fields)]
151152
pub enum DatabaseConfig {
152153
PostgresUrl {
153154
postgres_url: Url,
@@ -463,4 +464,61 @@ mod tests {
463464
"postgres://postgres@postgres/postgres"
464465
);
465466
}
467+
468+
// Test that we can fill in mandatory config fields missing from the config file with
469+
// environment variables
470+
#[sealed_test(files = ["minimal-config-example.toml"])]
471+
fn test_change_db_config_with_individual_vars() {
472+
let mut minimal_config: toml::Value = toml::from_str(
473+
fs::read_to_string("minimal-config-example.toml")
474+
.unwrap()
475+
.as_str(),
476+
)
477+
.unwrap();
478+
// Remove the database.postgres_url field from minimal config
479+
minimal_config
480+
.get_mut("database")
481+
.unwrap()
482+
.as_table_mut()
483+
.unwrap()
484+
.remove("postgres_url");
485+
486+
let database_table = minimal_config
487+
.get_mut("database")
488+
.unwrap()
489+
.as_table_mut()
490+
.unwrap();
491+
database_table.insert(
492+
"host".to_string(),
493+
toml::Value::String("postgres".to_string()),
494+
);
495+
database_table.insert(
496+
"user".to_string(),
497+
toml::Value::String("postgres".to_string()),
498+
);
499+
database_table.insert(
500+
"database".to_string(),
501+
toml::Value::String("postgres".to_string()),
502+
);
503+
504+
// Save the modified minimal config to a named temporary file using tempfile
505+
let temp_minimal_config_path = tempfile::NamedTempFile::new().unwrap();
506+
fs::write(
507+
temp_minimal_config_path.path(),
508+
toml::to_string(&minimal_config).unwrap(),
509+
)
510+
.unwrap();
511+
512+
// Parse the config with new datbase vars
513+
let config = Config::parse(
514+
ConfigPrefix::Service,
515+
&PathBuf::from(temp_minimal_config_path.path()),
516+
)
517+
.unwrap();
518+
519+
assert_eq!(
520+
config.database.get_formated_postgres_url().as_str(),
521+
"postgres://postgres@postgres/postgres"
522+
);
523+
}
466524
}

0 commit comments

Comments
 (0)