Skip to content

Commit

Permalink
Merge pull request #45 from Tesorio/fix/create_view_inside_transaction
Browse files Browse the repository at this point in the history
Create views inside an atomic transaction
  • Loading branch information
tpict authored Mar 5, 2019
2 parents bdc7234 + 1071817 commit 336f692
Showing 1 changed file with 4 additions and 3 deletions.
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

0 comments on commit 336f692

Please sign in to comment.