<commit_before>import flask
import uuid
import sqlalchemy

# TODO this import here is so strange
from infosystem import database
from infosystem.common import exception


class Operation(object):

    def __init__(self, manager):
        self.manager = manager
        self.driver = manager.driver if hasattr(manager, 'driver') else None

    def pre(self, **kwargs):
        return True

    def do(self, **kwargs):
        return True

    def post(self):
        pass

    def __call__(self, **kwargs):
        session = database.db.session

        if not self.pre(session=session, **kwargs):
            raise exception.PreconditionFailed()

        if not getattr(session, 'count', None):
           setattr(session, 'count', 1)
        else:
           session.count += 1

        try:
            result = self.do(session, **kwargs)
            session.count -= 1

            self.post()
            if session.count == 0:
                session.commit()
        except sqlalchemy.exc.IntegrityError:
            # TODO(samueldmq): integrity error may be something else...
            raise exception.DuplicatedEntity()
        except Exception as e:
            session.rollback()
            session.count = 0
            raise e
        return result


class Create(Operation):

    def pre(self, session, **kwargs):
        self.entity = self.driver.instantiate(id=uuid.uuid4().hex, **kwargs)
        return self.entity.is_stable()

    def do(self, session, **kwargs):
        self.driver.create(self.entity, session=session)
        return self.entity
<commit_msg>Add flush command to prevent constraint violantions<commit_after>import flask
import uuid
import sqlalchemy

# TODO this import here is so strange
from infosystem import database
from infosystem.common import exception


class Operation(object):

    def __init__(self, manager):
        self.manager = manager
        self.driver = manager.driver if hasattr(manager, 'driver') else None

    def pre(self, **kwargs):
        return True

    def do(self, **kwargs):
        return True

    def post(self):
        pass

    def __call__(self, **kwargs):
        session = database.db.session

        if not self.pre(session=session, **kwargs):
            raise exception.PreconditionFailed()

        if not getattr(session, 'count', None):
           setattr(session, 'count', 1)
        else:
           session.count += 1

        try:
            result = self.do(session, **kwargs)
            # NOTE(fdoliveira): This flush send all pending commands to DB but not commit then
            # This was used to prevent violations of foreign key on post inserts, updates ou deletes
            session.flush()
            session.count -= 1

            self.post()
            if session.count == 0:
                session.commit()
        except sqlalchemy.exc.IntegrityError as err:
            # TODO(samueldmq): integrity error may be something else...
            # print(err.orig.diag.constraint_name)
            raise exception.DuplicatedEntity()
        except Exception as e:
            session.rollback()
            session.count = 0
            raise e
        return result


class Create(Operation):

    def pre(self, session, **kwargs):
        self.entity = self.driver.instantiate(id=uuid.uuid4().hex, **kwargs)
        return self.entity.is_stable()

    def do(self, session, **kwargs):
        self.driver.create(self.entity, session=session)
        return self.entity

{'fraction_non_alphanumeric': 0.09454022988505748, 'fraction_numerical': 0.0034482758620689655, 'mean_word_length': 2.1530797101449277, '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': '659788', 'n_tokens_mistral': 942, 'n_tokens_neox': 895, 'n_words': 282}