From c846e2e65a664c50322f17bba2098d4a18c605e2 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 31 Mar 2021 20:39:22 +1100 Subject: [PATCH] Use env variables rather than custom ci scripts --- .github/workflows/coverage.yaml | 5 +- .github/workflows/mariadb.yaml | 15 +++- .github/workflows/mysql.yaml | 8 ++ .github/workflows/postgresql.yaml | 9 +++ InvenTree/InvenTree/ci_mysql.py | 19 ----- InvenTree/InvenTree/ci_postgresql.py | 19 ----- InvenTree/InvenTree/settings.py | 107 ++++++++++++--------------- 7 files changed, 78 insertions(+), 104 deletions(-) delete mode 100644 InvenTree/InvenTree/ci_mysql.py delete mode 100644 InvenTree/InvenTree/ci_postgresql.py diff --git a/.github/workflows/coverage.yaml b/.github/workflows/coverage.yaml index 52364c558e..50e9cd6587 100644 --- a/.github/workflows/coverage.yaml +++ b/.github/workflows/coverage.yaml @@ -1,6 +1,6 @@ # Perform CI checks, and calculate code coverage -name: Code Coverage +name: SQLite on: ["push", "pull_request"] @@ -10,10 +10,11 @@ jobs: # These tests are used for code coverage analysis coverage: runs-on: ubuntu-latest + env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} INVENTREE_DB_NAME: './test_db.sqlite' - INVENTREE_DB_ENGINE: sqlite3 + INVENTREE_DB_ENGINE: django.db.backends.sqlite3 steps: - name: Checkout Code diff --git a/.github/workflows/mariadb.yaml b/.github/workflows/mariadb.yaml index 98d5c833d0..b400785690 100644 --- a/.github/workflows/mariadb.yaml +++ b/.github/workflows/mariadb.yaml @@ -7,6 +7,15 @@ jobs: test: runs-on: ubuntu-latest + env: + # Database backend configuration + INVENTREE_DB_ENGINE: django.db.backends.mysql + INVENTREE_DB_USER: root + INVENTREE_DB_PASSWORD: password + INVENTREE_DB_HOST: '127.0.0.1' + INVENTREE_DB_PORT: 3306 + + services: mariadb: image: mariadb:latest @@ -32,6 +41,6 @@ jobs: pip3 install mysqlclient invoke install - name: Run Tests - run: | - cd InvenTree - python3 manage.py test --settings=InvenTree.ci_mysql \ No newline at end of file + run: | + cd InvenTree + python3 manage.py test --settings=InvenTree.ci_mysql diff --git a/.github/workflows/mysql.yaml b/.github/workflows/mysql.yaml index 38a9bda596..232b5fc97e 100644 --- a/.github/workflows/mysql.yaml +++ b/.github/workflows/mysql.yaml @@ -9,6 +9,14 @@ jobs: test: runs-on: ubuntu-latest + env: + # Database backend configuration + INVENTREE_DB_ENGINE: django.db.backends.mysql + INVENTREE_DB_USER: root + INVENTREE_DB_PASSWORD: password + INVENTREE_DB_HOST: '127.0.0.1' + INVENTREE_DB_PORT: 3306 + services: mysql: image: mysql:latest diff --git a/.github/workflows/postgresql.yaml b/.github/workflows/postgresql.yaml index e0986c1e50..8ccb1912b8 100644 --- a/.github/workflows/postgresql.yaml +++ b/.github/workflows/postgresql.yaml @@ -9,6 +9,15 @@ jobs: test: runs-on: ubuntu-latest + env: + # Database backend configuration + INVENTREE_DB_ENGINE: django.db.backends.postgresql + INVENTREE_DB_USER: inventree + INVENTREE_DB_PASSWORD: password + INVENTREE_DB_HOST: '127.0.0.1' + INVENTREE_DB_PORT: 5432 + + services: postgres: image: postgres diff --git a/InvenTree/InvenTree/ci_mysql.py b/InvenTree/InvenTree/ci_mysql.py deleted file mode 100644 index 74570339d1..0000000000 --- a/InvenTree/InvenTree/ci_mysql.py +++ /dev/null @@ -1,19 +0,0 @@ -""" -Configuration file for running tests against a MySQL database. -""" - -from InvenTree.settings import * - -# Override the 'test' database -if 'test' in sys.argv: - print('InvenTree: Running tests - Using MySQL test database') - - DATABASES['default'] = { - # Ensure mysql backend is being used - 'ENGINE': 'django.db.backends.mysql', - 'NAME': 'inventree', - 'USER': 'root', - 'PASSWORD': 'password', - 'HOST': '127.0.0.1', - 'PORT': '3306', - } diff --git a/InvenTree/InvenTree/ci_postgresql.py b/InvenTree/InvenTree/ci_postgresql.py deleted file mode 100644 index 52ebc57c7f..0000000000 --- a/InvenTree/InvenTree/ci_postgresql.py +++ /dev/null @@ -1,19 +0,0 @@ -""" -Configuration file for running tests against a MySQL database. -""" - -from InvenTree.settings import * - -# Override the 'test' database -if 'test' in sys.argv: - print('InvenTree: Running tests - Using PostGreSQL test database') - - DATABASES['default'] = { - # Ensure postgresql backend is being used - 'ENGINE': 'django.db.backends.postgresql', - 'NAME': 'inventree_test_db', - 'USER': 'inventree', - 'PASSWORD': 'password', - 'HOST': '127.0.0.1', - 'PORT': '5432' - } diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 1a298240bc..4e9bab3bb0 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -319,83 +319,68 @@ MARKDOWNIFY_BLEACH = False DATABASES = {} """ -When running unit tests, enforce usage of sqlite3 database, -so that the tests can be run in RAM without any setup requirements +Configure the database backend based on the user-specified values. + +- Primarily this configuration happens in the config.yaml file +- However there may be reason to configure the DB via environmental variables +- The following code lets the user "mix and match" database configuration """ -if 'test' in sys.argv: - logger.info('InvenTree: Running tests - Using sqlite3 memory database') - DATABASES['default'] = { - # Ensure sqlite3 backend is being used - 'ENGINE': 'django.db.backends.sqlite3', - # Doesn't matter what the database is called, it is executed in RAM - 'NAME': 'ram_test_db.sqlite3', - } -# Database backend selection -else: - """ - Configure the database backend based on the user-specified values. - - - Primarily this configuration happens in the config.yaml file - - However there may be reason to configure the DB via environmental variables - - The following code lets the user "mix and match" database configuration - """ +logger.info("Configuring database backend:") - logger.info("Configuring database backend:") +# Extract database configuration from the config.yaml file +db_config = CONFIG.get('database', {}) - # Extract database configuration from the config.yaml file - db_config = CONFIG.get('database', {}) +# If a particular database option is not specified in the config file, +# look for it in the environmental variables +# e.g. INVENTREE_DB_NAME / INVENTREE_DB_USER / etc - # If a particular database option is not specified in the config file, - # look for it in the environmental variables - # e.g. INVENTREE_DB_NAME / INVENTREE_DB_USER / etc +db_keys = ['ENGINE', 'NAME', 'USER', 'PASSWORD', 'HOST', 'PORT'] - db_keys = ['ENGINE', 'NAME', 'USER', 'PASSWORD', 'HOST', 'PORT'] +for key in db_keys: + if key not in db_config: + logger.debug(f" - Missing {key} value: Looking for environment variable INVENTREE_DB_{key}") + env_key = f'INVENTREE_DB_{key}' + env_var = os.environ.get(env_key, None) - for key in db_keys: - if key not in db_config: - logger.debug(f" - Missing {key} value: Looking for environment variable INVENTREE_DB_{key}") - env_key = f'INVENTREE_DB_{key}' - env_var = os.environ.get(env_key, None) + if env_var is not None: + logger.info(f'Using environment variable INVENTREE_DB_{key}') + db_config[key] = env_var + else: + logger.debug(f' INVENTREE_DB_{key} not found in environment variables') - if env_var is not None: - logger.info(f'Using environment variable INVENTREE_DB_{key}') - db_config[key] = env_var - else: - logger.debug(f' INVENTREE_DB_{key} not found in environment variables') +# Check that required database configuration options are specified +reqiured_keys = ['ENGINE', 'NAME'] - # Check that required database configuration options are specified - reqiured_keys = ['ENGINE', 'NAME'] +for key in reqiured_keys: + if key not in db_config: + error_msg = f'Missing required database configuration value {key} in config.yaml' + logger.error(error_msg) - for key in reqiured_keys: - if key not in db_config: - error_msg = f'Missing required database configuration value {key} in config.yaml' - logger.error(error_msg) + print('Error: ' + error_msg) + sys.exit(-1) - print('Error: ' + error_msg) - sys.exit(-1) +""" +Special considerations for the database 'ENGINE' setting. +It can be specified in config.yaml (or envvar) as either (for example): +- sqlite3 +- django.db.backends.sqlite3 +- django.db.backends.postgresql +""" - """ - Special considerations for the database 'ENGINE' setting. - It can be specified in config.yaml (or envvar) as either (for example): - - sqlite3 - - django.db.backends.sqlite3 - - django.db.backends.postgresql - """ +db_engine = db_config['ENGINE'] - db_engine = db_config['ENGINE'] +if db_engine.lower() in ['sqlite3', 'postgresql', 'mysql']: + # Prepend the required python module string + db_engine = f'django.db.backends.{db_engine.lower()}' + db_config['ENGINE'] = db_engine - if db_engine.lower() in ['sqlite3', 'postgresql', 'mysql']: - # Prepend the required python module string - db_engine = f'django.db.backends.{db_engine.lower()}' - db_config['ENGINE'] = db_engine +db_name = db_config['NAME'] - db_name = db_config['NAME'] +logger.info(f"Database ENGINE: '{db_engine}'") +logger.info(f"Database NAME: '{db_name}'") - logger.info(f"Database ENGINE: '{db_engine}'") - logger.info(f"Database NAME: '{db_name}'") - - DATABASES['default'] = db_config +DATABASES['default'] = db_config CACHES = { 'default': {