django_evolution.compat.db

Compatibility functions for database-related operations.

This provides functions for database operations, SQL generation, index name generation, and more. These functions translate to the various versions of Django that are supported.

Functions

atomic([using])

Perform database operations atomically within a transaction.

collect_sql_schema_editor(connection)

Create a schema editor for the purpose of collecting SQL.

convert_table_name(connection, name)

Convert a table name to a format required by the database backend.

create_constraint_name(connection, r_col, ...)

Return the name of a constraint.

create_index_name(connection, table_name[, ...])

Return the name for an index for a field.

create_index_together_name(connection, ...)

Return the name of an index for an index_together.

db_get_installable_models_for_app(app, db_state)

Return models that can be installed in a database.

db_router_allows_migrate(database, ...)

Return whether a database router allows migrate operations for a model.

db_router_allows_schema_upgrade(database, ...)

Return whether a database router allows a schema upgrade for a model.

db_router_allows_syncdb(database, model_cls)

Return whether a database router allows syncdb operations for a model.

digest(connection, *args)

Return a digest hash for a set of arguments.

sql_add_constraints(connection, model, refs)

Return SQL statements for adding constraints.

sql_create_app(app[, db_name])

Return SQL statements for creating all models for an app.

sql_create_for_many_to_many_field(...)

Return SQL statements for creating a ManyToManyField's table.

sql_create_models(models[, tables, db_name, ...])

Return SQL statements for creating a list of models.

sql_delete(app[, db_name])

Return SQL statements for deleting all models in an app.

sql_delete_constraints(connection, model, ...)

Return SQL statements for deleting constraints.

sql_delete_index(connection, model, index_name)

Return SQL statements for deleting an index.

sql_indexes_for_field(connection, model, field)

Return SQL statements for creating indexes for a field.

sql_indexes_for_fields(connection, model, fields)

Return SQL statements for creating indexes covering multiple fields.

sql_indexes_for_model(connection, model)

Return SQL statements for creating all indexes for a model.

django_evolution.compat.db.atomic(using=None)

Perform database operations atomically within a transaction.

The caller can use this to ensure SQL statements are executed within a transaction and then cleaned up nicely if there’s an error.

This provides compatibility with all supported versions of Django.

Parameters:

using (str, optional) – The database connection name to use. Defaults to the default database connection.

django_evolution.compat.db.create_constraint_name(connection, r_col, col, r_table, table)

Return the name of a constraint.

This provides compatibility with all supported versions of Django.

Parameters:
  • connection (object) – The database connection.

  • r_col (str) – The column name for the source of the relation.

  • col (str) – The column name for the “to” end of the relation.

  • r_table (str) – The table name for the source of the relation.

  • table (str) – The table name for the “to” end of the relation.

Returns:

The generated constraint name for this version of Django.

Return type:

str

django_evolution.compat.db.create_index_name(connection, table_name, field_names=[], col_names=[], unique=False, suffix='')

Return the name for an index for a field.

This provides compatibility with all supported versions of Django.

Parameters:
  • connection (object) – The database connection.

  • table_name (str) – The name of the table.

  • field_names (list of str, optional) – The list of field names for the index.

  • col_names (list of str, optional) – The list of column names for the index.

  • unique (bool, optional) – Whether or not this index is unique.

  • suffix (str, optional) – A suffix for the index. This is only used with Django >= 1.7.

Returns:

The generated index name for this version of Django.

Return type:

str

django_evolution.compat.db.create_index_together_name(connection, table_name, field_names)

Return the name of an index for an index_together.

This provides compatibility with all supported versions of Django >= 1.5. Prior versions don’t support index_together.

Parameters:
  • connection (object) – The database connection.

  • table_name (str) – The name of the table.

  • field_names (list of str) – The list of field names indexed together.

Returns:

The generated index name for this version of Django.

Return type:

str

django_evolution.compat.db.db_get_installable_models_for_app(app, db_state)

Return models that can be installed in a database.

Parameters:
django_evolution.compat.db.db_router_allows_migrate(database, app_label, model_cls)

Return whether a database router allows migrate operations for a model.

This will only return True for Django 1.7 and newer and if the router allows migrate operations. This is compatible with both the Django 1.7 and 1.8+ versions of allow_migrate.

Parameters:
  • database (unicode) – The name of the database.

  • app_label (unicode) – The application label.

  • model_cls (type) – The model class.

Returns:

True if routers allow migrate for this model.

Return type:

bool

django_evolution.compat.db.db_router_allows_schema_upgrade(database, app_label, model_cls)

Return whether a database router allows a schema upgrade for a model.

This is a convenience wrapper around db_router_allows_migrate() and db_router_allows_syncdb().

