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
|
Perform database operations atomically within a transaction. |
|
Create a schema editor for the purpose of collecting SQL. |
|
Convert a table name to a format required by the database backend. |
|
Return the name of a constraint. |
|
Return the name for an index for a field. |
|
Return the name of an index for an index_together. |
|
Return models that can be installed in a database. |
|
Return whether a database router allows migrate operations for a model. |
|
Return whether a database router allows a schema upgrade for a model. |
|
Return whether a database router allows syncdb operations for a model. |
|
Return a digest hash for a set of arguments. |
|
Return SQL statements for adding constraints. |
|
Return SQL statements for creating all models for an app. |
Return SQL statements for creating a ManyToManyField's table. |
|
|
Return SQL statements for creating a list of models. |
|
Return SQL statements for deleting all models in an app. |
|
Return SQL statements for deleting constraints. |
|
Return SQL statements for deleting an index. |
|
Return SQL statements for creating indexes for a field. |
|
Return SQL statements for creating indexes covering multiple fields. |
|
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:
- Returns:
The generated constraint name for this version of Django.
- Return type:
- 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
ofstr
, optional) – The list of field names for the index.col_names (
list
ofstr
, 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:
- 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.
- django_evolution.compat.db.db_get_installable_models_for_app(app, db_state)
Return models that can be installed in a database.
- Parameters:
app (
module
) – The models module for the app.db_state (
django_evolution.db.state.DatabaseState
) – The introspected state of the database.
- 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 ofallow_migrate
.
- 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()
anddb_router_allows_syncdb()
.
- 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.
- 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.
- 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:
connection (
object
) – The database connection.model (
django.db.models.Model
) – The database model to add constraints on.refs (
dict
) –A dictionary of constraint references to add.
The keys are instances of
django.db.models.Model
. The values are a tuple of (django.db.models.Model
,django.db.models.Field
).Warning
Keys may be removed as constraints are added. Make sure to pass in a copy of the dictionary if the original dictionary msut be preserved.
- Returns:
The list of SQL statements for adding constraints.
- Return type:
- 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.
- 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:
tables (
list
ofunicode
, 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. IfTrue
, 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:
- 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:
connection (
object
) – The database connection.model (
django.db.models.Model
) – The model for the ManyToManyField’s relations.field (
django.db.models.ManyToManyField
) – The field setting up the many-to-many relation.
- Returns:
The list of SQL statements for creating the table and constraints.
- Return type:
- 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.
- 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:
connection (
object
) – The database connection.model (
django.db.models.Model
) – The database model to delete constraints on.remove_refs (
dict
) –A dictionary of constraint references to remove.
The keys are instances of
django.db.models.Model
. The values are a tuple of (django.db.models.Model
,django.db.models.Field
).Warning
Keys may be removed as constraints are deleted. Make sure to pass in a copy of the dictionary if the original dictionary msut be preserved.
- Returns:
The list of SQL statements for deleting constraints.
- Return type:
- 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:
- 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:
connection (
object
) – The database connection.model (
django.db.models.Model
) – The database model owning the field.field (
django.db.models.Field
) – The field being indexed.
- Returns:
The list of SQL statements for creating the indexes.
- Return type:
- 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:
connection (
object
) – The database connection.model (
django.db.models.Model
) – The database model owning the fields.fields (
list
ofdjango.db.models.Field
) – The list of fields for the index.index_together (
bool
, optional) – Whether this is from an index_together rule.
- Returns:
The list of SQL statements for creating the indexes.
- Return type:
- 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:
connection (
object
) – The database connection.model (
django.db.models.Model
) – The database model to create indexes for.
- Returns:
The list of SQL statements for creating the indexes.
- Return type:
- 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.