[Fixed]-Django and postgresql testing schema

19๐Ÿ‘

โœ…

I ended up with writing a custom test runner to solve this problem (using django 1.9.x):

myapp/test/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'
๐Ÿ‘คdahrens

Leave a comment