Parameters:
  • database (unicode) – The name of the database.

  • app_label (unicode) – The application label.

  • model_cls (type) – The model class.

Returns:

True if routers allow migrate for this model.

Return type:

bool

django_evolution.compat.db.db_router_allows_syncdb(database, model_cls)

Return whether a database router allows syncdb operations for a model.

This will only return True for Django 1.6 and older and if the router allows syncdb operations.

Parameters:
  • database (unicode) – The name of the database.

  • model_cls (type) – The model class.

Returns:

True if routers allow syncdb for this model.

Return type:

bool

django_evolution.compat.db.digest(connection, *args)

Return a digest hash for a set of arguments.

This is mostly used as part of the index/constraint name generation processes. It offers compatibility with a range of Django versions.

Parameters:
  • connection (object) – The database connection.

  • *args (tuple) – The positional arguments used to build the digest hash out of.

Returns:

The resulting digest hash.

Return type:

str

django_evolution.compat.db.sql_add_constraints(connection, model, refs)

Return SQL statements for adding constraints.

This provides compatibility with all supported versions of Django.

Parameters:
Returns:

The list of SQL statements for adding constraints.

Return type:

list

django_evolution.compat.db.sql_create_app(app, db_name=None)

Return SQL statements for creating all models for an app.

This provides compatibility with all supported versions of Django.

Parameters:
  • app (module) – The application module.

  • db_name (str, optional) – The database connection name. Defaults to the default database connection.

Returns:

The list of SQL statements used to create the models for the app.

Return type:

list

django_evolution.compat.db.sql_create_models(models, tables=None, db_name=None, return_deferred=False)

Return SQL statements for creating a list of models.

This provides compatibility with all supported versions of Django.

It’s recommended that callers include auto-created models in the list, to ensure all references are correct.

Changed in version 2.2: Added the ``return_deferred` argument.

Parameters:
  • models (list of type) – The list of Model subclasses.

  • tables (list of unicode, optional) – A list of existing table names from the database. If not provided, this will be introspected from the database.

  • db_name (str, optional) – The database connection name. Defaults to the default database connection.

  • return_deferred (bool, optional) – Whether to return any deferred SQL separately from the model creation SQL. If True, the return type will change to a tuple.

Returns:

If return_deferred=False (the default), this will be a list of SQL statements used to create the models for the app.

If return_deferred=True, this will be a 2-tuple in the form of (list_of_sql, list_of_deferred_sql).

Return type:

list or tuple

django_evolution.compat.db.sql_create_for_many_to_many_field(connection, model, field)

Return SQL statements for creating a ManyToManyField’s table.

This provides compatibility with all supported versions of Django.

Parameters:
Returns:

The list of SQL statements for creating the table and constraints.

Return type:

list

django_evolution.compat.db.sql_delete(app, db_name=None)

Return SQL statements for deleting all models in an app.

This provides compatibility with all supported versions of Django.

Parameters:
  • app (module) – The application module containing the models to delete.

  • db_name (str, optional) – The database connection name. Defaults to the default database connection.

Returns:

The list of SQL statements for deleting the models and constraints.

Return type:

list

django_evolution.compat.db.sql_delete_constraints(connection, model, remove_refs)

Return SQL statements for deleting constraints.

This provides compatibility with all supported versions of Django.

Parameters:
Returns:

The list of SQL statements for deleting constraints.

Return type:

list

django_evolution.compat.db.sql_delete_index(connection, model, index_name)

Return SQL statements for deleting an index.

This provides compatibility with all supported versions of Django.

Parameters:
  • connection (object) – The database connection.

  • model (django.db.models.Model) – The database model to delete an index on.

  • index_name (unicode) – The name of the index to delete.

Returns:

The list of SQL statements for deleting the index.

Return type:

list

django_evolution.compat.db.sql_indexes_for_field(connection, model, field)

Return SQL statements for creating indexes for a field.

This provides compatibility with all supported versions of Django.

Parameters:
Returns:

The list of SQL statements for creating the indexes.

Return type:

list

django_evolution.compat.db.sql_indexes_for_fields(connection, model, fields, index_together=False)

Return SQL statements for creating indexes covering multiple fields.

This provides compatibility with all supported versions of Django.

Parameters:
Returns:

The list of SQL statements for creating the indexes.

Return type:

list

django_evolution.compat.db.sql_indexes_for_model(connection, model)

Return SQL statements for creating all indexes for a model.

This provides compatibility with all supported versions of Django.

Parameters:
Returns:

The list of SQL statements for creating the indexes.

Return type:

list

django_evolution.compat.db.truncate_name(identifier, length=None, hash_len=4)

Shorten a SQL identifier to a repeatable mangled version with the given length.

If a quote stripped name contains a namespace, e.g. USERNAME”.”TABLE, truncate the table portion only.