2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-29 12:06:44 +00:00

Environment variables take preference!

This commit is contained in:
Oliver Walters 2021-03-31 21:40:19 +11:00
parent 5d141e3568
commit 566c3af39e

View File

@ -331,6 +331,8 @@ logger.info("Configuring database backend:")
# Extract database configuration from the config.yaml file # Extract database configuration from the config.yaml file
db_config = CONFIG.get('database', {}) db_config = CONFIG.get('database', {})
# Environment variables take preference over config file!
# If a particular database option is not specified in the config file, # If a particular database option is not specified in the config file,
# look for it in the environmental variables # look for it in the environmental variables
# e.g. INVENTREE_DB_NAME / INVENTREE_DB_USER / etc # e.g. INVENTREE_DB_NAME / INVENTREE_DB_USER / etc
@ -338,16 +340,14 @@ db_config = CONFIG.get('database', {})
db_keys = ['ENGINE', 'NAME', 'USER', 'PASSWORD', 'HOST', 'PORT'] db_keys = ['ENGINE', 'NAME', 'USER', 'PASSWORD', 'HOST', 'PORT']
for key in db_keys: for key in db_keys:
if key not in db_config: # First, check the environment variables
logger.debug(f" - Missing {key} value: Looking for environment variable INVENTREE_DB_{key}") env_key = f"INVENTREE_DB_{key}"
env_key = f'INVENTREE_DB_{key}' env_var = os.environ.get(env_key, None)
env_var = os.environ.get(env_key, None)
if env_var is not None: if env_var:
logger.info(f'Using environment variable INVENTREE_DB_{key}') logger.info(f"{env_key}={env_var}")
db_config[key] = env_var # Override configuration value
else: db_config[key] = env_var
logger.debug(f' INVENTREE_DB_{key} not found in environment variables')
# Check that required database configuration options are specified # Check that required database configuration options are specified
reqiured_keys = ['ENGINE', 'NAME'] reqiured_keys = ['ENGINE', 'NAME']