Still problem with test database name
- Dominant language
- Python
- Stars
- 6.4k
- Forks
- 826
- PR merge metrics
- No merged PRs in 30d
Description
Hi,
the fix for https://github.com/django/channels/issues/2176 is not working for us.
Django: 5.2.6
Channels: 4.3.1
Daphne: 4.2.1
Python: 3.13.5
What we currently know:
`multiprocessing.set_start_method("spawn") --> settings.DATABASES["default"]["NAME"] = "foo"`
`multiprocessing.set_start_method("spawn") --> settings.DATABASES["default"]["TEST"]["NAME"] = None`
`multiprocessing.set_start_method("fork") --> settings.DATABASES["default"]["NAME"] = "test_foo"`
`multiprocessing.set_start_method("fork") --> settings.DATABASES["default"]["TEST"]["NAME"] = None`
for the following database settings:
```python
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"HOST": "localhost",
"NAME": "foo",
"USER": "foo",
"PASSWORD": "foo",
}
}
```
and
`multiprocessing.set_start_method("spawn") --> settings.DATABASES["default"]["NAME"] = "foo"`
`multiprocessing.set_start_method("spawn") --> settings.DATABASES["default"]["TEST"]["NAME"] = "test_foo"`
`multiprocessing.set_start_method("fork") --> settings.DATABASES["default"]["NAME"] = "test_foo"`
`multiprocessing.set_start_method("fork") --> settings.DATABASES["default"]["TEST"]["NAME"] = "test_foo"`
for the following database settings:
```python
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"HOST": "localhost",
"NAME": "foo",
"USER": "foo",
"PASSWORD": "foo",
"TEST": {
"NAME": "test_foo",
},
}
}
```
Out fix from https://github.com/django/channels/pull/2178 now results in a database name `test_test_foo` for if **no** test database name was given and for the `fork` method. The error is:
```python
django.db.utils.OperationalError: connection failed: connection to server at "127.0.0.1", port 5432 failed: FATAL: database "test_test_foo" does not exist
Multiple connection attempts failed. All failures were:
- host: 'localhost', port: None, hostaddr: '::1': connection failed: connection to server at "::1", port 5432 failed: FATAL: database "test_test_foo" does not exist
- host: 'localhost', port: None, hostaddr: '127.0.0.1': connection failed: connection to server at "127.0.0.1", port 5432 failed: FATAL: database "test_test_foo" does not exist
ERROR
```
We think that `multiprocessing.get_start_method()` should be used to differentiate between both cases. What do you think?
https://github.com/django/channels/blob/f726845eb856e69a7e094df6bb9d0f14e7f003ca/channels/testing/live.py#L22-L29
should be something like this:
```python
def set_database_connection():
from django.conf import settings
test_db_name = settings.DATABASES["default"]["TEST"]["NAME"]
if not test_db_name and multiprocessing.get_start_method() == "spawn":
test_db_name = TEST_DATABASE_PREFIX + settings.DATABASES["default"]["NAME"]
settings.DATABASES["default"]["NAME"] = test_db_name
```
Contributor guide
Research direction
Start with channels/testing/live.py at the referenced set_database_connection() code, then review issue #2176 and pull request #2178 for the existing behavior. Reproduce with PostgreSQL using both spawn and fork, with and without TEST.NAME. Done means the configured test database is selected correctly without producing test_test_foo or attempting a missing database.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, postgresql, python
- Domain
- backend, databases, testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100