Ticket #14051: transaction_signals.patch

File transaction_signals.patch, 5.0 KB (added by Andreas Pelme, 14 years ago)

Initial patch (with tests+docs)

  • new file django/db/signals.py

    diff --git a/django/db/signals.py b/django/db/signals.py
    new file mode 100644
    index 0000000..0cf666a
    - +  
     1from django.dispatch import Signal
     2
     3pre_commit = Signal()
     4post_commit = Signal()
     5
     6pre_rollback = Signal()
     7post_rollback = Signal()
  • django/db/transaction.py

    diff --git a/django/db/transaction.py b/django/db/transaction.py
    index 3c767f1..085e54b 100644
    a b try:  
    2121except ImportError:
    2222    from django.utils.functional import wraps  # Python 2.4 fallback.
    2323from django.db import connections, DEFAULT_DB_ALIAS
     24from django.db.signals import pre_commit, post_commit, pre_rollback, post_rollback
    2425from django.conf import settings
    2526
    2627class TransactionManagementError(Exception):
    def commit(using=None):  
    195196    """
    196197    if using is None:
    197198        using = DEFAULT_DB_ALIAS
     199
     200    pre_commit.send(sender=using)
    198201    connection = connections[using]
    199202    connection._commit()
    200203    set_clean(using=using)
     204    post_commit.send(sender=using)
    201205
    202206def rollback(using=None):
    203207    """
    def rollback(using=None):  
    205209    """
    206210    if using is None:
    207211        using = DEFAULT_DB_ALIAS
     212
     213    pre_rollback.send(sender=using)
     214
    208215    connection = connections[using]
    209216    connection._rollback()
    210217    set_clean(using=using)
     218    post_rollback.send(sender=using)
     219
    211220
    212221def savepoint(using=None):
    213222    """
  • docs/ref/signals.txt

    diff --git a/docs/ref/signals.txt b/docs/ref/signals.txt
    index 69ed48b..f48f288 100644
    a b Arguments that are sent with this signal:  
    327327``sender``
    328328    The model class which was just prepared.
    329329
     330
     331Transaction signals
     332===================
     333
     334.. module:: django.db.signals
     335   :synopsis: Signals sent when database transactions are committed/rolled back.
     336
     337The :mod:`django.db.signals` module defines a set of signals sent by the
     338transaction system. These signals are sent even if you are not using a transaction
     339capable database. See the :doc:`database transaction documentation </topics/db/transactions>`
     340for more information about database transactions.
     341
     342
     343pre_commit
     344--------
     345
     346.. attribute:: django.db.signals.pre_commit
     347   :module:
     348
     349.. ^^^^^^^ this :module: hack keeps Sphinx from prepending the module.
     350
     351Before a transaction is committed to the database, this signal is sent.
     352
     353Arguments sent with this signal:
     354
     355    ``sender``
     356        The database connection alias where the transaction is to be committed.
     357
     358post_commit
     359---------
     360
     361.. data:: django.db.signals.post_commit
     362   :module:
     363
     364Like pre_commit, but this one is sent after the transaction has been committed.
     365
     366Arguments sent with this signal:
     367
     368    ``sender``
     369        As above: The database connection alias.
     370
     371
     372pre_rollback
     373--------
     374
     375.. attribute:: django.db.signals.pre_rollback
     376   :module:
     377
     378.. ^^^^^^^ this :module: hack keeps Sphinx from prepending the module.
     379
     380Before a transaction is being rollbacked the database, this signal is sent.
     381
     382Arguments sent with this signal:
     383
     384    ``sender``
     385        The database connection alias where the transaction is to be rollbacked.
     386
     387
     388post_rollback
     389---------
     390
     391.. data:: django.db.signals.post_rollback
     392   :module:
     393
     394Like pre_rollback, but this one is sent after the transaction has been rollbacked.
     395
     396Arguments sent with this signal:
     397
     398    ``sender``
     399        As above: The database connection alias.
     400
     401
    330402Management signals
    331403==================
    332404
  • tests/modeltests/transactions/models.py

    diff --git a/tests/modeltests/transactions/models.py b/tests/modeltests/transactions/models.py
    index df0dd80..5578596 100644
    a b class Reporter(models.Model):  
    2222
    2323__test__ = {'API_TESTS':"""
    2424>>> from django.db import connection, transaction
     25>>> from django.db.signals import pre_commit, post_commit, pre_rollback, post_rollback
    2526"""}
    2627
    2728from django.conf import settings
    Traceback (most recent call last):  
    129130    ...
    130131TransactionManagementError: Transaction managed block ended with pending COMMIT/ROLLBACK
    131132
     133# Make sure transaction signals is sent
     134
     135>>> def transaction_signal_test(signal, sender, **kwargs):
     136...    signals = {
     137...        pre_commit: 'pre_commit',
     138...        post_commit: 'post_commit',
     139...        pre_rollback: 'pre_rollback',
     140...        post_rollback: 'post_rollback'
     141...    }
     142...
     143...    print "signal: ", signals.get(signal)
     144...    print 'sender: ', sender
     145
     146>>> pre_commit.connect(transaction_signal_test)
     147>>> post_commit.connect(transaction_signal_test)
     148>>> pre_rollback.connect(transaction_signal_test)
     149>>> post_rollback.connect(transaction_signal_test)
     150
     151>>> transaction.commit()
     152signal:  pre_commit
     153sender:  default
     154signal:  post_commit
     155sender:  default
     156
     157>>> transaction.rollback()
     158signal:  pre_rollback
     159sender:  default
     160signal:  post_rollback
     161sender:  default
     162
    132163"""
    133164
     165
    134166# Regression for #11900: If a function wrapped by commit_on_success writes a
    135167# transaction that can't be committed, that transaction should be rolled back.
    136168# The bug is only visible using the psycopg2 backend, though
Back to Top