Debian Codegraph @

django/core/management/commands/squashmigrations.py

raw
1 import os
2 import shutil
3
4 from django.apps import apps
5 from django.conf import settings
6 from django.core.management.base import BaseCommand, CommandError
7 from django.core.management.utils import run_formatters
8 from django.db import DEFAULT_DB_ALIAS, connections, migrations
9 from django.db.migrations.loader import AmbiguityError, MigrationLoader
10 from django.db.migrations.migration import SwappableTuple
11 from django.db.migrations.optimizer import MigrationOptimizer
12 from django.db.migrations.writer import MigrationWriter
13 from django.utils.version import get_docs_version
14
15
16 class Command(BaseCommand):
17 help = (
18 "Squashes an existing set of migrations (from first until specified) into a "
19 "single new one."
20 )
21
22 def add_arguments(self, parser):
23 parser.add_argument(
24 "app_label",
25 help="App label of the application to squash migrations for.",
26 )
27 parser.add_argument(
28 "start_migration_name",
29 nargs="?",
30 help=(
31 "Migrations will be squashed starting from and including this "
32 "migration."
33 ),
34 )
35 parser.add_argument(
36 "migration_name",
37 help="Migrations will be squashed until and including this migration.",
38 )
39 parser.add_argument(
40 "--no-optimize",
41 action="store_true",
42 help="Do not try to optimize the squashed operations.",
43 )
44 parser.add_argument(
45 "--noinput",
46 "--no-input",
47 action="store_false",
48 dest="interactive",
49 help="Tells Django to NOT prompt the user for input of any kind.",
50 )
51 parser.add_argument(
52 "--squashed-name",
53 help="Sets the name of the new squashed migration.",
54 )
55 parser.add_argument(
56 "--no-header",
57 action="store_false",
58 dest="include_header",
59 help="Do not add a header comment to the new squashed migration.",
60 )
61
62 def handle(self, **options):
63 self.verbosity = options["verbosity"]
64 self.interactive = options["interactive"]
65 app_label = options["app_label"]
66 start_migration_name = options["start_migration_name"]
67 migration_name = options["migration_name"]
68 no_optimize = options["no_optimize"]
69 squashed_name = options["squashed_name"]
70 include_header = options["include_header"]
71 # Validate app_label.
72 try:
73 apps.get_app_config(app_label)
74 except LookupError as err:
75 raise CommandError(str(err))
76 # Load the current graph state, check the app and migration they asked
77 # for exists.
78 loader = MigrationLoader(connections[DEFAULT_DB_ALIAS])
79 if app_label not in loader.migrated_apps:
80 raise CommandError(
81 "App '%s' does not have migrations (so squashmigrations on "
82 "it makes no sense)" % app_label
83 )
84
85 migration = self.find_migration(loader, app_label, migration_name)
86
87 # Work out the list of predecessor migrations
88 migrations_to_squash = [
89 loader.get_migration(al, mn)
90 for al, mn in loader.graph.forwards_plan(
91 (migration.app_label, migration.name)
92 )
93 if al == migration.app_label
94 ]
95
96 if start_migration_name:
97 start_migration = self.find_migration(
98 loader, app_label, start_migration_name
99 )
100 start = loader.get_migration(
101 start_migration.app_label, start_migration.name
102 )
103 try:
104 start_index = migrations_to_squash.index(start)
105 migrations_to_squash = migrations_to_squash[start_index:]
106 except ValueError:
107 raise CommandError(
108 "The migration '%s' cannot be found. Maybe it comes after "
109 "the migration '%s'?\n"
110 "Have a look at:\n"
111 " python manage.py showmigrations %s\n"
112 "to debug this issue." % (start_migration, migration, app_label)
113 )
114
115 # Tell them what we're doing and optionally ask if we should proceed
116 if self.verbosity > 0 or self.interactive:
117 self.stdout.write(
118 self.style.MIGRATE_HEADING("Will squash the following migrations:")
119 )
120 for migration in migrations_to_squash:
121 self.stdout.write(" - %s" % migration.name)
122
123 if self.interactive:
124 answer = None
125 while not answer or answer not in "yn":
126 answer = input("Do you wish to proceed? [y/N] ")
127 if not answer:
128 answer = "n"
129 break
130 else:
131 answer = answer[0].lower()
132 if answer != "y":
133 return
134
135 # Load the operations from all those migrations and concat together,
136 # along with collecting external dependencies and detecting
137 # double-squashing
138 operations = []
139 dependencies = set()
140 # We need to take all dependencies from the first migration in the list
141 # as it may be 0002 depending on 0001
142 first_migration = True
143 for smigration in migrations_to_squash:
144 if smigration.replaces:
145 raise CommandError(
146 "You cannot squash squashed migrations! Please transition it to a "
147 "normal migration first: https://docs.djangoproject.com/en/%s/"
148 "topics/migrations/#squashing-migrations" % get_docs_version()
149 )
150 operations.extend(smigration.operations)
151 for dependency in smigration.dependencies:
152 if isinstance(dependency, SwappableTuple):
153 if settings.AUTH_USER_MODEL == dependency.setting:
154 dependencies.add(("__setting__", "AUTH_USER_MODEL"))
155 else:
156 dependencies.add(dependency)
157 elif dependency[0] != smigration.app_label or first_migration:
158 dependencies.add(dependency)
159 first_migration = False
160
161 if no_optimize:
162 if self.verbosity > 0:
163 self.stdout.write(
164 self.style.MIGRATE_HEADING("(Skipping optimization.)")
165 )
166 new_operations = operations
167 else:
168 if self.verbosity > 0:
169 self.stdout.write(self.style.MIGRATE_HEADING("Optimizing..."))
170
171 optimizer = MigrationOptimizer()
172 new_operations = optimizer.optimize(operations, migration.app_label)
173
174 if self.verbosity > 0:
175 if len(new_operations) == len(operations):
176 self.stdout.write(" No optimizations possible.")
177 else:
178 self.stdout.write(
179 " Optimized from %s operations to %s operations."
180 % (len(operations), len(new_operations))
181 )
182
183 # Work out the value of replaces (any squashed ones we're re-squashing)
184 # need to feed their replaces into ours
185 replaces = []
186 for migration in migrations_to_squash:
187 if migration.replaces:
188 replaces.extend(migration.replaces)
189 else:
190 replaces.append((migration.app_label, migration.name))
191
192 # Make a new migration with those operations
193 subclass = type(
194 "Migration",
195 (migrations.Migration,),
196 {
197 "dependencies": dependencies,
198 "operations": new_operations,
199 "replaces": replaces,
200 },
201 )
202 if start_migration_name:
203 if squashed_name:
204 # Use the name from --squashed-name.
205 prefix, _ = start_migration.name.split("_", 1)
206 name = "%s_%s" % (prefix, squashed_name)
207 else:
208 # Generate a name.
209 name = "%s_squashed_%s" % (start_migration.name, migration.name)
210 new_migration = subclass(name, app_label)
211 else:
212 name = "0001_%s" % (squashed_name or "squashed_%s" % migration.name)
213 new_migration = subclass(name, app_label)
214 new_migration.initial = True
215
216 # Write out the new migration file
217 writer = MigrationWriter(new_migration, include_header)
218 if os.path.exists(writer.path):
219 raise CommandError(
220 f"Migration {new_migration.name} already exists. Use a different name."
221 )
222 with open(writer.path, "w", encoding="utf-8") as fh:
223 fh.write(writer.as_string())
224 run_formatters([writer.path], stderr=self.stderr)
225
226 if self.verbosity > 0:
227 self.stdout.write(
228 self.style.MIGRATE_HEADING(
229 "Created new squashed migration %s" % writer.path
230 )
231 + "\n"
232 " You should commit this migration but leave the old ones in place;\n"
233 " the new migration will be used for new installs. Once you are sure\n"
234 " all instances of the codebase have applied the migrations you "
235 "squashed,\n"
236 " you can delete them."
237 )
238 if writer.needs_manual_porting:
239 self.stdout.write(
240 self.style.MIGRATE_HEADING("Manual porting required") + "\n"
241 " Your migrations contained functions that must be manually "
242 "copied over,\n"
243 " as we could not safely copy their implementation.\n"
244 " See the comment at the top of the squashed migration for "
245 "details."
246 )
247 if shutil.which("black"):
248 self.stdout.write(
249 self.style.WARNING(
250 "Squashed migration couldn't be formatted using the "
251 '"black" command. You can call it manually.'
252 )
253 )
254
255 def find_migration(self, loader, app_label, name):
256 try:
257 return loader.get_migration_by_prefix(app_label, name)
258 except AmbiguityError:
259 raise CommandError(
260 "More than one migration matches '%s' in app '%s'. Please be "
261 "more specific." % (name, app_label)
262 )
263 except KeyError:
264 raise CommandError(
265 "Cannot find a migration matching '%s' from app '%s'."
266 % (name, app_label)
267 )