django_evolution.db.state

Database state tracking for in-progress evolutions.

Classes

DatabaseState(db_name[, scan])

Tracks some useful state in the database.

IndexState(name[, columns, unique])

An index recorded in the database state.

class django_evolution.db.state.IndexState(name, columns=[], unique=False)

Bases: object

An index recorded in the database state.

__init__(name, columns=[], unique=False)

Initialize the index state.

Parameters:
  • name (unicode, optional) – The name of the index.

  • columns (list of unicode, optional) – A list of columns that the index is comprised of.

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

__eq__(other_state)

Return whether two index states are equal.

Parameters:

other_state (IndexState) – The other index state to compare to.

Returns:

True if the two index states are equal. False if they are not.

Return type:

bool

__hash__()

Return a hash representation of the index.

Returns:

The hash representation.

Return type:

int

__repr__()

Return a string representation of the index state.

Returns:

A string representation of the index.

Return type:

unicode

class django_evolution.db.state.DatabaseState(db_name, scan=True)

Bases: object

Tracks some useful state in the database.

This primarily tracks indexes associated with tables, allowing them to be scanned from the database, explicitly added, removed, or cleared.

__init__(db_name, scan=True)

Initialize the state.

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

  • scan (bool, optional) – Whether to automatically scan state from the database during initialization. By default, information is scanned.

clone()

Clone the database state.

Returns:

The cloned copy of the state.

Return type:

DatabaseState

add_table(table_name)

Add a table to track.

This will add an empty entry for the table to the state.

Parameters:

table_name (unicode) – The name of the table.

has_table(table_name)

Return whether a table is being tracked.

This does not necessarily mean that the table exists in the database. Rather, state for the table is being tracked.

Parameters:

table_name (unicode) – The name of the table to look up.

Returns:

True if the table is being tracked. False if it is not.

Return type:

bool

has_model(model)

Return whether a database model is installed in the database.

Parameters:

model (type) – The model class.

Returns:

True if the model has an accompanying table in the database. False if it does not.

Return type:

bool

add_index(table_name, index_name, columns, unique=False)

Add a table’s index to the database state.

This index can be used for later lookup during the evolution process. It won’t otherwise be preserved, though the resulting indexes are expected to match the result in the database.

This requires the table to be tracked first.

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

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

  • columns (list of unicode) – A list of column names the index is comprised of.

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

Raises:

django_evolution.errors.DatabaseStateError – There was an issue adding this index. Details are in the exception’s message.

remove_index(table_name, index_name, unique=False)

Remove an index from the database state.

This index will no longer be found during lookups when generating evolution SQL, even if it exists in the database.

This requires the table to be tracked first and for the index to both exist and match the unique flag.

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

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

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

Raises:

django_evolution.errors.DatabaseStateError – There was an issue removing this index. Details are in the exception’s message.

get_index(table_name, index_name, unique=False)

Return the index state for a given name.

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

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

  • unique (bool, optional) –

    Whether this is a unique index.

    New in version 2.2.

Returns:

The state for the index, if found. None if the index could not be found.

Return type:

IndexState

find_index(table_name, columns, unique=False)

Find and return an index matching the given columns and flags.

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

  • columns (list of unicode) – The list of columns the index is comprised of.

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

Returns:

The state for the index, if found. None if an index matching the criteria could not be found.

Return type:

IndexState

clear_indexes(table_name)

Clear all recorded indexes for a table.

Parameters:

table_name (unicode) – The name of the table.

iter_indexes(table_name)

Iterate through all indexes for a table.

Parameters:

table_name (unicode) – The name of the table.

Yields:

IndexState – An index in the table.

rescan_tables()

Rescan the list of tables from the database.

This will look up all tables found in the database, along with information (such as indexes) on those tables.

Existing information on the tables will be flushed.