django_evolution.db.sqlite3

Evolution operations backend for SQLite.

Classes

EvolutionOperations(database_state[, connection])

Evolution operations backend for SQLite.

SQLiteAlterTableSQLResult(evolver, model[, ...])

Represents SQL statements used to rebuild a table on SQLite.

class django_evolution.db.sqlite3.SQLiteAlterTableSQLResult(evolver, model, alter_table=None, *args, **kwargs)

Bases: AlterTableSQLResult

Represents SQL statements used to rebuild a table on SQLite.

Unlike most databases, SQLite doesn’t offer typical ALTER TABLE support, instead requiring a full table rebuild and data transfer. This class handles that process, allowing operations for the rebuild (adding, deleting, or changing columns) to be batched together.

The rebuild uses the step-by-step instructions recommended by SQLite. It creates a new table with the desired schema, copies all data from the old table, drops the old table, and then renames the new table over.

It can also update the newly-populated rows in the new table with new initial data, if needed by a new column.

to_sql()

Return a list of SQL statements for the table rebuild.

Any alter_table operations will be collapsed together into a single table rebuild.

Returns:

The list of SQL statements to run for the rebuild.

Return type:

list of unicode

class django_evolution.db.sqlite3.EvolutionOperations(database_state, connection=<django.db.DefaultConnectionProxy object>)

Bases: BaseEvolutionOperations

Evolution operations backend for SQLite.

name = 'SQLite'

The name of the database type.

New in version 2.3.

supported_change_meta = {'constraints': True, 'db_table_comment': False, 'index_together': True, 'indexes': True, 'unique_together': True}
alter_table_sql_result_cls

alias of SQLiteAlterTableSQLResult

get_deferrable_sql()

Return the SQL for marking a reference as deferrable.

Despite SQLite3 supporting this, the Django SQLite3 backend does not implement the standard function (BaseDatabaseOperations.deferrable_sql()) for this.

This provides a value used internally for building references.

New in version 2.2.

Returns:

The SQL for marking a reference as deferrable.

Return type:

unicode

rename_table(model, old_db_table, new_db_table)

Rename a table.

Parameters:
Returns:

The resulting SQL for renaming the table.

Return type:

django_evolution.db.sql_result.SQLResult

delete_column(model, field)

Delete a column from the table.

Parameters:
  • model (type) – The Model class representing the table to delete the column from.

  • field (django.db.models.Field) – The field representing the column to delete.

Returns:

The resulting SQL for rebuilding the table.

Return type:

django_evolution.db.sql_result.SQLResult

rename_column(model, old_field, new_field)

Rename a column on a table.

Parameters:
Returns:

The resulting SQL for rebuilding the table.

Return type:

django_evolution.db.sql_result.SQLResult

add_column(model, field, initial)

Add a column to the table.

Parameters:
  • model (type) – The Model class representing the table to add the column to.

  • field (django.db.models.Field) – The field representing the column to add.

  • initial (object) –

    The initial data to set for the column. If None, the data will not be set.

    This will be required for NOT NULL columns.

Returns:

The resulting SQL for rebuilding the table.

Return type:

django_evolution.db.sql_result.SQLResult

change_column_attr_null(model, mutation, field, old_value, new_value)

Change a column’s NULL flag.

Parameters:
  • model (type) – The Model class representing the table to change the column on.

  • mutation (django_evolution.mutations.BaseModelMutation) – The mutation making this change.

  • field (django.db.models.Field) – The field representing the column to change.

  • old_value (bool, unused) – The old null flag.

  • new_value (bool) – The new null flag.

Returns:

The resulting SQL for rebuilding the table.

Return type:

django_evolution.db.sql_result.SQLResult

change_column_attr_decimal_type(model, mutation, field, new_max_digits, new_decimal_places)

Return SQL for changing a column’s decimal_places attribute.

This is used for DecimalField and subclasses to change the maximum number of digits or decimal places. As these are used together as a column type, they must be considered together as one attribute change.

Parameters:
  • model (type) – The model class that owns the field.

  • mutation (django_evolution.mutations.BaseModelMutation) – The mutation applying this change.

  • field (django.db.models.DecimalField) – The field being modified.

  • new_max_digits (int) – The new value for max_digits. If None, it wasn’t provided in the attribute change.

  • new_decimal_places (int) – The new value for decimal_places. If None, it wasn’t provided in the attribute change.

