Skip to content

4.11.0 test database is not created and used but main database is "cleared" #1234

@sshishov

Description

@sshishov

Hi we were using the same setup for years but recent upgrade to 4.11.1 broke everything. The problem that instead of creating test_<db> it is loading "session" fixtures inside main db and flushes it as we are doing call_command

Rolling back to 4.10.0 fixes the issue and test_<db> is created successfully.

This is part of our setup:

def run_sql(query: str, fetch: bool = False, db: str = 'default') -> tuple[t.Any, ...] | None:
    conn = psycopg.connect(
        user=ADMIN_USER, password=ADMIN_PASSWORD, host=django_settings.DATABASES[db]['HOST'], port=django_settings.DATABASES['default']['PORT']
    )
    (cur := conn.cursor()).execute(query)
    response: tuple[t.Any, ...] | None = cur.fetchone() if fetch else None
    conn.close()
    return response


@pytest.fixture(scope='session')
def django_db_createdb(request: pytest.FixtureRequest, django_db_createdb: bool) -> bool:
    db_name = f'test_{django_settings.DATABASES["default"]["NAME"]}'
    if xdist_suffix := getattr(request.config, 'workerinput', {}).get('workerid'):
        db_name = f'{db_name}_{xdist_suffix}'
    db_exists = (result := run_sql(query=f"SELECT EXISTS (SELECT 1 FROM pg_database WHERE datname='{db_name}')", fetch=True)) and result and result[0]
    if django_db_createdb or not db_exists:
        run_sql('CREATE EXTENSION IF NOT EXISTS vector')
    return django_db_createdb or not db_exists


@pytest.fixture(scope='session')
def django_db_setup(django_db_setup: None, django_db_blocker: pytest_django.DjangoDbBlocker, django_db_createdb: bool) -> None:
    del django_db_setup
    if django_db_createdb:
        with django_db_blocker.unblock():
            call_command('flush', '--noinput')
            call_command('loaddata', *pathlib.Path().glob('tests/db_fixtures/**/*.yaml'))

run_sql is just custom raw SQL runner to execute random queries. And then we have django_db_createdb and django_db_setup where we override something, decide something and also flushing test db and loading fixtures.

Somehow now everything is executed on our main <db> instead of test_<db>

I guess you can easily reproduce it using these 2 fixtures without non needed stuff

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions