Limited lines to 119 characters in django/{contrib,db}.

Refs #23395.
This commit is contained in:
Berker Peksag 2014-10-25 07:42:44 +03:00 committed by Tim Graham
parent 349471eeb9
commit c9178ef17a
10 changed files with 69 additions and 30 deletions

View File

@ -55,7 +55,10 @@ class ContentTypeManager(models.Manager):
# It's possible to migrate a single app before contenttypes, # It's possible to migrate a single app before contenttypes,
# as it's not a required initial dependency (it's contrib!) # as it's not a required initial dependency (it's contrib!)
# Have a nice error for this. # Have a nice error for this.
raise RuntimeError("Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually.") raise RuntimeError(
"Error creating new content types. Please make sure contenttypes "
"is migrated before trying to migrate apps individually."
)
except self.model.DoesNotExist: except self.model.DoesNotExist:
# Not found in the database; we proceed to create it. This time we # Not found in the database; we proceed to create it. This time we
# use get_or_create to take care of any race conditions. # use get_or_create to take care of any race conditions.

View File

@ -162,7 +162,8 @@ class MigrationAutodetector(object):
old_model_state = self.from_state.models[app_label, old_model_name] old_model_state = self.from_state.models[app_label, old_model_name]
for field_name, field in old_model_state.fields: for field_name, field in old_model_state.fields:
old_field = self.old_apps.get_model(app_label, old_model_name)._meta.get_field_by_name(field_name)[0] old_field = self.old_apps.get_model(app_label, old_model_name)._meta.get_field_by_name(field_name)[0]
if hasattr(old_field, "rel") and getattr(old_field.rel, "through", None) and not old_field.rel.through._meta.auto_created: if (hasattr(old_field, "rel") and getattr(old_field.rel, "through", None)
and not old_field.rel.through._meta.auto_created):
through_key = ( through_key = (
old_field.rel.through._meta.app_label, old_field.rel.through._meta.app_label,
old_field.rel.through._meta.object_name.lower(), old_field.rel.through._meta.object_name.lower(),
@ -961,7 +962,8 @@ class MigrationAutodetector(object):
old_model_name = self.renamed_models.get((app_label, model_name), model_name) old_model_name = self.renamed_models.get((app_label, model_name), model_name)
old_model_state = self.from_state.models[app_label, old_model_name] old_model_state = self.from_state.models[app_label, old_model_name]
new_model_state = self.to_state.models[app_label, model_name] new_model_state = self.to_state.models[app_label, model_name]
if old_model_state.options.get("order_with_respect_to", None) != new_model_state.options.get("order_with_respect_to", None): if (old_model_state.options.get("order_with_respect_to", None) !=
new_model_state.options.get("order_with_respect_to", None)):
# Make sure it comes second if we're adding # Make sure it comes second if we're adding
# (removal dependency is part of RemoveField) # (removal dependency is part of RemoveField)
dependencies = [] dependencies = []

View File

@ -37,9 +37,13 @@ class MigrationGraph(object):
def add_dependency(self, migration, child, parent): def add_dependency(self, migration, child, parent):
if child not in self.nodes: if child not in self.nodes:
raise KeyError("Migration %s dependencies reference nonexistent child node %r" % (migration, child)) raise KeyError(
"Migration %s dependencies reference nonexistent child node %r" % (migration, child)
)
if parent not in self.nodes: if parent not in self.nodes:
raise KeyError("Migration %s dependencies reference nonexistent parent node %r" % (migration, parent)) raise KeyError(
"Migration %s dependencies reference nonexistent parent node %r" % (migration, parent)
)
self.dependencies.setdefault(child, set()).add(parent) self.dependencies.setdefault(child, set()).add(parent)
self.dependents.setdefault(parent, set()).add(child) self.dependents.setdefault(parent, set()).add(child)
@ -72,7 +76,8 @@ class MigrationGraph(object):
""" """
roots = set() roots = set()
for node in self.nodes: for node in self.nodes:
if not any(key[0] == node[0] for key in self.dependencies.get(node, set())) and (not app or app == node[0]): if (not any(key[0] == node[0] for key in self.dependencies.get(node, set()))
and (not app or app == node[0])):
roots.add(node) roots.add(node)
return sorted(roots) return sorted(roots)
@ -86,7 +91,8 @@ class MigrationGraph(object):
""" """
leaves = set() leaves = set()
for node in self.nodes: for node in self.nodes:
if not any(key[0] == node[0] for key in self.dependents.get(node, set())) and (not app or app == node[0]): if (not any(key[0] == node[0] for key in self.dependents.get(node, set()))
and (not app or app == node[0])):
leaves.add(node) leaves.add(node)
return sorted(leaves) return sorted(leaves)
@ -116,7 +122,10 @@ class MigrationGraph(object):
return list(OrderedSet(visited)) return list(OrderedSet(visited))
def __str__(self): def __str__(self):
return "Graph: %s nodes, %s edges" % (len(self.nodes), sum(len(x) for x in self.dependencies.values())) return "Graph: %s nodes, %s edges" % (
len(self.nodes),
sum(len(x) for x in self.dependencies.values()),
)
def make_state(self, nodes=None, at_end=True, real_apps=None): def make_state(self, nodes=None, at_end=True, real_apps=None):
""" """

View File

@ -106,7 +106,9 @@ class MigrationLoader(object):
break break
raise raise
if not hasattr(migration_module, "Migration"): if not hasattr(migration_module, "Migration"):
raise BadMigrationError("Migration %s in app %s has no Migration class" % (migration_name, app_config.label)) raise BadMigrationError(
"Migration %s in app %s has no Migration class" % (migration_name, app_config.label)
)
# Ignore South-style migrations # Ignore South-style migrations
if hasattr(migration_module.Migration, "forwards"): if hasattr(migration_module.Migration, "forwards"):
south_style_migrations = True south_style_migrations = True
@ -127,7 +129,9 @@ class MigrationLoader(object):
if l == app_label and n.startswith(name_prefix): if l == app_label and n.startswith(name_prefix):
results.append((l, n)) results.append((l, n))
if len(results) > 1: if len(results) > 1:
raise AmbiguityError("There is more than one migration for '%s' with the prefix '%s'" % (app_label, name_prefix)) raise AmbiguityError(
"There is more than one migration for '%s' with the prefix '%s'" % (app_label, name_prefix)
)
elif len(results) == 0: elif len(results) == 0:
raise KeyError("There no migrations for '%s' with the prefix '%s'" % (app_label, name_prefix)) raise KeyError("There no migrations for '%s' with the prefix '%s'" % (app_label, name_prefix))
else: else:

View File

@ -90,7 +90,8 @@ class Migration(object):
# there instead # there instead
if collect_sql and not operation.reduces_to_sql: if collect_sql and not operation.reduces_to_sql:
schema_editor.collected_sql.append("--") schema_editor.collected_sql.append("--")
schema_editor.collected_sql.append("-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE WRITTEN AS SQL:") schema_editor.collected_sql.append("-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE "
"WRITTEN AS SQL:")
schema_editor.collected_sql.append("-- %s" % operation.describe()) schema_editor.collected_sql.append("-- %s" % operation.describe())
schema_editor.collected_sql.append("--") schema_editor.collected_sql.append("--")
continue continue
@ -122,7 +123,8 @@ class Migration(object):
# there instead # there instead
if collect_sql and not operation.reduces_to_sql: if collect_sql and not operation.reduces_to_sql:
schema_editor.collected_sql.append("--") schema_editor.collected_sql.append("--")
schema_editor.collected_sql.append("-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE WRITTEN AS SQL:") schema_editor.collected_sql.append("-- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE "
"WRITTEN AS SQL:")
schema_editor.collected_sql.append("-- %s" % operation.describe()) schema_editor.collected_sql.append("-- %s" % operation.describe())
schema_editor.collected_sql.append("--") schema_editor.collected_sql.append("--")
continue continue

View File

@ -174,7 +174,8 @@ class RenameField(Operation):
def state_forwards(self, app_label, state): def state_forwards(self, app_label, state):
# Rename the field # Rename the field
state.models[app_label, self.model_name.lower()].fields = [ state.models[app_label, self.model_name.lower()].fields = [
(self.new_name if n == self.old_name else n, f) for n, f in state.models[app_label, self.model_name.lower()].fields (self.new_name if n == self.old_name else n, f)
for n, f in state.models[app_label, self.model_name.lower()].fields
] ]
# Fix unique_together to refer to the new field # Fix unique_together to refer to the new field
options = state.models[app_label, self.model_name.lower()].options options = state.models[app_label, self.model_name.lower()].options

View File

@ -300,7 +300,8 @@ class MigrationOptimizer(object):
return [other] return [other]
def reduce_add_field_rename_field(self, operation, other, in_between): def reduce_add_field_rename_field(self, operation, other, in_between):
if operation.model_name.lower() == other.model_name.lower() and operation.name.lower() == other.old_name.lower(): if (operation.model_name.lower() == other.model_name.lower() and
operation.name.lower() == other.old_name.lower()):
return [ return [
migrations.AddField( migrations.AddField(
model_name=operation.model_name, model_name=operation.model_name,
@ -310,7 +311,8 @@ class MigrationOptimizer(object):
] ]
def reduce_alter_field_rename_field(self, operation, other, in_between): def reduce_alter_field_rename_field(self, operation, other, in_between):
if operation.model_name.lower() == other.model_name.lower() and operation.name.lower() == other.old_name.lower(): if (operation.model_name.lower() == other.model_name.lower() and
operation.name.lower() == other.old_name.lower()):
return [ return [
other, other,
migrations.AlterField( migrations.AlterField(
@ -321,7 +323,8 @@ class MigrationOptimizer(object):
] ]
def reduce_rename_field_self(self, operation, other, in_between): def reduce_rename_field_self(self, operation, other, in_between):
if operation.model_name.lower() == other.model_name.lower() and operation.new_name.lower() == other.old_name.lower(): if (operation.model_name.lower() == other.model_name.lower() and
operation.new_name.lower() == other.old_name.lower()):
return [ return [
migrations.RenameField( migrations.RenameField(
operation.model_name, operation.model_name,

View File

@ -163,11 +163,15 @@ class InteractiveMigrationQuestioner(MigrationQuestioner):
def ask_rename(self, model_name, old_name, new_name, field_instance): def ask_rename(self, model_name, old_name, new_name, field_instance):
"Was this field really renamed?" "Was this field really renamed?"
return self._boolean_input("Did you rename %s.%s to %s.%s (a %s)? [y/N]" % (model_name, old_name, model_name, new_name, field_instance.__class__.__name__), False) msg = "Did you rename %s.%s to %s.%s (a %s)? [y/N]"
return self._boolean_input(msg % (model_name, old_name, model_name, new_name,
field_instance.__class__.__name__), False)
def ask_rename_model(self, old_model_state, new_model_state): def ask_rename_model(self, old_model_state, new_model_state):
"Was this model really renamed?" "Was this model really renamed?"
return self._boolean_input("Did you rename the %s.%s model to %s? [y/N]" % (old_model_state.app_label, old_model_state.name, new_model_state.name), False) msg = "Did you rename the %s.%s model to %s? [y/N]"
return self._boolean_input(msg % (old_model_state.app_label, old_model_state.name,
new_model_state.name), False)
def ask_merge(self, app_label): def ask_merge(self, app_label):
return self._boolean_input( return self._boolean_input(

View File

@ -68,7 +68,12 @@ class ProjectState(object):
except InvalidBasesError: except InvalidBasesError:
new_unrendered_models.append(model) new_unrendered_models.append(model)
if len(new_unrendered_models) == len(unrendered_models): if len(new_unrendered_models) == len(unrendered_models):
raise InvalidBasesError("Cannot resolve bases for %r\nThis can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)\n in an app with no migrations; see https://docs.djangoproject.com/en/1.7/topics/migrations/#dependencies for more" % new_unrendered_models) raise InvalidBasesError(
"Cannot resolve bases for %r\nThis can happen if you are inheriting models from an "
"app with migrations (e.g. contrib.auth)\n in an app with no migrations; see "
"https://docs.djangoproject.com/en/1.7/topics/migrations/#dependencies "
"for more" % new_unrendered_models
)
unrendered_models = new_unrendered_models unrendered_models = new_unrendered_models
# make sure apps has no dangling references # make sure apps has no dangling references
if self.apps._pending_lookups: if self.apps._pending_lookups:
@ -78,16 +83,15 @@ class ProjectState(object):
try: try:
model = self.apps.get_model(lookup_model[0], lookup_model[1]) model = self.apps.get_model(lookup_model[0], lookup_model[1])
except LookupError: except LookupError:
if "%s.%s" % (lookup_model[0], lookup_model[1]) == settings.AUTH_USER_MODEL and ignore_swappable: app_label = "%s.%s" % (lookup_model[0], lookup_model[1])
if app_label == settings.AUTH_USER_MODEL and ignore_swappable:
continue continue
# Raise an error with a best-effort helpful message # Raise an error with a best-effort helpful message
# (only for the first issue). Error message should look like: # (only for the first issue). Error message should look like:
# "ValueError: Lookup failed for model referenced by # "ValueError: Lookup failed for model referenced by
# field migrations.Book.author: migrations.Author" # field migrations.Book.author: migrations.Author"
raise ValueError("Lookup failed for model referenced by field {field}: {model[0]}.{model[1]}".format( msg = "Lookup failed for model referenced by field {field}: {model[0]}.{model[1]}"
field=operations[0][1], raise ValueError(msg.format(field=operations[0][1], model=lookup_model))
model=lookup_model,
))
else: else:
do_pending_lookups(model) do_pending_lookups(model)
try: try:
@ -329,7 +333,8 @@ class ModelState(object):
(self.app_label == other.app_label) and (self.app_label == other.app_label) and
(self.name == other.name) and (self.name == other.name) and
(len(self.fields) == len(other.fields)) and (len(self.fields) == len(other.fields)) and
all((k1 == k2 and (f1.deconstruct()[1:] == f2.deconstruct()[1:])) for (k1, f1), (k2, f2) in zip(self.fields, other.fields)) and all((k1 == k2 and (f1.deconstruct()[1:] == f2.deconstruct()[1:]))
for (k1, f1), (k2, f2) in zip(self.fields, other.fields)) and
(self.options == other.options) and (self.options == other.options) and
(self.bases == other.bases) (self.bases == other.bases)
) )

View File

@ -155,10 +155,12 @@ class MigrationWriter(object):
imports.discard("from django.db import models") imports.discard("from django.db import models")
items["imports"] = "\n".join(imports) + "\n" if imports else "" items["imports"] = "\n".join(imports) + "\n" if imports else ""
if migration_imports: if migration_imports:
items["imports"] += "\n\n# Functions from the following migrations need manual copying.\n# Move them and any dependencies into this file, then update the\n# RunPython operations to refer to the local versions:\n# %s" % ( items["imports"] += (
"\n# ".join(migration_imports) "\n\n# Functions from the following migrations need manual "
) "copying.\n# Move them and any dependencies into this file, "
"then update the\n# RunPython operations to refer to the local "
"versions:\n# %s"
) % "\n# ".join(migration_imports)
# If there's a replaces, make a string for it # If there's a replaces, make a string for it
if self.migration.replaces: if self.migration.replaces:
items['replaces_str'] = "\n replaces = %s\n" % self.serialize(self.migration.replaces)[0] items['replaces_str'] = "\n replaces = %s\n" % self.serialize(self.migration.replaces)[0]
@ -396,7 +398,11 @@ class MigrationWriter(object):
return "re.compile(%s)" % ', '.join(args), imports return "re.compile(%s)" % ', '.join(args), imports
# Uh oh. # Uh oh.
else: else:
raise ValueError("Cannot serialize: %r\nThere are some values Django cannot serialize into migration files.\nFor more, see https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing" % value) raise ValueError(
"Cannot serialize: %r\nThere are some values Django cannot serialize into "
"migration files.\nFor more, see https://docs.djangoproject.com/en/dev/"
"topics/migrations/#migration-serializing" % value
)
MIGRATION_TEMPLATE = """\ MIGRATION_TEMPLATE = """\