Opened 4 years ago

Closed 4 years ago

#32242 closed Cleanup/optimization (wontfix)

@override_settings does not work with DATABASES

Reported by: Peter Inglesby Owned by: nobody
Component: Testing framework Version: 3.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

#19031 introduced a warning that @override_settings "may not work with DATABASES".

In the last seven years, things have changed so that @override_settings does not work with DATABASES at all.

This is because once django.setup() has been called, django.db.connections.databases is cached via cached_property, and so overriding the settings has no effect.

As such, the warning should be changed to an exception.

(I fully accept that anybody messing around with DATABASES like this deserves all the trouble they encounter!)

Change History (2)

comment:1 by Peter Inglesby, 4 years ago

If anybody stumbles across this via Google, this seems to work as a way of changing DATABASES:

    databases = connections.databases
    assert databases["default"]["ENGINE"] == "django.db.backends.sqlite3"
    databases["default"]["NAME"] = ":memory:"
    del connections.databases
    connections.__init__(databases=databases)
    call_command("migrate")

(In my case I wanted to switch a SQLite database to an in-memory SQLite database, but the principle should be the same.)

Tested with Django 3.1.4.

comment:2 by Mariusz Felisiak, 4 years ago

Component: UncategorizedTesting framework
Resolution: wontfix
Status: newclosed
Type: UncategorizedCleanup/optimization

IMO the current wording is clear enough "Overriding setting DATABASES can lead to unexpected behavior." and we shouldn't raise an exception. If someone really want to use override_settings with DATABASES and will mix it with deleting connections, mocking, et cetera.... I don't see a strong reason to prevent users from experimenting. Someone somewhere in the universe is probably using it with success 🖖.

Note: See TracTickets for help on using tickets.
Back to Top