Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Flexibility when establishing database connection #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

burcaw
Copy link

@burcaw burcaw commented Mar 26, 2018

Here is a pass at providing flexibility in establishing the database connection per the following two issues:

Permit disabling 'require_ssl' when running locally for development
#10

Ability to Override MAX_CONN_AGE
#11

I welcome any feedback to make these changes acceptable for inclusion.

@@ -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'])
Copy link
Contributor

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 mean True and 0 to mean False but this will always evaluate to True 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

Copy link

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:

try:
   # already defined in settings before
   MAX_CONN_AGE
except NameError:
    # assuming no preference. set a sensible default
    # 600 doesn't impact local and makes heroku's postgres servers happier
   MAX_CONN_AGE = 600

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.

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':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, many people like using 1 and 0 for boolean values. This is very limiting

# Set the Django setting from the environment variable.
config['DEVELOPMENT'] = os.environ['DEVELOPMENT']
if os.environ['DEVELOPMENT'] == 'True':
ssl_required = False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we have a dictionary, perhaps named dj_database_config that aggregates these options. So above we would set

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 .parse() or .config() we just have one call, e.g.,

config['DATABASES'][db_color] = dj_database_url.parse(url, **dj_database_config)

and

config['DATABASES']['default'] = dj_database_url.config(**dj_database_config)

Copy link

@kcolton kcolton Oct 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sigmavirus24 @burcaw

What if this was simply:

ssl_require = not DEBUG

I agree this particular setting is counter intuitive and tripped me up.

What if we have a dictionary, perhaps named dj_database_config that aggregates these options. So above we would set

More specific settings could still be tweaked by updating DATABASES['default'] dict after django_heroku does its thing, so there is already a way to tweak settings. This one, however, would be nice to "just work".

Copy link

@ochui ochui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Database configuration should be flexible. Thanks, am currently using this fork

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants