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

Create views inside an atomic transaction #45

Merged
merged 2 commits into from
Mar 5, 2019
Merged
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
7 changes: 4 additions & 3 deletions django_pgviews/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import django
from django.core import exceptions
from django.db import connection
from django.db import connection, transaction
from django.db.models.query import QuerySet
from django.db import models
from django.utils import six
Expand Down Expand Up @@ -64,6 +64,7 @@ def realize_deferred_projections(sender, *args, **kwargs):
models.signals.class_prepared.connect(realize_deferred_projections)


@transaction.atomic()
def create_view(connection, view_name, view_query, update=True, force=False,
materialized=False, index=None):
"""
Expand Down Expand Up @@ -100,10 +101,10 @@ def create_view(connection, view_name, view_query, update=True, force=False,
# update this copy, and detecting errors.
cursor.execute('CREATE TEMPORARY VIEW check_conflict AS SELECT * FROM {0};'.format(view_name))
try:
cursor.execute('CREATE OR REPLACE TEMPORARY VIEW check_conflict AS {0};'.format(view_query))
with transaction.atomic():
cursor.execute('CREATE OR REPLACE TEMPORARY VIEW check_conflict AS {0};'.format(view_query))
except psycopg2.ProgrammingError:
force_required = True
cursor.connection.rollback()
finally:
cursor.execute('DROP VIEW IF EXISTS check_conflict;')

Expand Down