<commit_before>from django.db import connection, transaction, models
from django.db.models import signals
from django.db.utils import DatabaseError
from pgfields.viewtables import View, MatView

@transaction.autocommit
def create_views(sender, **kwargs):
    cursor = connection.cursor()
    app = kwargs['app']
    if type(app) == str:
        import sys
        app = getattr(sys.modules[app], 'models')
    import pdb; pdb.set_trace()
    for model in models.get_models(app):
        if issubclass(model, View):
            # Check if view exists
            sql = model.create_check()
            cursor.execute(*sql)
            result = cursor.fetchone()
            if not result[0]:
                # Create View
                sql = model.create_sql()
                cursor.execute(*sql)
            if issubclass(model, MatView):
                sql = MatView.storedproc_check()
                expected_resp = sql[2]
                sql = sql[:2]
                cursor.execute(*sql)
                res = cursor.fetchall()
                if res[0][0] != expected_resp:
                    for sql in MatView.storedproc_sql():
                        cursor.execute(sql, ())
                func = model.create_matview()
                try:
                    cursor.execute(*func)
                except DatabaseError as e:
                    if e.message.startswith('MatView') and e.message.find('already exists') != -1:
                        pass
                    else:
                        raise

signals.post_syncdb.connect(create_views)
try:
    from south.signals import post_migrate
    post_migrate.connect(create_views)
except ImportError:
    pass<commit_msg>bugfix<commit_after>from django.db import connection, transaction, models
from django.db.models import signals
from django.db.utils import DatabaseError
from pgfields.viewtables import View, MatView

@transaction.commit_manually()
def create_views(sender, **kwargs):
    cursor = connection.cursor()
    app = kwargs['app']
    if type(app) == str:
        import sys
        app = getattr(sys.modules[app], 'models')
    for model in models.get_models(app):
        if issubclass(model, View):
            # Check if view exists
            sql = model.create_check()
            cursor.execute(*sql)
            result = cursor.fetchone()
            if not result[0]:
                # Create View
                sql = model.create_sql()
                cursor.execute(*sql)
            if issubclass(model, MatView):
                sql = MatView.storedproc_check()
                expected_resp = sql[2]
                sql = sql[:2]
                cursor.execute(*sql)
                res = cursor.fetchall()
                if res[0][0] != expected_resp:
                    for sql in MatView.storedproc_sql():
                        cursor.execute(sql, ())
                func = model.create_matview()
                try:
                    cursor.execute(*func)
                    transaction.commit()
                except DatabaseError as e:
                    if e.message.startswith('MatView') and e.message.find('already exists') != -1:
                        transaction.rollback()
                        pass
                    else:
                        raise

signals.post_syncdb.connect(create_views)
try:
    from south.signals import post_migrate
    post_migrate.connect(create_views)
except ImportError:
    pass
{'fraction_non_alphanumeric': 0.0922809053975624, 'fraction_numerical': 0.003482298316889147, 'mean_word_length': 1.6454336147352264, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5808318', 'n_tokens_mistral': 916, 'n_tokens_neox': 881, 'n_words': 216}