pytest-dev / pytest-dev/pytest-django

Where can we add SQL for schema creation in Postgres?

Open
#872 13 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I've got some database tables that exist in a separate Postgres schema called legacy. When using pytest-django to set up a test database, I get this error when it tries to apply the migrations:

django.db.utils.ProgrammingError: schema "legacy" does not exist

That makes good sense as it's just created a fresh test database and that schema, in fact, does not exist.

However, I'm really struggling to see where I can hook into pytest-django's ecosystem of fixtures in order to create the schema I need before the migrations are applied.

I've read some some other related issues, but haven't come across a definitive answer.

I tried adding an autouse fixture in conftest.py, but I think it would run too late in the process, and so my test run ends with the error above.

import pytest
from django.db import connection

@pytest.fixture(scope='session')
def legacy_schema(django_db_setup, django_db_blocker):
    # https://docs.djangoproject.com/en/3.1/topics/db/sql/#executing-custom-sql-directly
    with django_db_blocker.unblock():
        with connection.cursor() as cursor:
            cursor.execute('CREATE SCHEMA legacy;')

Can anyone point me in the right direction? Or if this is genuinely impossible with pytest-django, let me know so I can pick a different test runner.

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 reading the django_db_setup and django_db_blocker fixtures discussed in the issue, then review the three linked issues for existing guidance on database setup timing. Determine whether pytest-django needs a documented hook or a code change to create the legacy schema before migrations; done means a clear supported path or a confirmed limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, postgresql, python
Domain
databases, testing-qa
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.