Пример #1
0
def main():
    '''Read sys.argv for configuration, and perform the imports.'''
    if len(sys.argv) != 3:
        print "Usage: %s pootle.prefs users.prefs" % (
                sys.argv[0])
        return

    oldprefsfile = sys.argv[1]
    parsed_oldprefs = prefs.PrefsParser(oldprefsfile)
    usersfile = sys.argv[2]
    parsed_users = prefs.PrefsParser(usersfile)
    try:
        try:
            transaction.enter_transaction_management()
            transaction.managed(True)

            set_up_db_then_import_languages_then_users(parsed_oldprefs,
                                                       parsed_users)
        except:
            if transaction.is_dirty():
                transaction.rollback()
            if transaction.is_managed():
                transaction.leave_transaction_management()
            raise
    finally:
        if transaction.is_managed():
            if transaction.is_dirty():
                transaction.commit()
        if transaction.is_managed():
            transaction.leave_transaction_management()
Пример #2
0
    def afterTest(self, test):
        """
        Clean up any changes to the test database.
        """
        # Restore transaction support on tests
        from django.conf import settings
        from django.db import connections, transaction
        from django.test.utils import setup_test_environment, teardown_test_environment

        use_transaction_isolation = self._should_use_transaction_isolation(
            test, settings)

        if self._should_rebuild_schema(test):
            for connection in connections.all():
                connection.creation.destroy_test_db(
                    self.old_db, verbosity=self.verbosity)

            teardown_test_environment()

            setup_test_environment()
            for connection in connections.all():
                connection.creation.create_test_db(verbosity=self.verbosity)

            self.restore_transaction_support(transaction)
            transaction.commit()
            if transaction.is_managed():
                transaction.leave_transaction_management()
            # If connection is not closed Postgres can go wild with
            # character encodings.
            for connection in connections.all():
                connection.close()
            logger.debug("Running syncdb")
            self._num_syncdb_calls += 1
            self._loaded_test_fixtures = []
            return

        if use_transaction_isolation:
            self.restore_transaction_support(transaction)
            logger.debug("Rolling back")
            transaction.rollback()
            if transaction.is_managed():
                transaction.leave_transaction_management()
            # If connection is not closed Postgres can go wild with
            # character encodings.
            for connection in connections.all():
                connection.close()
        else:
            # Have to clear the db even if we're using django because django
            # doesn't properly flush the database after a test. It relies on
            # flushing before a test, so we want to avoid the case where a django
            # test doesn't flush and then a normal test runs, because it will
            # expect the db to already be flushed
            self._flush_db()
            self._loaded_test_fixtures = []


        self.call_plugins_method('afterRollback', settings)
Пример #3
0
def test_can_use_twice():
    """Can use same xact object as context manager twice."""
    cm = xact.xact()

    assert not transaction.is_managed()

    for i in range(2):
        with cm:
            assert transaction.is_managed()
        assert not transaction.is_managed()
Пример #4
0
 def test_django_db_transaction_managed(self):
     """
     Check that django.db.transaction.managed is not affected
     by monkey-patching
     """
     from django.db import transaction
     self.assertFalse(transaction.is_managed())
     transaction.enter_transaction_management()
     try:
         transaction.managed()
         self.assertTrue(transaction.is_managed())
     finally:
         transaction.leave_transaction_management()
Пример #5
0
 def test_django_db_transaction_managed(self):
     """
     Check that django.db.transaction.managed is not affected
     by monkey-patching
     """
     from django.db import transaction
     self.assertFalse(transaction.is_managed())
     transaction.enter_transaction_management()
     try:
         transaction.managed()
         self.assertTrue(transaction.is_managed())
     finally:
         transaction.leave_transaction_management()
Пример #6
0
    def process_response(self, request, response):
        """Commit or abort the transaction after processing the response.

        On successful completion of the request, the transaction will
        be committed.

        As an exception to this, if the L{STORM_COMMIT_SAFE_METHODS}
        setting is False, and the request used either of the GET and
        HEAD methods, the transaction will be aborted.
        """
        from django.db import transaction as django_transaction
        # If process_exception() has been called, then we'll no longer
        # be in managed transaction mode.
        if django_transaction.is_managed():
            if self.commit_safe_methods or (request.method
                                            not in ['HEAD', 'GET']):
                try:
                    transaction.commit()
                except Exception:
                    transaction.abort()
                    raise
            else:
                transaction.abort()
            django_transaction.set_clean()
            django_transaction.leave_transaction_management()
        return response
Пример #7
0
def is_managed(using=None):
    if django.VERSION[:2] < (1, 6):
        return transaction.is_managed(using=using)
    elif django.VERSION[:2] >= (1, 6):
        # See https://code.djangoproject.com/ticket/21004
        return not transaction.get_autocommit(using=using)
    return False
Пример #8
0
    def associate(self, handle, entry_id):
        """
        Associate an unassociated handle with an id. It is illegal to
        associate a handle with more than one entry. It is also
        illegal to have two handles point to the same entry.

        :param handle: The handle.
        :type handle: str
        :param entry_id: The entry id.
        :type entry_id: int
        """
        if not transaction.is_managed():
            raise Exception("transactions must be managed")

        if self.id(handle) is not None:
            raise ValueError("handle {0} already associated".format(handle))

        if self.entry_id_to_handle.get(entry_id, None) is not None:
            raise ValueError("id {0} already associated".format(entry_id))

        handle_obj = Handle.objects.select_for_update().get(
            id=self.handle_to_handle_obj_id[handle])
        handle_obj.entry_id = entry_id
        handle_obj.save()

        self.handle_to_entry_id[handle] = entry_id
        self.entry_id_to_handle[entry_id] = handle
Пример #9
0
    def id(self, handle):
        """
Return the id associated with a handle.

:param handle: The handle.
:type handle: str
:returns: The id.
:rtype: int or None
"""
        if not transaction.is_managed():
            raise Exception("transactions must be managed")

        # If it is there and not none, it can't have changed, so we
        # don't have to read the database.
        if handle in self.handle_to_entry_id and \
           self.handle_to_entry_id[handle] is not None:
            return self.handle_to_entry_id[handle]

        # Otherwise, check whether it has been associated.
        try:
            handle_obj = Handle.objects.get(session=self.session_key,
                                            handle=handle)
        except Handle.DoesNotExist:
            raise ValueError("handle {0} does not exist".format(handle))

        entry_id = handle_obj.entry.id if handle_obj.entry is not None else None
        self.handle_to_entry_id[handle] = entry_id
        self.handle_to_handle_obj_id[handle] = handle_obj.id
        if entry_id is not None:
            self.entry_id_to_handle[entry_id] = handle
        return entry_id
Пример #10
0
    def apply_async(cls, *args, **kwargs):
        # Delay the task unless the client requested otherwise or transactions
        # aren't being managed (i.e. the signal handlers won't send the task).

        # A rather roundabout way of allowing control of transaction behaviour from source. I'm sure there's a better way.
        after_transaction = True
        if len(args) > 1:
            if isinstance(args[1], dict):
                after_transaction = args[1].pop('after_transaction', True)
        if 'after_transaction' in kwargs:
            after_transaction = kwargs.pop('after_transaction')

        if transaction.is_managed() and after_transaction:
            if not transaction.is_dirty():
                # Always mark the transaction as dirty
                # because we push task in queue that must be fired or discarded
                if 'using' in kwargs:
                    transaction.set_dirty(using=kwargs['using'])
                else:
                    transaction.set_dirty()
            _get_task_queue().append((cls, args, kwargs))
        else:
            apply_async_orig = cls.original_apply_async
            if current_app.conf.CELERY_ALWAYS_EAGER:
                apply_async_orig = transaction.autocommit()(apply_async_orig)
            return apply_async_orig(*args, **kwargs)
Пример #11
0
def _single_commit_on_success(db_name):
    if transaction.is_managed(db_name):
        _logger.error(
            '_single_commit_on_success: raise DuplicateTransactionError')
        raise DuplicateTransactionError, db_name

    return transaction.commit_on_success(using=db_name)
Пример #12
0
 def __enter__(self):
     if transaction.is_managed(self.using):
         # We're already in a transaction; create a savepoint.
         self.sid = transaction.savepoint(self.using)
     else:
         transaction.enter_transaction_management(using=self.using)
         transaction.managed(True, using=self.using)
    def add_jobs(self, jobs, command):
        """Add a job, and any others which are required in order to reach its prerequisite state"""
        # Important: the Job must not be committed until all
        # its dependencies and locks are in.
        assert transaction.is_managed()

        for job in jobs:
            for dependency in self._dep_cache.get(job).all():
                if not dependency.satisfied():
                    log.info("add_jobs: setting required dependency %s %s" %
                             (dependency.stateful_object,
                              dependency.preferred_state))
                    self._set_state(dependency.get_stateful_object(),
                                    dependency.preferred_state, command)
            log.info("add_jobs: done checking dependencies")
            locks = self._create_locks(job)
            job.locks_json = json.dumps([l.to_dict() for l in locks])
            self._create_dependencies(job, locks)
            with transaction.commit_on_success():
                job.save()

            log.info("add_jobs: created Job %s (%s)" %
                     (job.pk, job.description()))

            for l in locks:
                self._lock_cache.add(l)

            command.jobs.add(job)

        self._job_collection.add_command(command, jobs)
Пример #14
0
def in_transaction(test_ignore=True):
    result = transaction.is_managed()
    if test_ignore:
        # Ignore when running inside a Django test case, which uses
        # transactions.
        result = result and not hasattr(mail, "outbox")
    return result
Пример #15
0
 def new_func(*args, **kwargs):
     if settings.BROKER_TRANSPORT == "django" or not transaction.is_managed():
         # если используется db транспорт или мы не под транзакцией, то нам это не нужно.
         # все работает по старому 
         return old_func(*args, **kwargs)
     else:
         store.queue.append(curry(old_func, *args, **kwargs)) #@UndefinedVariable
Пример #16
0
def _publish_article(slug, article):
    article.set_spam_status('clean')
    from django.db import transaction
    if transaction.is_managed():
        transaction.commit()
    article.ping_external_urls()
    caching.invalidate_by_article(slug, article.topic_id)
Пример #17
0
 def __enter__(self):
     if transaction.is_managed(self.using):
         # We're already in a transaction; create a savepoint.
         self.sid = transaction.savepoint(self.using)
     else:
         transaction.enter_transaction_management(using=self.using)
         transaction.managed(True, using=self.using)
Пример #18
0
def _acquire_entry_lock(entry, user):
    """
Acquire the lock. The caller must make sure that there is no lock yet
on the entry before calling this function.

:param entry: The entry for which to acquire the lock.
:type entry: :class:`.Entry`
:param user: The user who is acquiring the lock.
:type user: The value of :attr:`settings.AUTH_USER_MODEL` determines the class.
:returns: The lock if the lock was acquired, or ``None`` if not.
:rtype: :class:`.EntryLock`
"""
    if not transaction.is_managed():
        raise Exception("_acquire_entry_lock requires transactions to be "
                        "managed")

    lock = EntryLock()
    lock.entry = entry
    now = util.utcnow()
    lock.owner = user
    lock.datetime = now
    lock.save()

    _report(lock, "acquired")
    return lock
Пример #19
0
    def results_iter(self):
        """
        Returns an iterator over the results from executing this query.
        """
        resolve_columns = hasattr(self, 'resolve_columns')
        fields = None
        has_aggregate_select = bool(self.query.aggregate_select)
        # Set transaction dirty if we're using SELECT FOR UPDATE to ensure
        # a subsequent commit/rollback is executed, so any database locks
        # are released.
        if self.query.select_for_update and transaction.is_managed(self.using):
            transaction.set_dirty(self.using)
        for rows in self.execute_sql(MULTI):
            for row in rows:
                if resolve_columns:
                    if fields is None:
                        # We only set this up here because
                        # related_select_cols isn't populated until
                        # execute_sql() has been called.

                        # We also include types of fields of related models that
                        # will be included via select_related() for the benefit
                        # of MySQL/MySQLdb when boolean fields are involved
                        # (#15040).

                        # This code duplicates the logic for the order of fields
                        # found in get_columns(). It would be nice to clean this up.
                        if self.query.select:
                            fields = [f.field for f in self.query.select]
                        else:
                            fields = self.query.model._meta.fields
                        fields = fields + [
                            f.field for f in self.query.related_select_cols
                        ]

                        # If the field was deferred, exclude it from being passed
                        # into `resolve_columns` because it wasn't selected.
                        only_load = self.deferred_to_columns()
                        if only_load:
                            db_table = self.query.model._meta.db_table
                            fields = [
                                f for f in fields if db_table in only_load
                                and f.column in only_load[db_table]
                            ]
                    row = self.resolve_columns(row, fields)

                if has_aggregate_select:
                    aggregate_start = len(self.query.extra_select) + len(
                        self.query.select)
                    aggregate_end = aggregate_start + len(
                        self.query.aggregate_select)
                    row = tuple(row[:aggregate_start]) + tuple([
                        self.query.resolve_aggregate(value, aggregate,
                                                     self.connection)
                        for (alias, aggregate), value in zip(
                            self.query.aggregate_select.items(),
                            row[aggregate_start:aggregate_end])
                    ]) + tuple(row[aggregate_end:])

                yield row
Пример #20
0
    def apply_async(cls, *args, **kwargs):
        # Delay the task unless the client requested otherwise or transactions
        # aren't being managed (i.e. the signal handlers won't send the task).

        # A rather roundabout way of allowing control of transaction behaviour from source. I'm sure there's a better way.
        after_transaction = True
        if len(args) > 1:
            if isinstance(args[1], dict):
                after_transaction = args[1].pop('after_transaction', True)
        if 'after_transaction' in kwargs:
            after_transaction = kwargs.pop('after_transaction')

        if transaction.is_managed() and after_transaction:
            if not transaction.is_dirty():
                # Always mark the transaction as dirty
                # because we push task in queue that must be fired or discarded
                if 'using' in kwargs:
                    transaction.set_dirty(using=kwargs['using'])
                else:
                    transaction.set_dirty()
            _get_task_queue().append((cls, args, kwargs))
        else:
            apply_async_orig = cls.original_apply_async

            if current_app.conf.CELERY_ALWAYS_EAGER:
                apply_async_orig = transaction.autocommit()(apply_async_orig)
            return apply_async_orig(*args, **kwargs)
Пример #21
0
    def process_response(self, request, response):
        """Commit or abort the transaction after processing the response.

        On successful completion of the request, the transaction will
        be committed.

        As an exception to this, if the L{STORM_COMMIT_SAFE_METHODS}
        setting is False, and the request used either of the GET and
        HEAD methods, the transaction will be aborted.
        """
        from django.db import transaction as django_transaction
        # If process_exception() has been called, then we'll no longer
        # be in managed transaction mode.
        if django_transaction.is_managed():
            if self.commit_safe_methods or (
                request.method not in ['HEAD', 'GET']):
                try:
                    transaction.commit()
                except Exception:
                    transaction.abort()
                    raise
            else:
                transaction.abort()
            django_transaction.set_clean()
            django_transaction.leave_transaction_management()
        return response
Пример #22
0
 def process_response(self, request, response):
     """Commits and leaves transaction management."""
     if transaction.is_managed(using=self.get_tenant(request)):
         if transaction.is_dirty(using=self.get_tenant(request)):
             transaction.commit(using=self.get_tenant(request))
         transaction.leave_transaction_management(using=self.get_tenant(request))
     return response
Пример #23
0
 def test_savepoint_rollback(self):
     """Tests rollbacks of savepoints"""
     from django.db import transaction
     from testapp.models import Genre, Publisher
     from johnny import cache
     if not connection.features.uses_savepoints:
         return
     self.failUnless(transaction.is_managed() == False)
     self.failUnless(transaction.is_dirty() == False)
     connection.queries = []
     cache.local.clear()
     transaction.enter_transaction_management()
     transaction.managed()
     g = Genre.objects.get(pk=1)
     start_title = g.title
     g.title = "Adventures in Savepoint World"
     g.save()
     g = Genre.objects.get(pk=1)
     self.failUnless(g.title == "Adventures in Savepoint World")
     sid = transaction.savepoint()
     g.title = "In the Void"
     g.save()
     g = Genre.objects.get(pk=1)
     self.failUnless(g.title == "In the Void")
     transaction.savepoint_rollback(sid)
     g = Genre.objects.get(pk=1)
     self.failUnless(g.title == "Adventures in Savepoint World")
     transaction.rollback()
     g = Genre.objects.get(pk=1)
     self.failUnless(g.title == start_title)
     transaction.managed(False)
     transaction.leave_transaction_management()
Пример #24
0
def transaction_rollback(using):
    """
    DB(using)をロールバックし、トランザクション区間を終了する。
    """
    if ( transaction.is_managed(using=using) ):
        transaction.rollback(using=using)
        transaction.leave_transaction_management(using=using)
Пример #25
0
    def process_exception(self, request, exception):
        """Rolls back the database and leaves transaction management."""

        if transaction.is_managed():
            if transaction.is_dirty():
                transaction.rollback()
            transaction.leave_transaction_management()
Пример #26
0
def is_managed(using=None):
    if django.VERSION[:2] < (1, 6):
        return transaction.is_managed(using=using)
    elif django.VERSION[:2] >= (1, 6):
        # See https://code.djangoproject.com/ticket/21004
        return not transaction.get_autocommit(using=using)
    return False
Пример #27
0
def _publish_article(slug, article):
    article.set_spam_status('clean')
    from django.db import transaction
    if transaction.is_managed():
        transaction.commit()
    article.ping_external_urls()
    caching.invalidate_by_article(slug, article.topic_id)
