From 63434454339988e23dbdd3557c8ae07cf6e31180 Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 15 Oct 2021 15:05:55 +1100 Subject: [PATCH 1/2] Add support for backend-specific database functionality --- InvenTree/InvenTree/settings.py | 100 +++++++++++++++++++++----------- 1 file changed, 66 insertions(+), 34 deletions(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index a07324ec84..9b38c27159 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -385,39 +385,6 @@ Q_CLUSTER = { 'sync': False, } -# Markdownx configuration -# Ref: https://neutronx.github.io/django-markdownx/customization/ -MARKDOWNX_MEDIA_PATH = datetime.now().strftime('markdownx/%Y/%m/%d') - -# Markdownify configuration -# Ref: https://django-markdownify.readthedocs.io/en/latest/settings.html - -MARKDOWNIFY_WHITELIST_TAGS = [ - 'a', - 'abbr', - 'b', - 'blockquote', - 'em', - 'h1', 'h2', 'h3', - 'i', - 'img', - 'li', - 'ol', - 'p', - 'strong', - 'ul' -] - -MARKDOWNIFY_WHITELIST_ATTRS = [ - 'href', - 'src', - 'alt', -] - -MARKDOWNIFY_BLEACH = False - -DATABASES = {} - """ Configure the database backend based on the user-specified values. @@ -484,7 +451,41 @@ logger.info(f"DB_ENGINE: {db_engine}") logger.info(f"DB_NAME: {db_name}") logger.info(f"DB_HOST: {db_host}") -DATABASES['default'] = db_config +""" +In addition to base-level database configuration, we may wish to specify specific options to the database backend +Ref: https://docs.djangoproject.com/en/3.2/ref/settings/#std:setting-OPTIONS + +Various database options can be specified in config.yaml if required: + +""" + +db_options = db_config.get('OPTIONS', db_config.get('options', {})) + +# Specific options for postgres backend +if 'postgres' in db_engine: + from psycopg2.extensions import ISOLATION_LEVEL_READ_COMMITTED, ISOLATION_LEVEL_SERIALIZABLE + + # Connection timeout + if 'connect_timeout' not in db_options: + db_options['connect_timeout'] = int(os.getenv('INVENTREE_DB_TIMEOUT'), 2) + +# Specific options for MySql / MariaDB backend +if 'mysql' in db_engine: + # TODO + pass + +# Specific options for sqlite backend +if 'sqlite' in db_engine: + # TODO + pass + +# Provide OPTIONS dict back to the database configuration dict +db_config['OPTIONS'] = db_options + +DATABASES = { + 'default': db_config +} + CACHES = { 'default': { @@ -683,3 +684,34 @@ ACCOUNT_FORMS = { SOCIALACCOUNT_ADAPTER = 'InvenTree.forms.CustomSocialAccountAdapter' ACCOUNT_ADAPTER = 'InvenTree.forms.CustomAccountAdapter' + +# Markdownx configuration +# Ref: https://neutronx.github.io/django-markdownx/customization/ +MARKDOWNX_MEDIA_PATH = datetime.now().strftime('markdownx/%Y/%m/%d') + +# Markdownify configuration +# Ref: https://django-markdownify.readthedocs.io/en/latest/settings.html + +MARKDOWNIFY_WHITELIST_TAGS = [ + 'a', + 'abbr', + 'b', + 'blockquote', + 'em', + 'h1', 'h2', 'h3', + 'i', + 'img', + 'li', + 'ol', + 'p', + 'strong', + 'ul' +] + +MARKDOWNIFY_WHITELIST_ATTRS = [ + 'href', + 'src', + 'alt', +] + +MARKDOWNIFY_BLEACH = False From 603f7d5f45ce694f5fa2849643a2138a1c5068f6 Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 15 Oct 2021 15:13:06 +1100 Subject: [PATCH 2/2] Fixes --- InvenTree/InvenTree/settings.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 9b38c27159..2095cab533 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -454,11 +454,9 @@ logger.info(f"DB_HOST: {db_host}") """ In addition to base-level database configuration, we may wish to specify specific options to the database backend Ref: https://docs.djangoproject.com/en/3.2/ref/settings/#std:setting-OPTIONS +""" -Various database options can be specified in config.yaml if required: - -""" - +# 'OPTIONS' or 'options' can be specified in config.yaml db_options = db_config.get('OPTIONS', db_config.get('options', {})) # Specific options for postgres backend @@ -467,7 +465,15 @@ if 'postgres' in db_engine: # Connection timeout if 'connect_timeout' not in db_options: - db_options['connect_timeout'] = int(os.getenv('INVENTREE_DB_TIMEOUT'), 2) + db_options['connect_timeout'] = int(os.getenv('INVENTREE_DB_TIMEOUT', 2)) + + # Postgres's default isolation level is Read Committed which is + # normally fine, but most developers think the database server is + # actually going to do Serializable type checks on the queries to + # protect against simultaneous changes. + if 'isolation_level' not in db_options: + serializable = _is_true(os.getenv("PG_ISOLATION_SERIALIZABLE", "true")) + db_options['isolation_level'] = ISOLATION_LEVEL_SERIALIZABLE if serializable else ISOLATION_LEVEL_READ_COMMITTED # Specific options for MySql / MariaDB backend if 'mysql' in db_engine: