Commit 92ad14e9 authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Made prefix of environment database variables a variable

parent 0dc2ab9f
Pipeline #42286 passed with stage
in 2 minutes and 27 seconds
...@@ -177,10 +177,18 @@ class Configuration: ...@@ -177,10 +177,18 @@ class Configuration:
config, "number_cores" config, "number_cores"
) )
self.database_built_up = ConfigurationMethods.retrieve_database_credentials( self.database_built_up = ConfigurationMethods.retrieve_database_credentials(
config, "database_built_up", "test_db_built_up.env", force_config_over_hierarchies config,
"database_built_up",
"test_db_built_up.env",
"GDEIMPORTER",
force_config_over_hierarchies,
) )
self.database_gde_tiles = ConfigurationMethods.retrieve_database_credentials( self.database_gde_tiles = ConfigurationMethods.retrieve_database_credentials(
config, "database_gde_tiles", "test_db_gde_tiles.env", force_config_over_hierarchies config,
"database_gde_tiles",
"test_db_gde_tiles.env",
"GDEIMPORTER",
force_config_over_hierarchies,
) )
self.data_units_surface_threshold = ConfigurationMethods.assign_float_parameter( self.data_units_surface_threshold = ConfigurationMethods.assign_float_parameter(
config, "data_units_surface_threshold", True, -1e-15, 100.0 config, "data_units_surface_threshold", True, -1e-15, 100.0
......
...@@ -319,7 +319,7 @@ class ConfigurationMethods: ...@@ -319,7 +319,7 @@ class ConfigurationMethods:
@staticmethod @staticmethod
def retrieve_database_credentials( def retrieve_database_credentials(
config, input_parameter, env_filename, force_config_over_hierarchies config, input_parameter, env_filename, ci_variable_name, force_config_over_hierarchies
): ):
"""This function retrieves the credentials needed to (later) connect to a specific SQL """This function retrieves the credentials needed to (later) connect to a specific SQL
database. If force_config_over_hierarchies is False, it does so hieararchically, by database. If force_config_over_hierarchies is False, it does so hieararchically, by
...@@ -338,6 +338,9 @@ class ConfigurationMethods: ...@@ -338,6 +338,9 @@ class ConfigurationMethods:
Name of the desired parameter, to be searched for as a primary key of config. Name of the desired parameter, to be searched for as a primary key of config.
env_filename (str): env_filename (str):
Name of a local .env file that will be run to load environment variables. Name of a local .env file that will be run to load environment variables.
ci_variable_name (str):
Prefix of the name of the variables associated with the test databases in the CI
pipeline.
force_config_over_hierarchies (bool): force_config_over_hierarchies (bool):
If True, the contents of the .yml configuration file specified in filepath will If True, the contents of the .yml configuration file specified in filepath will
take precedence over any other hierarchy (e.g. preference of environment take precedence over any other hierarchy (e.g. preference of environment
...@@ -366,24 +369,26 @@ class ConfigurationMethods: ...@@ -366,24 +369,26 @@ class ConfigurationMethods:
if "port" in config: if "port" in config:
db_config["port"] = int(db_config["port"]) db_config["port"] = int(db_config["port"])
if "GDEIMPORTER_DB_HOST" in os.environ and not (force_config_over_hierarchies): if ("%s_DB_HOST" % (ci_variable_name)) in os.environ and not (
force_config_over_hierarchies
):
# Running the CI pipeline # Running the CI pipeline
db_config = { db_config = {
"host": os.environ.get("GDEIMPORTER_DB_HOST"), "host": os.environ.get("%s_DB_HOST" % (ci_variable_name)),
"dbname": os.environ.get("GDEIMPORTER_DB"), "dbname": os.environ.get("%s_DB" % (ci_variable_name)),
"username": os.environ.get("GDEIMPORTER_USER"), "username": os.environ.get("%s_USER" % (ci_variable_name)),
"password": os.environ.get("GDEIMPORTER_PASSWORD"), "password": os.environ.get("%s_PASSWORD" % (ci_variable_name)),
"sourceid": os.environ.get("GDEIMPORTER_SOURCEID"), "sourceid": os.environ.get("%s_SOURCEID" % (ci_variable_name)),
} }
elif os.path.isfile(env_filename) and not (force_config_over_hierarchies): elif os.path.isfile(env_filename) and not (force_config_over_hierarchies):
# Testing locally with a test database # Testing locally with a test database
load_dotenv(env_filename) load_dotenv(env_filename)
db_config = { db_config = {
"host": os.environ.get("GDEIMPORTER_LOCAL_DB_HOST"), "host": os.environ.get("%s_LOCAL_DB_HOST" % (ci_variable_name)),
"dbname": os.environ.get("GDEIMPORTER_LOCAL_DB"), "dbname": os.environ.get("%s_LOCAL_DB" % (ci_variable_name)),
"username": os.environ.get("GDEIMPORTER_LOCAL_USER"), "username": os.environ.get("%s_LOCAL_USER" % (ci_variable_name)),
"password": os.environ.get("GDEIMPORTER_LOCAL_PASSWORD"), "password": os.environ.get("%s_LOCAL_PASSWORD" % (ci_variable_name)),
"sourceid": os.environ.get("GDEIMPORTER_LOCAL_SOURCEID"), "sourceid": os.environ.get("%s_LOCAL_SOURCEID" % (ci_variable_name)),
} }
return db_config return db_config
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment