django-environ icon indicating copy to clipboard operation
django-environ copied to clipboard

Add support for CONN_HEALTH_CHECKS and OPTIONS

Open lorddaedra opened this issue 1 year ago • 8 comments

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...

lorddaedra avatar Aug 22 '22 07:08 lorddaedra

https://github.com/joke2k/django-environ/pull/418

denys-pidlisnyi avatar Sep 16 '22 11:09 denys-pidlisnyi

Implemented in https://github.com/joke2k/django-environ/pull/https://github.com/joke2k/django-environ/pull/418. Thank you for contributing!

sergeyklay avatar Sep 20 '22 07:09 sergeyklay

@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

lorddaedra avatar Sep 20 '22 10:09 lorddaedra

Feel free to make a PR :)

sergeyklay avatar Sep 20 '22 11:09 sergeyklay

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...

lorddaedra avatar Sep 20 '22 17:09 lorddaedra

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.

stat1c-void avatar Jan 31 '23 17:01 stat1c-void

@sergeyklay it would be useful if you could bump the package version and release it so that this change is available on PyPi

Bensge avatar Feb 21 '23 13:02 Bensge

@Bensge I'm on this track.

sergeyklay avatar Mar 01 '23 19:03 sergeyklay

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:).

pataquets avatar Apr 17 '24 11:04 pataquets

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?

lorddaedra avatar Apr 17 '24 17:04 lorddaedra

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)

pataquets avatar Apr 17 '24 18:04 pataquets

I agree with you, we can open new issue if needed for pool and server_side_binding.

There is nothing special with them.

lorddaedra avatar Apr 17 '24 18:04 lorddaedra