Django and postgresql testing scheme

I am using PostgreSQL 9.3 and Django 1.7.4 with psycopg2 2.5.4

The database administrator asked us to create a schema for our application, and not publish it.

We defined the circuit and we had to add

'OPTIONS': {
    'options': '-c search_path=custom-schema-name'
},

to the settings.

During testing, Django creates a test database with the appropriate name, but we cannot set the name of the user schema.

I tried to find a way to customize the name of the user schema (I read the documents ), but I can not find a way to force the creation of the name of the schema during testing.

The error I get is

django.db.utils.ProgrammingError: no schema was selected to create in

When I see the created database, it has a public schema created by default.

public

'OPTIONS': {
    'options': '-c search_path=custom-schema-name,public'
},

.

- , ?

+4
1

( django 1.9.x):

MyApp//runner.py

from types import MethodType
from django.test.runner import DiscoverRunner
from django.db import connections

def prepare_database(self):
    self.connect()
    self.connection.cursor().execute("""
    CREATE SCHEMA foobar_schema AUTHORIZATION your_user;
    GRANT ALL ON SCHEMA foobar_schema TO your_user;
    """)


class PostgresSchemaTestRunner(DiscoverRunner):

    def setup_databases(self, **kwargs):
        for connection_name in connections:
            connection = connections[connection_name]
            connection.prepare_database = MethodType(prepare_database, connection)
        return super().setup_databases(**kwargs)

settings.py

TEST_RUNNER = 'myapp.test.runner.PostgresSchemaTestRunner'
+4

All Articles