pytest-dev / pytest-dev/pytest-django

Need to clear caches in `django.db.connections` during `django_db_modify_db_settings`?

Open
#587 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

ConnectionHandler (django.db.connections is an instance of this) caches, so I ended up with with problems due to using connection from the old config† after changing settings from a django_db_modify_db_settings fixture. I fixed my issue by calling the following code before+after the fixture:

def clear_caches():
    """Invalidate the connection caches."""
    connections._connections = threading.local()
    # this will clear the cached property
    del connections.databases

If there isn't any disagreement I will submit a PR for this to change the default fixture and also update the documentation.

This looks like it should work back to at least 1.7.x, I haven't checked back further. Is there an officially supported range of Django versions?

† in my case I was only getting the database which was configured in DJANGO_SETTINGS_MODULE, I am not sure if that was because of app code causing a query too soon, or if it is expected every time in Django (in 1.11.11)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the django_db_modify_db_settings fixture and the referenced ConnectionHandler implementation in django/db/utils.py. Determine the supported Django version range and whether the default fixture should clear these caches, then update the documentation if that behavior is adopted.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
backend, databases, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.