Пример #28
0
def truncate_queryset(qs):
    """
    Deletes all records matched by queryset using

        DELETE from table WHERE <condition>

    query without fetching PK values for all items in original queryset
    unlike django's standard QuerySet.delete().

    See https://code.djangoproject.com/ticket/9519.
    """

    delete_query = qs.query.clone(DeleteQuery)

    # transaction management code is copied from QuerySet.update
    if not transaction.is_managed(using=qs.db):
        transaction.enter_transaction_management(using=qs.db)
        forced_managed = True
    else:
        forced_managed = False
    try:
        delete_query.get_compiler(qs.db).execute_sql(None)
        if forced_managed:
            transaction.commit(using=qs.db)
        else:
            transaction.commit_unless_managed(using=qs.db)
    finally:
        if forced_managed:
            transaction.leave_transaction_management(using=qs.db)
    def apply_async(self, *args, **kwargs):
        # Delay the task unless the client requested otherwise or transactions
        # aren't being managed (i.e. the signal handlers won't send the task).

        celery_eager = _get_celery_settings('CELERY_ALWAYS_EAGER')

        # New setting to run eager task post transaction
        # defaults to `not CELERY_ALWAYS_EAGER`
        eager_transaction = _get_celery_settings('CELERY_EAGER_TRANSACTION',
                                                 not celery_eager)

        if django.VERSION < (1, 6):

            if transaction.is_managed() and eager_transaction:
                if not transaction.is_dirty():
                    # Always mark the transaction as dirty
                    # because we push task in queue that must be fired or discarded
                    if 'using' in kwargs:
                        transaction.set_dirty(using=kwargs['using'])
                    else:
                        transaction.set_dirty()
                _get_task_queue().append((self, args, kwargs))
            else:
                apply_async_orig = super(PostTransactionTask, self).apply_async
                return apply_async_orig(*args, **kwargs)

        else:

            connection = get_connection()
            if connection.in_atomic_block and eager_transaction:
                _get_task_queue().append((self, args, kwargs))
            else:
                return self.original_apply_async(*args, **kwargs)
Пример #30
0
 def process_response(self, request, response):
     """Commits and leaves transaction management."""
     if transaction.is_managed():
         if transaction.is_dirty():
             transaction.commit()
         transaction.leave_transaction_management()
     return response
Пример #31
0
 def process_response(self, request, response):
     """Commits and leaves transaction management."""
     if transaction.is_managed():
         if transaction.is_dirty():
             transaction.commit()
         transaction.leave_transaction_management()
     return response
Пример #32
0
 def update(self, **kwargs):
     if transaction.is_managed(using=self.db):
         # トランザクション中.
         return super(PIQuerySet, self).update(**kwargs)
     else:
         # トランザクション外.
         return save_custom_retries(self.__update_sub, **kwargs)
Пример #33
0
    def test_transaction_rollback(self):
        """Tests johnny's handling of transaction rollbacks.

        Similar to the commit, this sets up a write to a db in a transaction,
        reads from it (to force a cache write of sometime), then rolls back."""
        from Queue import Queue as queue
        from django.db import transaction
        from testapp.models import Genre, Publisher
        from johnny import cache
        if settings.DATABASE_ENGINE == 'sqlite3':
            print "\n  Skipping test requiring multiple threads."
            return

        self.failUnless(transaction.is_managed() == False)
        self.failUnless(transaction.is_dirty() == False)
        connection.queries = []
        cache.local.clear()
        q = queue()
        other = lambda x: self._run_threaded(x, q)

        # load some data
        start = Genre.objects.get(id=1)
        other('Genre.objects.get(id=1)')
        hit, ostart = q.get()
        # these should be the same and should have hit cache
        self.failUnless(hit)
        self.failUnless(ostart == start)
        # enter manual transaction management
        transaction.enter_transaction_management()
        transaction.managed()
        start.title = 'Jackie Chan Novels'
        # local invalidation, this key should hit the localstore!
        nowlen = len(cache.local)
        start.save()
        self.failUnless(nowlen != len(cache.local))
        # perform a read OUTSIDE this transaction... it should still see the
        # old gen key, and should still find the "old" data
        other('Genre.objects.get(id=1)')
        hit, ostart = q.get()
        self.failUnless(hit)
        self.failUnless(ostart.title != start.title)
        # perform a READ inside the transaction;  this should hit the localstore
        # but not the outside!
        nowlen = len(cache.local)
        start2 = Genre.objects.get(id=1)
        self.failUnless(start2.title == start.title)
        self.failUnless(len(cache.local) > nowlen)
        transaction.rollback()
        # we rollback, and flush all johnny keys related to this transaction
        # subsequent gets should STILL hit the cache in the other thread
        # and indeed, in this thread.

        self.failUnless(transaction.is_dirty() == False)
        other('Genre.objects.get(id=1)')
        hit, ostart = q.get()
        self.failUnless(hit)
        start = Genre.objects.get(id=1)
        self.failUnless(ostart.title == start.title)
        transaction.managed(False)
        transaction.leave_transaction_management()
Пример #34
0
    def create_qark(attrs=None):
        """
        """

        # Wrap the creation of the facet and its attributes in a transaction savepoint
        # and ensure that a transaction is active
        assert transaction.is_managed(), "A managed transaction is required to create a Fact"
        sid = transaction.savepoint()

        try:
            # Create a QArk and save it; make sure it is new
            qark = QArk(nextrev=1)
            qark.save(force_insert=True)

            # Look up the id's of any attributes specified by name

            # Find all of the Facets by looking up the attributes specified

            # Create each of the attributes

            for attrdict in attrs:
                # Create a QArkAttr and save it; make sure it's new; and add it to the QArk
                attr = QArkAttr.create_qarkattr(**attrdict)
                attr.qark = qark
                attr.rev = 0
                attr.save(force_insert=True)

            return qark

        except:
            transaction.savepoint_rollback(sid)
            # TODO: Need an exception here that is meaningful to what went wrong
            raise
        else:
            transaction.savepoint_commit(sid)
Пример #35
0
 def _commit_on_success_unless_managed(*args, **kw):
     try:
         if transaction.is_managed():
             forced_managed = False
         else:
             transaction.enter_transaction_management()
             forced_managed = True
         
         try:
             res = func(*args, **kw)
         except:
             # All exceptions must be handled here (even string ones).
             if transaction.is_dirty():
                 if forced_managed:
                     transaction.rollback()
                 else:
                     transaction.rollback_unless_managed()
             raise
         else:
             if transaction.is_dirty():
                 if forced_managed:
                     transaction.commit()
                 else:
                     transaction.commit_unless_managed()
         return res
     finally:
         if forced_managed:
             transaction.leave_transaction_management()
Пример #36
0
def in_transaction(test_ignore=True):
    result = transaction.is_managed()
    if test_ignore:
        # Ignore when running inside a Django test case, which uses
        # transactions.
        result = result and not hasattr(mail, 'outbox')
    return result
Пример #37
0
 def tearDown(self):
     from django.db import transaction
     if transaction.is_managed():
         if transaction.is_dirty():
             transaction.rollback()
         transaction.managed(False)
         transaction.leave_transaction_management()
Пример #38
0
def transaction_rollback(using):
    """
    DB(using)をロールバックし、トランザクション区間を終了する。
    """
    if (transaction.is_managed(using=using)):
        transaction.rollback(using=using)
        transaction.leave_transaction_management(using=using)
Пример #39
0
 def createTable(self):
     """
     Sets up the database table using self.field_list
     """
     
     if not self.field_list:
         self._getModelFieldList()
     
     if not transaction.is_managed():
         db.start_transaction()
         db.create_table(self._tname, tuple(self.field_list))
         
         # Executing deferred SQL, after correcting the CREATE INDEX statements
         deferred_sql = []
         for stmt in db.deferred_sql:
             deferred_sql.append(re.sub('^CREATE INDEX \"customforms\".', 'CREATE INDEX ', stmt))
         db.deferred_sql = deferred_sql    
         db.execute_deferred_sql()    
         db.commit_transaction()
     else:
         db.create_table(self._tname, tuple(self.field_list))
         
         # Executing deferred SQL, after correcting the CREATE INDEX statements
         deferred_sql = []
         for stmt in db.deferred_sql:
             deferred_sql.append(re.sub('^CREATE INDEX \"customforms\".', 'CREATE INDEX ', stmt))
         db.deferred_sql = deferred_sql    
         db.execute_deferred_sql()    
Пример #40
0
 def update(self, **kwargs):
     """
     Updates all elements in the current QuerySet, setting all the given
     fields to the appropriate values.
     """
     assert self.query.can_filter(), \
             "Cannot update a query once a slice has been taken."
     query = self.query.clone(sql.UpdateQuery)
     query.add_update_values(kwargs)
     if not transaction.is_managed():
         transaction.enter_transaction_management()
         forced_managed = True
     else:
         forced_managed = False
     try:
         rows = query.execute_sql(None)
         if forced_managed:
             transaction.commit()
         else:
             transaction.commit_unless_managed()
     finally:
         if forced_managed:
             transaction.leave_transaction_management()
     self._result_cache = None
     return rows
Пример #41
0
    def apply_async(self, *args, **kwargs):
        # Delay the task unless the client requested otherwise or transactions
        # aren't being managed (i.e. the signal handlers won't send the task).

        celery_eager = _get_celery_settings('CELERY_ALWAYS_EAGER')

        # New setting to run eager task post transaction
        # defaults to `not CELERY_ALWAYS_EAGER`
        eager_transaction = _get_celery_settings('CELERY_EAGER_TRANSACTION',
                                                 not celery_eager)

        if django.VERSION < (1, 6):

            if transaction.is_managed() and eager_transaction:
                if not transaction.is_dirty():
                    # Always mark the transaction as dirty
                    # because we push task in queue that must be fired or discarded
                    if 'using' in kwargs:
                        transaction.set_dirty(using=kwargs['using'])
                    else:
                        transaction.set_dirty()
                _get_task_queue().append((self, args, kwargs))
            else:
                apply_async_orig = super(PostTransactionTask, self).apply_async
                return apply_async_orig(*args, **kwargs)

        else:

            connection = get_connection()
            if connection.in_atomic_block and eager_transaction:
                _get_task_queue().append((self, args, kwargs))
            else:
                return self.original_apply_async(*args, **kwargs)
    def apply_async(self, *args, **kwargs):
        # Delay the task unless the client requested otherwise or transactions
        # aren't being managed (i.e. the signal handlers won't send the task).

        if django.VERSION < (1, 6):

            if transaction.is_managed() and not current_app.conf.CELERY_ALWAYS_EAGER:
                if not transaction.is_dirty():
                    # Always mark the transaction as dirty
                    # because we push task in queue that must be fired or discarded
                    if 'using' in kwargs:
                        transaction.set_dirty(using=kwargs['using'])
                    else:
                        transaction.set_dirty()
                _get_task_queue().append((self, args, kwargs))
            else:
                apply_async_orig = super(PostTransactionTask, self).apply_async
                return apply_async_orig(*args, **kwargs)

        else:

            connection = get_connection()
            if connection.in_atomic_block and not getattr(current_app.conf, 'CELERY_ALWAYS_EAGER', False):
                _get_task_queue().append((self, args, kwargs))
            else:
                return self.original_apply_async(*args, **kwargs)
    def test_django_db_transaction_managed(self):
        """
        Check that django.db.transaction.managed is not affected
        by monkey-patching
        """

        if django.VERSION >= (1,6):
            self.skipTest('Django 1.6 does not need this test')

        from django.db import transaction
        self.assertFalse(transaction.is_managed())
        transaction.enter_transaction_management()
        try:
            transaction.managed()
            self.assertTrue(transaction.is_managed())
        finally:
            transaction.leave_transaction_management()
Пример #44
0
 def process_response(self, request, response):
     if transaction.is_managed():
         try:
             transaction.commit()
         except:
             pass
         transaction.leave_transaction_management()
     return response
 def wrapper(*args, **kwargs):
     if transaction.is_managed():
         @transaction.commit_on_success
         def f_deferred(*a, **kw):
             f(*args, **kwargs)
         transaction.signals.post_commit.connect(f_deferred, weak=False)
     else:
         f(*args, **kwargs)
Пример #46
0
 def mark_deleted(self):
     # If this is not within a managed transaction we must use commit_on_success to ensure that the object is
     # only marked deleted if the updates to alerts also succeed
     if transaction.is_managed():
         self._mark_deleted()
     else:
         with transaction.commit_on_success():
             self._mark_deleted()
Пример #47
0
def transaction_commit(using):
    """
    DB(using)がdirtyな場合、commitし、トランザクション区間を終了する。
    """
    if (transaction.is_managed(using=using)):
        if (transaction.is_dirty(using=using)):
            transaction.commit(using=using)
        transaction.leave_transaction_management(using=using)
Пример #48
0
 def process_response(self, request, response):
     if transaction.is_managed():
         try:
             transaction.commit()
         except:
             pass
         transaction.leave_transaction_management()
     return response
Пример #49
0
def daemon_refresh():
    """Ask daemon to reload configuration"""

    if transaction.is_managed():
        transaction.commit()

    queue = redis.Redis(db=0)
    queue.lpush('bot', 'REFRESH')
Пример #50
0
def commit_unless_managed(using=None):
    if not transaction.is_managed(using=using):
        connection = conn(using)
        transaction_signals.pre_commit.send(sender=connection)
        yield
        send_robust_and_log_errors("post_commit", sender=connection)
    else:
        yield
