diff --git a/django_pgviews/db/sql/query.py b/django_pgviews/db/sql/query.py index eb515ba..bd21e5a 100644 --- a/django_pgviews/db/sql/query.py +++ b/django_pgviews/db/sql/query.py @@ -1,6 +1,5 @@ from django.db import connections from django.db.models.sql import query - from django_pgviews.db.sql import compiler diff --git a/django_pgviews/management/commands/clear_pgviews.py b/django_pgviews/management/commands/clear_pgviews.py index 52ce109..4f32bb9 100644 --- a/django_pgviews/management/commands/clear_pgviews.py +++ b/django_pgviews/management/commands/clear_pgviews.py @@ -1,11 +1,9 @@ import logging -from django.core.management.base import BaseCommand from django.apps import apps -from django.db import connection - -from django_pgviews.view import clear_view, View, MaterializedView - +from django.core.management.base import BaseCommand +from django.db import connections, router +from django_pgviews.view import MaterializedView, View, clear_view log = logging.getLogger('django_pgviews.sync_pgviews') @@ -22,6 +20,8 @@ def handle(self, **options): hasattr(view_cls, 'sql')): continue python_name = '{}.{}'.format(view_cls._meta.app_label, view_cls.__name__) + using = router.db_for_write(view_cls) + connection = connections[using] status = clear_view( connection, view_cls._meta.db_table, materialized=isinstance(view_cls(), MaterializedView)) diff --git a/django_pgviews/management/commands/sync_pgviews.py b/django_pgviews/management/commands/sync_pgviews.py index fd785b6..8d6fa42 100644 --- a/django_pgviews/management/commands/sync_pgviews.py +++ b/django_pgviews/management/commands/sync_pgviews.py @@ -1,13 +1,11 @@ -from optparse import make_option import logging +from optparse import make_option +from django.apps import apps from django.core.management.base import BaseCommand from django.db import connection -from django.apps import apps - from django_pgviews.models import ViewSyncer - log = logging.getLogger('django_pgviews.sync_pgviews') diff --git a/django_pgviews/models.py b/django_pgviews/models.py index 89728cb..24e3f3c 100644 --- a/django_pgviews/models.py +++ b/django_pgviews/models.py @@ -1,14 +1,12 @@ import logging from django.apps import apps -from django.db import connection - -from django_pgviews.view import create_view, View, MaterializedView -from django_pgviews.signals import view_synced, all_views_synced +from django.db import connections, router +from django_pgviews.signals import all_views_synced, view_synced +from django_pgviews.view import MaterializedView, View, create_view log = logging.getLogger('django_pgviews.sync_pgviews') - class ViewSyncer(object): def run(self, force, update, **options): self.synced = [] @@ -50,7 +48,9 @@ def run_backlog(self, models, force, update): continue # Skip try: - status = create_view(connection, view_cls._meta.db_table, + using = router.db_for_write(view_cls) + _connection = connections[using] + status = create_view(_connection, view_cls._meta.db_table, view_cls.sql, update=update, force=force, materialized=isinstance(view_cls(), MaterializedView), index=view_cls._concurrent_index) diff --git a/django_pgviews/signals.py b/django_pgviews/signals.py index 8a98da4..802fba5 100644 --- a/django_pgviews/signals.py +++ b/django_pgviews/signals.py @@ -1,6 +1,5 @@ from django.dispatch import Signal - view_synced = Signal( providing_args=['update', 'force', 'status', 'has_changed']) all_views_synced = Signal() diff --git a/django_pgviews/view.py b/django_pgviews/view.py index 63a6f4c..9f98d69 100644 --- a/django_pgviews/view.py +++ b/django_pgviews/view.py @@ -6,17 +6,15 @@ import re import django +import psycopg2 +from django.apps import apps from django.core import exceptions -from django.db import connection +from django.db import connections, models, router from django.db.models.query import QuerySet -from django.db import models from django.utils import six -from django.apps import apps -import psycopg2 - from django_pgviews.db import get_fields_by_name - - +import django.db.models.options as options +options.DEFAULT_NAMES = options.DEFAULT_NAMES + ('using',) FIELD_SPEC_REGEX = (r'^([A-Za-z_][A-Za-z0-9_]*)\.' r'([A-Za-z_][A-Za-z0-9_]*)\.' r'(\*|(?:[A-Za-z_][A-Za-z0-9_]*))$') @@ -266,6 +264,9 @@ class MaterializedView(View): """ @classmethod def refresh(self, concurrently=False): + using = router.db_for_write(self) + connection = connections[using] + cursor_wrapper = connection.cursor() cursor = cursor_wrapper.cursor try: diff --git a/doc/conf.py b/doc/conf.py index d62fe61..1e2030a 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -11,7 +11,8 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys, os +import os +import sys # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the diff --git a/setup.py b/setup.py index 7767926..a64e744 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from os.path import isfile -from setuptools import setup, find_packages +from setuptools import find_packages, setup try: import pypandoc diff --git a/tests/test_project/test_project/settings/ci.py b/tests/test_project/test_project/settings/ci.py index 27dbe1d..004325a 100644 --- a/tests/test_project/test_project/settings/ci.py +++ b/tests/test_project/test_project/settings/ci.py @@ -1,6 +1,5 @@ from .base import * - DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', diff --git a/tests/test_project/test_project/viewtest/models.py b/tests/test_project/test_project/viewtest/models.py index 991a9ed..b56d3f7 100644 --- a/tests/test_project/test_project/viewtest/models.py +++ b/tests/test_project/test_project/viewtest/models.py @@ -1,5 +1,4 @@ from django.db import models - from django_pgviews import view diff --git a/tests/test_project/test_project/viewtest/tests.py b/tests/test_project/test_project/viewtest/tests.py index af68a3d..9586b3f 100644 --- a/tests/test_project/test_project/viewtest/tests.py +++ b/tests/test_project/test_project/viewtest/tests.py @@ -8,9 +8,7 @@ from django.db.models import signals from django.dispatch import receiver from django.test import TestCase -from django_pgviews.signals import view_synced, all_views_synced - -from . import models +from django_pgviews.signals import all_views_synced, view_synced @receiver(signals.post_migrate) diff --git a/tests/test_project/test_project/wsgi.py b/tests/test_project/test_project/wsgi.py index 43ff066..6127c6f 100644 --- a/tests/test_project/test_project/wsgi.py +++ b/tests/test_project/test_project/wsgi.py @@ -15,16 +15,17 @@ """ import os +# This application object is used by any WSGI server configured to use this +# file. This includes Django's development server, if the WSGI_APPLICATION +# setting points here. +from django.core.wsgi import get_wsgi_application + # We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks # if running multiple sites in the same mod_wsgi process. To fix this, use # mod_wsgi daemon mode with each site in its own daemon process, or use # os.environ["DJANGO_SETTINGS_MODULE"] = "test_project.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_project.settings") -# This application object is used by any WSGI server configured to use this -# file. This includes Django's development server, if the WSGI_APPLICATION -# setting points here. -from django.core.wsgi import get_wsgi_application application = get_wsgi_application() # Apply WSGI middleware here.