Refs #23879 -- Made introspection respect required_db_features.

This commit is contained in:
Simon Charette 2019-08-11 21:46:23 -04:00 committed by Mariusz Felisiak
parent 73ac9e3f04
commit 2fb872e56f
1 changed files with 31 additions and 33 deletions

View File

@ -54,6 +54,16 @@ class BaseDatabaseIntrospection:
""" """
raise NotImplementedError('subclasses of BaseDatabaseIntrospection may require a get_table_list() method') raise NotImplementedError('subclasses of BaseDatabaseIntrospection may require a get_table_list() method')
def get_migratable_models(self):
from django.apps import apps
from django.db import router
return (
model
for app_config in apps.get_app_configs()
for model in router.get_migratable_models(app_config, self.connection.alias)
if model._meta.can_migrate(self.connection)
)
def django_table_names(self, only_existing=False, include_views=True): def django_table_names(self, only_existing=False, include_views=True):
""" """
Return a list of all table names that have associated Django models and Return a list of all table names that have associated Django models and
@ -61,18 +71,15 @@ class BaseDatabaseIntrospection:
If only_existing is True, include only the tables in the database. If only_existing is True, include only the tables in the database.
""" """
from django.apps import apps
from django.db import router
tables = set() tables = set()
for app_config in apps.get_app_configs(): for model in self.get_migratable_models():
for model in router.get_migratable_models(app_config, self.connection.alias): if not model._meta.managed:
if not model._meta.managed: continue
continue tables.add(model._meta.db_table)
tables.add(model._meta.db_table) tables.update(
tables.update( f.m2m_db_table() for f in model._meta.local_many_to_many
f.m2m_db_table() for f in model._meta.local_many_to_many if f.remote_field.through._meta.managed
if f.remote_field.through._meta.managed )
)
tables = list(tables) tables = list(tables)
if only_existing: if only_existing:
existing_tables = set(self.table_names(include_views=include_views)) existing_tables = set(self.table_names(include_views=include_views))
@ -88,14 +95,9 @@ class BaseDatabaseIntrospection:
Return a set of all models represented by the provided list of table Return a set of all models represented by the provided list of table
names. names.
""" """
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))
tables = set(map(self.identifier_converter, tables)) tables = set(map(self.identifier_converter, tables))
return { return {
m for m in all_models m for m in self.get_migratable_models()
if self.identifier_converter(m._meta.db_table) in tables if self.identifier_converter(m._meta.db_table) in tables
} }
@ -104,24 +106,20 @@ class BaseDatabaseIntrospection:
Return a list of information about all DB sequences for all models in Return a list of information about all DB sequences for all models in
all apps. all apps.
""" """
from django.apps import apps
from django.db import router
sequence_list = [] sequence_list = []
with self.connection.cursor() as cursor: with self.connection.cursor() as cursor:
for app_config in apps.get_app_configs(): for model in self.get_migratable_models():
for model in router.get_migratable_models(app_config, self.connection.alias): if not model._meta.managed:
if not model._meta.managed: continue
continue if model._meta.swapped:
if model._meta.swapped: continue
continue sequence_list.extend(self.get_sequences(cursor, model._meta.db_table, model._meta.local_fields))
sequence_list.extend(self.get_sequences(cursor, model._meta.db_table, model._meta.local_fields)) for f in model._meta.local_many_to_many:
for f in model._meta.local_many_to_many: # If this is an m2m using an intermediate table,
# If this is an m2m using an intermediate table, # we don't need to reset the sequence.
# we don't need to reset the sequence. if f.remote_field.through._meta.auto_created:
if f.remote_field.through._meta.auto_created: sequence = self.get_sequences(cursor, f.m2m_db_table())
sequence = self.get_sequences(cursor, f.m2m_db_table()) sequence_list.extend(sequence or [{'table': f.m2m_db_table(), 'column': None}])
sequence_list.extend(sequence or [{'table': f.m2m_db_table(), 'column': None}])
return sequence_list return sequence_list
def get_sequences(self, cursor, table_name, table_fields=()): def get_sequences(self, cursor, table_name, table_fields=()):