2015-01-13 04:20:40 +08:00
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
# Structure returned by DatabaseIntrospection.get_table_list()
|
|
|
|
TableInfo = namedtuple('TableInfo', ['name', 'type'])
|
|
|
|
|
|
|
|
# Structure returned by the DB-API cursor.description interface (PEP 249)
|
2016-12-01 00:21:57 +08:00
|
|
|
FieldInfo = namedtuple('FieldInfo', 'name type_code display_size internal_size precision scale null_ok default')
|
2015-01-13 04:20:40 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class BaseDatabaseIntrospection:
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Encapsulate backend-specific introspection utilities."""
|
2015-01-13 04:20:40 +08:00
|
|
|
data_types_reverse = {}
|
|
|
|
|
|
|
|
def __init__(self, connection):
|
|
|
|
self.connection = connection
|
|
|
|
|
|
|
|
def get_field_type(self, data_type, description):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""
|
|
|
|
Hook for a database backend to use the cursor description to
|
2015-01-13 04:20:40 +08:00
|
|
|
match a Django field type to a database column.
|
|
|
|
|
|
|
|
For Oracle, the column data_type on its own is insufficient to
|
2017-01-25 07:04:12 +08:00
|
|
|
distinguish between a FloatField and IntegerField, for example.
|
|
|
|
"""
|
2015-01-13 04:20:40 +08:00
|
|
|
return self.data_types_reverse[data_type]
|
|
|
|
|
2018-11-21 16:06:50 +08:00
|
|
|
def identifier_converter(self, name):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""
|
2018-11-21 16:06:50 +08:00
|
|
|
Apply a conversion to the identifier for the purposes of comparison.
|
2015-01-13 04:20:40 +08:00
|
|
|
|
2018-11-21 16:06:50 +08:00
|
|
|
The default identifier converter is for case sensitive comparison.
|
2015-01-13 04:20:40 +08:00
|
|
|
"""
|
|
|
|
return name
|
|
|
|
|
|
|
|
def table_names(self, cursor=None, include_views=False):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Return a list of names of all tables that exist in the database.
|
|
|
|
Sort the returned table list by Python's default sorting. Do NOT use
|
|
|
|
the database's ORDER BY here to avoid subtle differences in sorting
|
|
|
|
order between databases.
|
2015-01-13 04:20:40 +08:00
|
|
|
"""
|
|
|
|
def get_names(cursor):
|
|
|
|
return sorted(ti.name for ti in self.get_table_list(cursor)
|
|
|
|
if include_views or ti.type == 't')
|
|
|
|
if cursor is None:
|
|
|
|
with self.connection.cursor() as cursor:
|
|
|
|
return get_names(cursor)
|
|
|
|
return get_names(cursor)
|
|
|
|
|
|
|
|
def get_table_list(self, cursor):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Return an unsorted list of TableInfo named tuples of all tables and
|
2015-01-13 04:20:40 +08:00
|
|
|
views that exist in the database.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError('subclasses of BaseDatabaseIntrospection may require a get_table_list() method')
|
|
|
|
|
|
|
|
def django_table_names(self, only_existing=False, include_views=True):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Return a list of all table names that have associated Django models and
|
2015-01-13 04:20:40 +08:00
|
|
|
are in INSTALLED_APPS.
|
|
|
|
|
2017-01-25 07:04:12 +08:00
|
|
|
If only_existing is True, include only the tables in the database.
|
2015-01-13 04:20:40 +08:00
|
|
|
"""
|
|
|
|
from django.apps import apps
|
|
|
|
from django.db import router
|
|
|
|
tables = set()
|
|
|
|
for app_config in apps.get_app_configs():
|
|
|
|
for model in router.get_migratable_models(app_config, self.connection.alias):
|
|
|
|
if not model._meta.managed:
|
|
|
|
continue
|
|
|
|
tables.add(model._meta.db_table)
|
2016-03-02 12:10:46 +08:00
|
|
|
tables.update(
|
|
|
|
f.m2m_db_table() for f in model._meta.local_many_to_many
|
|
|
|
if f.remote_field.through._meta.managed
|
|
|
|
)
|
2015-01-13 04:20:40 +08:00
|
|
|
tables = list(tables)
|
|
|
|
if only_existing:
|
2018-11-21 16:06:50 +08:00
|
|
|
existing_tables = set(self.table_names(include_views=include_views))
|
2015-01-13 04:20:40 +08:00
|
|
|
tables = [
|
|
|
|
t
|
|
|
|
for t in tables
|
2018-11-21 16:06:50 +08:00
|
|
|
if self.identifier_converter(t) in existing_tables
|
2015-01-13 04:20:40 +08:00
|
|
|
]
|
|
|
|
return tables
|
|
|
|
|
|
|
|
def installed_models(self, tables):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""
|
|
|
|
Return a set of all models represented by the provided list of table
|
|
|
|
names.
|
|
|
|
"""
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.apps import apps
|
|
|
|
from django.db import router
|
|
|
|
all_models = []
|
|
|
|
for app_config in apps.get_app_configs():
|
|
|
|
all_models.extend(router.get_migratable_models(app_config, self.connection.alias))
|
2018-11-21 16:06:50 +08:00
|
|
|
tables = set(map(self.identifier_converter, tables))
|
2015-01-13 04:20:40 +08:00
|
|
|
return {
|
|
|
|
m for m in all_models
|
2018-11-21 16:06:50 +08:00
|
|
|
if self.identifier_converter(m._meta.db_table) in tables
|
2015-01-13 04:20:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
def sequence_list(self):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""
|
|
|
|
Return a list of information about all DB sequences for all models in
|
|
|
|
all apps.
|
|
|
|
"""
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.apps import apps
|
2017-09-14 02:12:32 +08:00
|
|
|
from django.db import router
|
2015-01-13 04:20:40 +08:00
|
|
|
|
|
|
|
sequence_list = []
|
2017-11-28 21:12:28 +08:00
|
|
|
with self.connection.cursor() as cursor:
|
|
|
|
for app_config in apps.get_app_configs():
|
|
|
|
for model in router.get_migratable_models(app_config, self.connection.alias):
|
|
|
|
if not model._meta.managed:
|
|
|
|
continue
|
|
|
|
if model._meta.swapped:
|
|
|
|
continue
|
|
|
|
sequence_list.extend(self.get_sequences(cursor, model._meta.db_table, model._meta.local_fields))
|
|
|
|
for f in model._meta.local_many_to_many:
|
|
|
|
# If this is an m2m using an intermediate table,
|
|
|
|
# we don't need to reset the sequence.
|
2018-07-11 16:18:41 +08:00
|
|
|
if f.remote_field.through._meta.auto_created:
|
2017-11-28 21:12:28 +08:00
|
|
|
sequence = self.get_sequences(cursor, f.m2m_db_table())
|
|
|
|
sequence_list.extend(sequence or [{'table': f.m2m_db_table(), 'column': None}])
|
2015-01-13 04:20:40 +08:00
|
|
|
return sequence_list
|
|
|
|
|
2017-09-14 02:12:32 +08:00
|
|
|
def get_sequences(self, cursor, table_name, table_fields=()):
|
|
|
|
"""
|
|
|
|
Return a list of introspected sequences for table_name. Each sequence
|
|
|
|
is a dict: {'table': <table_name>, 'column': <column_name>}. An optional
|
|
|
|
'name' key can be added if the backend supports named sequences.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError('subclasses of BaseDatabaseIntrospection may require a get_sequences() method')
|
|
|
|
|
2015-01-13 04:20:40 +08:00
|
|
|
def get_key_columns(self, cursor, table_name):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Backends can override this to return a list of:
|
|
|
|
(column_name, referenced_table_name, referenced_column_name)
|
|
|
|
for all key columns in given table.
|
2015-01-13 04:20:40 +08:00
|
|
|
"""
|
|
|
|
raise NotImplementedError('subclasses of BaseDatabaseIntrospection may require a get_key_columns() method')
|
|
|
|
|
|
|
|
def get_primary_key_column(self, cursor, table_name):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Return the name of the primary key column for the given table.
|
2015-01-13 04:20:40 +08:00
|
|
|
"""
|
2016-08-20 18:14:02 +08:00
|
|
|
for constraint in self.get_constraints(cursor, table_name).values():
|
|
|
|
if constraint['primary_key']:
|
|
|
|
return constraint['columns'][0]
|
2015-01-13 04:20:40 +08:00
|
|
|
return None
|
|
|
|
|
|
|
|
def get_constraints(self, cursor, table_name):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Retrieve any constraints or keys (unique, pk, fk, check, index)
|
2015-01-13 04:20:40 +08:00
|
|
|
across one or more columns.
|
|
|
|
|
2017-01-25 07:04:12 +08:00
|
|
|
Return a dict mapping constraint names to their attributes,
|
2015-01-13 04:20:40 +08:00
|
|
|
where attributes is a dict with keys:
|
|
|
|
* columns: List of columns this covers
|
|
|
|
* primary_key: True if primary key, False otherwise
|
|
|
|
* unique: True if this is a unique constraint, False otherwise
|
|
|
|
* foreign_key: (table, column) of target, or None
|
|
|
|
* check: True if check constraint, False otherwise
|
|
|
|
* index: True if index, False otherwise.
|
2016-08-25 15:12:17 +08:00
|
|
|
* orders: The order (ASC/DESC) defined for the columns of indexes
|
|
|
|
* type: The type of the index (btree, hash, etc.)
|
2015-01-13 04:20:40 +08:00
|
|
|
|
|
|
|
Some backends may return special constraint names that don't exist
|
|
|
|
if they don't name constraints of a certain type (e.g. SQLite)
|
|
|
|
"""
|
|
|
|
raise NotImplementedError('subclasses of BaseDatabaseIntrospection may require a get_constraints() method')
|