4

I am trying to install the hstore extension before running django tests. For that, I have overridden the default DiscoverRunner's setup_databases method.

However, the extension is not installed & the tests show this error django.db.utils.ProgrammingError: type "hstore" does not exist

Here's my code to override the default discover runner.

settings.py

TEST_RUNNER = 'project.tests.CustomDiscovererRunner'

tests.py

from django.db import DEFAULT_DB_ALIAS, connections
from django.test.runner import DiscoverRunner

class CustomDiscovererRunner(DiscoverRunner):
    def setup_databases(self, **kwargs):

        result = super().setup_databases(**kwargs)

        connection = connections[DEFAULT_DB_ALIAS]
        cursor = connection.cursor()
        cursor.execute('CREATE EXTENSION IF NOT EXISTS HSTORE')

        return result
Pranjal
  • 500
  • 6
  • 12

1 Answers1

-1

Super late but... For the new people asking the same question:

Django has its own function to create extensions using the migrations (which are executed before the tests), and the example is the HStore

https://docs.djangoproject.com/en/dev/ref/contrib/postgres/operations/#creating-extension-using-migrations

However...

For most extensions, this requires a database user with superuser privileges. If the Django database user doesn’t have the appropriate privileges, you’ll have to create the extension outside of Django migrations with a user that has them. In that case, connect to your Django database and run the query CREATE EXTENSION IF NOT EXISTS hstore;.