Skip to content

Commit

Permalink
Fixed #21968: Bad detection of old-style apps to add initial migration
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgodwin committed Feb 9, 2014
1 parent 75a96f0 commit 2085f53
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
2 changes: 2 additions & 0 deletions django/core/management/commands/makemigrations.py
Expand Up @@ -47,7 +47,9 @@ def handle(self, *app_labels, **options):


# Load the current graph state. Takes a connection, but it's not used # Load the current graph state. Takes a connection, but it's not used
# (makemigrations doesn't look at the database state). # (makemigrations doesn't look at the database state).
# Also make sure the graph is built without unmigrated apps shoehorned in.
loader = MigrationLoader(connections[DEFAULT_DB_ALIAS]) loader = MigrationLoader(connections[DEFAULT_DB_ALIAS])
loader.build_graph(ignore_unmigrated=True)


# Before anything else, see if there's conflicting apps and drop out # Before anything else, see if there's conflicting apps and drop out
# hard if there are any and they don't want to merge # hard if there are any and they don't want to merge
Expand Down
51 changes: 28 additions & 23 deletions django/db/migrations/loader.py
Expand Up @@ -135,7 +135,7 @@ def get_migration_by_prefix(self, app_label, name_prefix):
else: else:
return self.disk_migrations[results[0]] return self.disk_migrations[results[0]]


def build_graph(self): def build_graph(self, ignore_unmigrated=False):
""" """
Builds a migration dependency graph using both the disk and database. Builds a migration dependency graph using both the disk and database.
You'll need to rebuild the graph if you apply migrations. This isn't You'll need to rebuild the graph if you apply migrations. This isn't
Expand Down Expand Up @@ -200,33 +200,38 @@ def build_graph(self):
# even have migrations. # even have migrations.
if parent[1] == "__first__" and parent not in self.graph: if parent[1] == "__first__" and parent not in self.graph:
if parent[0] in self.unmigrated_apps: if parent[0] in self.unmigrated_apps:
# This app isn't migrated, but something depends on it. if ignore_unmigrated:
# We'll add a fake initial migration for it into the migration.dependencies.remove(parent)
# graph. parent = None
app_config = apps.get_app_config(parent[0]) else:
ops = [] # This app isn't migrated, but something depends on it.
for model in app_config.get_models(): # We'll add a fake initial migration for it into the
model_state = ModelState.from_model(model) # graph.
ops.append( app_config = apps.get_app_config(parent[0])
operations.CreateModel( ops = []
name=model_state.name, for model in app_config.get_models():
fields=model_state.fields, model_state = ModelState.from_model(model)
options=model_state.options, ops.append(
bases=model_state.bases, operations.CreateModel(
name=model_state.name,
fields=model_state.fields,
options=model_state.options,
bases=model_state.bases,
)
) )
) new_migration = type(
new_migration = type( "FakeInitialMigration",
"FakeInitialMigration", (Migration, ),
(Migration, ), {"operations": ops},
{"operations": ops}, )(parent[1], parent[0])
)(parent[1], parent[0]) self.graph.add_node(parent, new_migration)
self.graph.add_node(parent, new_migration) self.applied_migrations.add(parent)
self.applied_migrations.add(parent)
elif parent[0] in self.migrated_apps: elif parent[0] in self.migrated_apps:
parent = list(self.graph.root_nodes(parent[0]))[0] parent = list(self.graph.root_nodes(parent[0]))[0]
else: else:
raise ValueError("Dependency on unknown app %s" % parent[0]) raise ValueError("Dependency on unknown app %s" % parent[0])
self.graph.add_dependency(key, parent) if parent is not None:
self.graph.add_dependency(key, parent)


def detect_conflicts(self): def detect_conflicts(self):
""" """
Expand Down

0 comments on commit 2085f53

Please sign in to comment.