Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #39 #40

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion django_pgviews/db/sql/query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.db import connections
from django.db.models.sql import query

from django_pgviews.db.sql import compiler


Expand Down
10 changes: 5 additions & 5 deletions django_pgviews/management/commands/clear_pgviews.py
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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))
Expand Down
6 changes: 2 additions & 4 deletions django_pgviews/management/commands/sync_pgviews.py
Original file line number Diff line number Diff line change
@@ -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')


Expand Down
12 changes: 6 additions & 6 deletions django_pgviews/models.py
Original file line number Diff line number Diff line change
@@ -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 = []
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion django_pgviews/signals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.dispatch import Signal


view_synced = Signal(
providing_args=['update', 'force', 'status', 'has_changed'])
all_views_synced = Signal()
15 changes: 8 additions & 7 deletions django_pgviews/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_]*))$')
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from os.path import isfile

from setuptools import setup, find_packages
from setuptools import find_packages, setup

try:
import pypandoc
Expand Down
1 change: 0 additions & 1 deletion tests/test_project/test_project/settings/ci.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from .base import *


DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
Expand Down
1 change: 0 additions & 1 deletion tests/test_project/test_project/viewtest/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.db import models

from django_pgviews import view


Expand Down
4 changes: 1 addition & 3 deletions tests/test_project/test_project/viewtest/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions tests/test_project/test_project/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down