Returns:

The SQL for modifying the value.

Return type:

django_evolution.db.sql_result.AlterTableSQLResult

change_column_attr_max_length(model, mutation, field, old_value, new_value)

Change a column’s max length.

Parameters:
  • model (type) – The Model class representing the table to change the column on.

  • mutation (django_evolution.mutations.BaseModelMutation) – The mutation making this change.

  • field (django.db.models.Field) – The field representing the column to change.

  • old_value (int, unused) – The old max length.

  • new_value (int, unused) – The new max length.

Returns:

The resulting SQL for rebuilding the table.

Return type:

django_evolution.db.sql_result.SQLResult

change_column_type(model, old_field, new_field, new_attrs)

Return SQL to change the type of a column.

New in version 2.2.

Parameters:
Returns:

The SQL statements for changing the column type.

Return type:

SQLiteAlterTableSQLResult

get_change_unique_sql(model, field, new_unique_value, constraint_name, initial)

Change a column’s unique flag.

Parameters:
  • model (type) – The Model class representing the table to change the column on.

  • mutation (django_evolution.mutations.BaseModelMutation) – The mutation making this change.

  • field (django.db.models.Field) – The field representing the column to change.

  • old_value (bool, unused) – The old unique flag.

  • new_value (bool, unused) – The new unique flag.

Returns:

The resulting SQL for rebuilding the table.

Return type:

django_evolution.db.sql_result.SQLResult

get_update_table_constraints_sql(model, old_constraints, new_constraints, to_add, to_remove)

Return SQL for updating the constraints on a table.

This will perform a table rebuild, including only any new constraints in the new schema.

Parameters:
  • model (django.db.models.Model) – The model being changed.

  • old_constraints (list of :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` django.db.models.constraints.BaseConstraint) – The old constraints pre-evolution.

  • new_constraints (list of :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` django.db.models.constraints.BaseConstraint) – The new constraints post-evolution.

  • to_add (list of django.db.models.constraints.BaseConstraint) – A list of new constraints to add to the database that weren’t set before.

  • to_remove (list of django.db.models.constraints.BaseConstraint) – A list of old constraints to remove from the database that aren’t set now.

Returns:

The SQL statements for changing the constraints.

Return type:

django_evolution.sql_result.SQLResult

change_column_attr_db_index(model, mutation, field, old_value, new_value)

Return the SQL for creating/dropping indexes for a column.

If setting db_index=True, SQL for generating the index will be returned immediately.

If setting db_index=False, the dropping of the index will be scheduled as an Alter Table operation, ensuring it’s dropped immediately (and cached/queued database index state updated) before the table is rebuilt, never later.

New in version 2.3.

Parameters:
  • model (django.db.models.Model) – The model being changed.

  • mutation (django_evolution.mutations.BaseModelMutation) – The mutation applying this change.

  • field (django.db.models.DecimalField) – The field being modified.

  • old_value (bool) – The old value for db_index.

  • new_value (bool) – The new value for db_index.

Returns:

The resulting SQL for creating the index or scheduling a drop.

Return type:

django_evolution.db.sql_result.SQLResult

get_drop_unique_constraint_sql(model, index_name)

Return SQL for dropping unique constraints.

Parameters:
  • model (type) – The Model class representing the table to drop unique constraints on.

  • index_name (unicode) – The name of the unique constraint index to drop.

Returns:

The resulting SQL for rebuilding the table.

Return type:

django_evolution.db.sql_result.SQLResult

get_indexes_for_table(table_name)

Return all known indexes on a table.

This is a fallback used only on Django 1.6, due to lack of proper introspection on that release.

Parameters:

table_name (unicode) – The name of the table.

Returns:

A dictionary mapping index names to a dictionary containing:

columns (list):

The list of columns that the index covers.

unique (bool):

Whether this is a unique index.

Return type:

dict

is_column_referenced(reffed_table_name, reffed_col_name)

Return whether a column on a table is referenced by another table.

Parameters:
  • reffed_table_name (unicode) – The name of the table that may be referenced.

  • reffed_col_name (unicode) – The name of the column that may be referenced.

Returns:

True if this table and column are referenced by another table, or False if it’s not referenced.

Return type:

bool