Пример #51
0
def rollback_unless_managed(using=None):
    if not transaction.is_managed(using=using):
        connection = conn(using)
        signals.pre_rollback.send(sender=connection)
        yield
        send_robust_and_log_errors("post_rollback", sender=connection)
    else:
        yield
Пример #52
0
    def test_django_db_transaction_managed(self):
        """
        Check that django.db.transaction.managed is not affected
        by monkey-patching
        """

        if django.VERSION >= (1, 6):
            self.skipTest('Django 1.6 does not need this test')

        from django.db import transaction
        self.assertFalse(transaction.is_managed())
        transaction.enter_transaction_management()
        try:
            transaction.managed()
            self.assertTrue(transaction.is_managed())
        finally:
            transaction.leave_transaction_management()
Пример #53
0
def transaction_commit(using):
    """
    DB(using)がdirtyな場合、commitし、トランザクション区間を終了する。
    """
    if ( transaction.is_managed(using=using) ):
        if ( transaction.is_dirty(using=using) ):
            transaction.commit(using=using)
        transaction.leave_transaction_management(using=using)
Пример #54
0
def create_tables_for_dynamic_classes(*model_classes):
    """
        Creates the table for the dynamic model class if needed
    :param model_classes: 0 or more model classes for which to create a table
    :return:
    """
    for model_class in model_classes:
        if dynamic_model_table_exists(model_class):
            continue

        #info = "Model class table {model_class} doesn't exist -- creating it \n"
        #logger.debug(info.format(model_class=model_class._meta.db_table))

        fields = [(f.name, f) for f in model_class._meta.local_fields]
        table_name = model_class._meta.db_table
        db.create_table(table_name, fields)

        # some fields (eg GeoDjango) require additional SQL to be executed
        # Because of the poor Django/GeoDjango support for schemas, we have to manipulate the GeoDjango sql here so that the table is resolved to the correct schema, sigh
        if len(table_name.split('.')) == 2:
            schema, table = parse_schema_and_table(table_name)
            for i, sql in enumerate(db.deferred_sql):
                # Replace the POSTGIS single argument with two arguments
                # TODO this stupidly assumes that all deferred sql is POSTGIS
                # Substitution for '"schema"."table"' to 'schema','table'. This is for AddGeometryColumn
                db.deferred_sql[i] = re.sub(
                    "'{0}'".format(table_name),
                    "'{0}','{1}'".format(schema, table), sql)
                # Substitution for "schema"."table" to schema.table. This is for CREATE INDEX
                db.deferred_sql[i] = re.sub("{0}".format(table_name),
                                            "{0}.{1}".format(schema, table),
                                            db.deferred_sql[i])
                # Substitution for "schema"."tableHEX". Some indexes add random hex to the table name inside the double quotes. They may also truncate the table name, so just capture everything between "s
                # Also truncate to 64 characters the schema name minus the length of the table name, favoring the end of the schema which is most unique
                db.deferred_sql[i] = re.sub(r'"(".*)"\."(.*") ON',
                                            r'\1.\2 ON'.format(schema, table),
                                            db.deferred_sql[i])
                # Last ditch effort to remove extra " when we can't match generated index
                db.deferred_sql[i] = re.sub(r'""', r'"', db.deferred_sql[i])
                if string.find(db.deferred_sql[i], 'CREATE INDEX') == 0:
                    subs = db.deferred_sql[i]
                    # Truncate the index name. This could be done more elegantly
                    # db.deferred_sql[i] = subs[0:14] + subs[14:string.index(subs, '" ON')][-63:] + subs[string.index(subs, '" ON'):]
                    db.deferred_sql[i] = subs[0:14] + table + '_' + re.findall(
                        r'"([^"]*)"',
                        subs)[1] + subs[string.index(subs, '" ON'):]

        try:
            db.execute_deferred_sql()
        except Exception, e:
            raise Exception(
                "The table {table_name} was not created. Original exception: {message}. Deferred sql calls: {sql}"
                .format(table_name=model_class._meta.db_table,
                        message=e.message,
                        sql='\n'.join(db.deferred_sql)))
        # TODO I don't know if or when this is needed.
        if transaction.is_managed():
            transaction.commit()
Пример #55
0
 def process_response(self, request, response):
     """Commits and leaves transaction management."""
     db = self.get_tenant(request)
     if db:
         if transaction.is_managed(using=db):
             if transaction.is_dirty(using=db):
                 transaction.commit(using=db)
             transaction.leave_transaction_management(using=db)
     return response