django_evolution.utils.sql

Utilities for working with SQL statements.

Classes

BaseGroupedSQL(sql)

Base class for a grouped list of SQL statements.

NewTransactionSQL(sql)

A list of SQL statements to execute in its own transaction.

NoTransactionSQL(sql)

A list of SQL statements to execute outside of a transaction.

SQLExecutor(database[, check_constraints])

Management for the execution of SQL.

class django_evolution.utils.sql.BaseGroupedSQL(sql)

Bases: object

Base class for a grouped list of SQL statements.

This is a simple wrapper around a list of SQL statements, used to group statements under some category defined by a subclass.

sql

A list of SQL statements, as allowed by run_sql().

Type:

list

__init__(sql)

Initialize the group.

Parameters:

sql (list) – A list of SQL statements, as allowed by run_sql().

class django_evolution.utils.sql.NewTransactionSQL(sql)

Bases: BaseGroupedSQL

A list of SQL statements to execute in its own transaction.

class django_evolution.utils.sql.NoTransactionSQL(sql)

Bases: BaseGroupedSQL

A list of SQL statements to execute outside of a transaction.

class django_evolution.utils.sql.SQLExecutor(database, check_constraints=True)

Bases: object

Management for the execution of SQL.

This allows callers to perform raw SQL queries against the database, and to do so with a fine degree of transaction management. Callers can continually add new SQL to execute and, in-between, enter into a new transaction, ensure a previous transaction is already open, or close out any existing transaction.

Through this, it can effectively script a set of transactions and queries in a more loose form than normally allowed by Django.

New in version 2.1.

__init__(database, check_constraints=True)

Initialize the executor.

Parameters:
  • database (unicode) – The registered database name where queries will be executed.

  • check_constraints (bool, optional) – Whether to check constraints during the execution of SQL. If disabled, it’s up to the caller to manually invoke a constraint check.

__enter__()

Enter the context manager.

This will prepare internal state for execution, and optionally disable constraint checking (if requested during construction).

The context manager must be entered before operations will work.

Context:

SQLExecutor – This instance.

__exit__(*args, **kwargs)

Exit the context manager.

This will commit any transaction that may be in progress, close the database cursor, and re-enable constraint checking if it were previously disabled.

Parameters:
  • *args (tuple, unused) – Unused positional arguments.

  • **kwargs (dict, unused) – Unused keyword arguments.

new_transaction()

Start a new transaction.

This will commit any prior transaction, if one exists, and then start a new one.

ensure_transaction()

Ensure a transaction has started.

If no existing transaction has started, this will start a new one.

finish_transaction()

Finish and commit a transaction.

run_sql(sql, capture=False, execute=False)

Run (execute and/or capture) a list of SQL statements.

Parameters:
  • sql (list) – A list of SQL statements. Each entry might be a string, a tuple consisting of a format string and formatting arguments, or a subclass of BaseGroupedSQL, or a callable that returns a list of the above.

  • capture (bool, optional) – Whether to capture any processed SQL statements.

  • execute (bool, optional) – Whether to execute any executed SQL statements and return them.

Returns:

The list of SQL statements executed, if passing capture=True. Otherwise, this will just be an empty list.

Return type:

list of unicode

Raises:

django.db.transaction.TransactionManagementError – Could not execute a batch of SQL statements inside of an existing transaction.