django-environ
django-environ copied to clipboard
Add support for CONN_HEALTH_CHECKS and OPTIONS
Hello!
How to convert my config to django-environ? I'm worry about CONN_HEALTH_CHECKS and OPTIONS.
DATABASES = types.MappingProxyType({
'default': {
'ENGINE': 'django.db.backends.postgresql',
'HOST': '127.0.0.1',
'NAME': 'devdatabase',
'CONN_MAX_AGE': None,
'CONN_HEALTH_CHECKS': True,
'OPTIONS': {'sslmode': 'require'} if IS_PRODUCTION else {},
'PASSWORD': 'django-insecure-database_password',
'PORT': '5432',
'USER': 'devdatabaseuser',
},
})
Names like CONN_MAX_AGE
are hardcoded in https://github.com/joke2k/django-environ/blob/main/environ/environ.py#L132-L137...
https://github.com/joke2k/django-environ/pull/418
Implemented in https://github.com/joke2k/django-environ/pull/https://github.com/joke2k/django-environ/pull/418. Thank you for contributing!
@denys-pidlisnyi @sergeyklay What's about OPTIONS? (It's not possible to use sslmode, isolation_level etc. settings without OPTIONS)
https://docs.djangoproject.com/en/4.1/ref/databases/#isolation-level https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
Feel free to make a PR :)
To be honestly, I use another package, dj-database-url
. "Sister issue": https://github.com/jazzband/dj-database-url/issues/180 :-)
this is my current settings.py
, it works fine without any package code modifications:
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
DATABASES = types.MappingProxyType({
'default': dj_database_url.parse(
url=os.environ['DATABASE_URL'],
engine='django.db.backends.postgresql',
conn_max_age=None,
ssl_require=not IS_DEVELOPMENT and not bool(os.getenv('CI', default=False)),
) | {'CONN_HEALTH_CHECKS': True},
})
I just commented because of you closed this issue (may be by mistake) but only half of issue was fixed... May be it should stay open but it's up to you...
I wonder - are those changes really needed?
Here's example code for adding non-standard params to DB configuration:
DATABASES = {
'default': {
**env.db_url(),
'CONN_MAX_AGE': None,
'CONN_HEALTH_CHECKS': True,
'TEST': {
'MIGRATE': False
}
}
}
OPTIONS
can be done in a similar way.
@sergeyklay it would be useful if you could bump the package version and release it so that this change is available on PyPi
@Bensge I'm on this track.
Looks like OPTIONS
is already supported and released:
https://github.com/joke2k/django-environ/blob/df301b607f287df049b4efeda14717fcdbe123b1/environ/environ.py#L590-L597
@lorddaedra: fancy to close the issue?
Also, I think @stat1c-void smart trick is worth a "tips" section snippet. I'll get to it as soon as I have some more time (unless someone beats to me at it :smile:).
About trick: yes, it's useful workaround (but I usually prefer modern style https://peps.python.org/pep-0584/ )
About options: may be we should add pool
and server_side_binding
to supported options too.
https://docs.djangoproject.com/en/dev/ref/databases/#connection-pool
Can we somehow do not hardcode them but load on app start instead?
Thanks for the trick, @lorddaedra! I didn't knew about it. About options: if options is supported (in general), wouldn't it be better to file separate issues for specific options? (unless I'm missing sthg important about options, not very familiar using it, please bear with my newbiness)
I agree with you, we can open new issue if needed for pool
and server_side_binding
.
There is nothing special with them.