summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2014-07-11 18:59:21 -0700
committerAndrew Godwin <andrew@aeracode.org>2014-07-11 19:01:47 -0700
commit563046a7de24d5e1eac1d455ec5530d0479186e0 (patch)
tree3e044ed81cf276e02f6e6cb69fbdfbe25199b5b5
parent736e289445aea5083da04ddd8734c3ff20310408 (diff)
[1.7.x] Fixed #22983: Alert when squashing RunPython operations with referred functions.
-rw-r--r--django/core/management/commands/squashmigrations.py6
-rw-r--r--django/db/migrations/writer.py14
2 files changed, 19 insertions, 1 deletions
diff --git a/django/core/management/commands/squashmigrations.py b/django/core/management/commands/squashmigrations.py
index 3101ca0b14..e86000b5ef 100644
--- a/django/core/management/commands/squashmigrations.py
+++ b/django/core/management/commands/squashmigrations.py
@@ -112,3 +112,9 @@ class Command(BaseCommand):
self.stdout.write(" the new migration will be used for new installs. Once you are sure")
self.stdout.write(" all instances of the codebase have applied the migrations you squashed,")
self.stdout.write(" you can delete them.")
+ if writer.needs_manual_porting:
+ self.stdout.write(self.style.MIGRATE_HEADING("Manual porting required"))
+ self.stdout.write(" Your migrations contained functions that must be manually copied over,")
+ self.stdout.write(" as we could not safely copy their implementation.")
+ self.stdout.write(" See the comment at the top of the squashed migration for details.")
+
diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py
index dbeaae184c..593bff902d 100644
--- a/django/db/migrations/writer.py
+++ b/django/db/migrations/writer.py
@@ -111,6 +111,7 @@ class MigrationWriter(object):
def __init__(self, migration):
self.migration = migration
+ self.needs_manual_porting = False
def as_string(self):
"""
@@ -142,9 +143,20 @@ class MigrationWriter(object):
dependencies.append(" %s," % self.serialize(dependency)[0])
items["dependencies"] = "\n".join(dependencies) + "\n" if dependencies else ""
- # Format imports nicely
+ # Format imports nicely, swapping imports of functions from migration files
+ # for comments
+ migration_imports = set()
+ for line in list(imports):
+ if re.match("^import (.*)\.\d+[^\s]*$", line):
+ migration_imports.add(line.split("import")[1].strip())
+ imports.remove(line)
+ self.needs_manual_porting = True
imports.discard("from django.db import models")
items["imports"] = "\n".join(imports) + "\n" if imports else ""
+ 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" % (
+ "\n# ".join(migration_imports)
+ )
# If there's a replaces, make a string for it
if self.migration.replaces: