Ejemplo n.º 1
0
def atomic(app, using, savepoint):
    """Synnefo wrapper for Django atomic transactions.

    This function serves as a wrapper for Django atomic(). Its goal is to
    assist in using transactions in a multi-database setup.

    We explicitly forbid nested transactions --- we need to be sure that an
    atomic block really commits to the database rather than acting as a
    savepoint for an outer atomic block.
    """
    db = select_db(app) if not using or callable(using) else using
    ctx = NonNestedAtomic(db, savepoint)
    return ctx(using) if callable(using) else ctx
Ejemplo n.º 2
0
def rollback(using=None):
    using = select_db("im") if using is None else using
    django_transaction.rollback(using=using)
Ejemplo n.º 3
0
def commit(using=None):
    using = select_db("im") if using is None else using
    django_transaction.commit(using=using)
Ejemplo n.º 4
0
def rollback(using=None):
    using = select_db("db") if using is None else using
    transaction.rollback(using=using)
Ejemplo n.º 5
0
def commit(using=None):
    using = select_db("db") if using is None else using
    transaction.commit(using=using)
Ejemplo n.º 6
0
def _transaction_func(app, method, using):
    """Synnefo wrapper for Django transactions.

    This function serves as a wrapper for Django transaction functions. It is
    mainly used by Synnefo transaction functions and its goal is to assist in
    using transactions in a multi-database setup.

    Arguments:
        @app: The name of app that initiates the transaction (e.g. "db", "im"),
        @method: The actual Django transaction function that will be used (e.g.
                 "commit_manually", "commit_on_success")
        @using: The database to use for the transaction (e.g. "cyclades",
                "astakos")

    Returns:
        Either a decorator/context manageer or a wrapped function, depending on
        how the aforementioned Synnefo transaction functions are called.

    To illustrate the return value, let's consider the following Cyclades
    transaction function:

    >>> from django.db import transaction
    >>> def commit_on_success(using=None):
    >>>     method = transaction.commit_on_success
    >>>     return _transaction_func("db", method, using)

    We present below two possible uses of the above function and what will
    _transaction_func return for each of them:

    1) Decorator with provided database:

    >>> @transaction.commit_on_success(using="other_db")
    >>> def func(...):
    >>>     ...

    In this case, the arguments for _transaction_func are:
        app = "db"
        method = transaction.commit_on_success
        using = "other_db"

    The returned result is the following_decorator:

        transaction.commit_on_success(using="other_db")

    2) Decorator with no database provided:

    >>> @transaction.commit_on_success
    >>> def func(...):
    >>>     ...

    In this case, the arguments for _transaction_func are:
        app = "db"
        method = transaction.commit_on_success
        using = func

    Not so surpringly, in this case the "using" argument contains the function
    to wrap instead of a database. Therefore, _transaction_func will determine
    what is the appropriate database for the app and return the decorated
    function:

        transaction.commit_on_success(using="db")(func)
    """
    db = using
    if not using or callable(using):
        db = select_db(app)

    if callable(using):
        return method(using=db)(using)
    else:
        return method(using=db)
Ejemplo n.º 7
0
 def db_for_write(self, model, **hints):
     """Select db to write."""
     app = model._meta.app_label
     return select_db(app)
Ejemplo n.º 8
0
 def db_for_read(self, model, **hints):
     """Select db to read."""
     app = model._meta.app_label
     return select_db(app)
Ejemplo n.º 9
0
def rollback(using=None):
    using = select_db("im") if using is None else using
    django_transaction.rollback(using=using)
Ejemplo n.º 10
0
def commit(using=None):
    using = select_db("im") if using is None else using
    django_transaction.commit(using=using)
Ejemplo n.º 11
0
 def db_for_write(self, model, **hints):
     """Select db to write."""
     app = model._meta.app_label
     return select_db(app)
Ejemplo n.º 12
0
 def db_for_read(self, model, **hints):
     """Select db to read."""
     app = model._meta.app_label
     return select_db(app)