-
Notifications
You must be signed in to change notification settings - Fork 140
Flexibility when establishing database connection #12
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,19 @@ def teardown_databases(self, old_config, **kwargs): | |
|
||
|
||
def settings(config, *, db_colors=False, databases=True, test_runner=True, staticfiles=True, allowed_hosts=True, logging=True, secret_key=True): | ||
if 'USE_MAX_CONN_AGE' in os.environ: | ||
use_max_conn_age = bool(os.environ['USE_MAX_CONN_AGE']) | ||
else: | ||
use_max_conn_age = True | ||
|
||
# DEVELOPMENT mode configuration. | ||
ssl_required = True | ||
if 'DEVELOPMENT' in os.environ: | ||
logger.info('Adding $DEVELOPMENT to DEVELOPMENT Django setting.') | ||
# Set the Django setting from the environment variable. | ||
config['DEVELOPMENT'] = os.environ['DEVELOPMENT'] | ||
if os.environ['DEVELOPMENT'] == 'True': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, many people like using |
||
ssl_required = False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we have a dictionary, perhaps named dj_database_config['conn_max_age'] = MAX_CONN_AGE If someone wants to use it and then here we set dj_database_config['ssl_required'] = True # or False And then below where we config['DATABASES'][db_color] = dj_database_url.parse(url, **dj_database_config) and config['DATABASES']['default'] = dj_database_url.config(**dj_database_config) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if this was simply:
I agree this particular setting is counter intuitive and tripped me up.
More specific settings could still be tweaked by updating |
||
|
||
# Database configuration. | ||
# TODO: support other database (e.g. TEAL, AMBER, etc, automatically.) | ||
|
@@ -60,13 +73,19 @@ def settings(config, *, db_colors=False, databases=True, test_runner=True, stati | |
|
||
logger.info('Adding ${} to DATABASES Django setting ({}).'.format(env, db_color)) | ||
|
||
config['DATABASES'][db_color] = dj_database_url.parse(url, conn_max_age=MAX_CONN_AGE, ssl_require=True) | ||
if use_max_conn_age: | ||
config['DATABASES'][db_color] = dj_database_url.parse(url, conn_max_age=MAX_CONN_AGE, ssl_require=True) | ||
else: | ||
config['DATABASES'][db_color] = dj_database_url.parse(url, ssl_require=True) | ||
|
||
if 'DATABASE_URL' in os.environ: | ||
logger.info('Adding $DATABASE_URL to default DATABASE Django setting.') | ||
|
||
# Configure Django for DATABASE_URL environment variable. | ||
config['DATABASES']['default'] = dj_database_url.config(conn_max_age=MAX_CONN_AGE, ssl_require=True) | ||
if use_max_conn_age: | ||
config['DATABASES']['default'] = dj_database_url.config(conn_max_age=MAX_CONN_AGE, ssl_require=ssl_required) | ||
else: | ||
config['DATABASES']['default'] = dj_database_url.config(ssl_require=ssl_required) | ||
|
||
logger.info('Adding $DATABASE_URL to TEST default DATABASE Django setting.') | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most people want
1
to meanTrue
and0
to meanFalse
but this will always evaluate toTrue
regardless of the following values:USE_MAX_CONN_AGE=1
USE_MAX_CONN_AGE=0
USE_MAX_CONN_AGE=True
USE_MAX_CONN_AGE=False
USE_MAX_CONN_AGE=false
USE_MAX_CONN_AGE=fALSE
USE_MAX_CONN_AGE=not-a-bool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sigmavirus24 @burcaw What if the line was:
tbh, this setting I used to update in my setting_prod.py with
DATABASES['defaut']['CONN_MAX_AGE'] = 600
. django's default of 0 is rather silly and adds extra connection overhead without people knowing. For most indivudals not a big deal, but across an entire infrastructure I can see why these opionated defaults are in place.