Skip to content

Commit

Permalink
Refs #29898 -- Changed ProjectState.real_apps to set.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixxm authored and carltongibson committed Aug 11, 2021
1 parent 3a6431d commit 54a30a7
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion django/db/migrations/executor.py
Expand Up @@ -66,7 +66,7 @@ def _create_project_state(self, with_applied_migrations=False):
Create a project state including all the applications without
migrations and applied migrations if with_applied_migrations=True.
"""
state = ProjectState(real_apps=list(self.loader.unmigrated_apps))
state = ProjectState(real_apps=self.loader.unmigrated_apps)
if with_applied_migrations:
# Create the forwards plan Django would follow on an empty database
full_plan = self.migration_plan(self.loader.graph.leaf_nodes(), clean_start=True)
Expand Down
2 changes: 1 addition & 1 deletion django/db/migrations/loader.py
Expand Up @@ -335,7 +335,7 @@ def project_state(self, nodes=None, at_end=True):
See graph.make_state() for the meaning of "nodes" and "at_end".
"""
return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps))
return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=self.unmigrated_apps)

def collect_sql(self, plan):
"""
Expand Down
12 changes: 7 additions & 5 deletions django/db/migrations/state.py
Expand Up @@ -91,7 +91,10 @@ class ProjectState:
def __init__(self, models=None, real_apps=None):
self.models = models or {}
# Apps to include from main registry, usually unmigrated ones
self.real_apps = real_apps or []
if real_apps:
self.real_apps = real_apps if isinstance(real_apps, set) else set(real_apps)
else:
self.real_apps = set()
self.is_delayed = False
# {remote_model_key: {model_key: [(field_name, field)]}}
self.relations = None
Expand Down Expand Up @@ -352,23 +355,22 @@ def resolve_fields_and_relations(self):
self.relations = defaultdict(partial(defaultdict, list))
concretes, proxies = self._get_concrete_models_mapping_and_proxy_models()

real_apps = set(self.real_apps)
for model_key in concretes:
model_state = self.models[model_key]
for field_name, field in model_state.fields.items():
remote_field = field.remote_field
if not remote_field:
continue
remote_model_key = resolve_relation(remote_field.model, *model_key)
if remote_model_key[0] not in real_apps and remote_model_key in concretes:
if remote_model_key[0] not in self.real_apps and remote_model_key in concretes:
remote_model_key = concretes[remote_model_key]
self.relations[remote_model_key][model_key].append((field_name, field))

through = getattr(remote_field, 'through', None)
if not through:
continue
through_model_key = resolve_relation(through, *model_key)
if through_model_key[0] not in real_apps and through_model_key in concretes:
if through_model_key[0] not in self.real_apps and through_model_key in concretes:
through_model_key = concretes[through_model_key]
self.relations[through_model_key][model_key].append((field_name, field))
for model_key in proxies:
Expand Down Expand Up @@ -432,7 +434,7 @@ def from_apps(cls, apps):
return cls(app_models)

def __eq__(self, other):
return self.models == other.models and set(self.real_apps) == set(other.real_apps)
return self.models == other.models and self.real_apps == other.real_apps


class AppConfigStub(AppConfig):
Expand Down
2 changes: 1 addition & 1 deletion tests/auth_tests/test_management.py
Expand Up @@ -1131,7 +1131,7 @@ def test_unavailable_models(self):
with self.assertNumQueries(0):
create_permissions(self.app_config, verbosity=0, apps=state.apps)
# Unavailable auth.Permission
state = migrations.state.ProjectState(real_apps=['contenttypes'])
state = migrations.state.ProjectState(real_apps={'contenttypes'})
with self.assertNumQueries(0):
create_permissions(self.app_config, verbosity=0, apps=state.apps)

Expand Down
4 changes: 2 additions & 2 deletions tests/migrations/test_autodetector.py
Expand Up @@ -2412,7 +2412,7 @@ def test_first_dependency(self):
loader = MigrationLoader(connection)
before = self.make_project_state([])
after = self.make_project_state([self.book_migrations_fk])
after.real_apps = ["migrations"]
after.real_apps = {'migrations'}
autodetector = MigrationAutodetector(before, after)
changes = autodetector._detect_changes(graph=loader.graph)
# Right number/type of migrations?
Expand All @@ -2431,7 +2431,7 @@ def test_last_dependency(self):
loader = MigrationLoader(connection)
before = self.make_project_state([])
after = self.make_project_state([self.book_migrations_fk])
after.real_apps = ["migrations"]
after.real_apps = {'migrations'}
autodetector = MigrationAutodetector(before, after)
changes = autodetector._detect_changes(graph=loader.graph)
# Right number/type of migrations?
Expand Down
2 changes: 1 addition & 1 deletion tests/migrations/test_state.py
Expand Up @@ -916,7 +916,7 @@ class Meta:
project_state.apps

# If we include the real app it should succeed
project_state = ProjectState(real_apps=["contenttypes"])
project_state = ProjectState(real_apps={'contenttypes'})
project_state.add_model(ModelState.from_model(TestModel))
rendered_state = project_state.apps
self.assertEqual(
Expand Down

0 comments on commit 54a30a7

Please sign in to comment.