|
12 | 12 | from django.db.migrations.autodetector import MigrationAutodetector
|
13 | 13 | from django.db.migrations.loader import MigrationLoader
|
14 | 14 | from django.db.migrations.migration import SwappableTuple
|
| 15 | +from django.db.migrations.operations.special import ( |
| 16 | + RunPython, |
| 17 | + RunSQL, |
| 18 | + SeparateDatabaseAndState, |
| 19 | +) |
15 | 20 | from django.db.migrations.optimizer import MigrationOptimizer
|
16 | 21 | from django.db.migrations.questioner import (
|
17 | 22 | InteractiveMigrationQuestioner,
|
@@ -145,6 +150,9 @@ def handle(self, *app_labels, **options):
|
145 | 150 | if has_bad_labels:
|
146 | 151 | sys.exit(2)
|
147 | 152 |
|
| 153 | + loader_from_disk = MigrationLoader(None, ignore_no_migrations=True) |
| 154 | + loader_from_disk.load_disk() |
| 155 | + |
148 | 156 | # Load the current graph state. Pass in None for the connection so
|
149 | 157 | # the loader doesn't try to resolve replaced migrations from DB.
|
150 | 158 | if self.replace_all:
|
@@ -299,8 +307,22 @@ def handle(self, *app_labels, **options):
|
299 | 307 | for dependency in app_migration.dependencies
|
300 | 308 | if dependency not in app_migration.replaces
|
301 | 309 | ]
|
| 310 | + for app_label, name in app_migration.replaces: |
| 311 | + app_migration.operations += [ |
| 312 | + operation |
| 313 | + for operation in loader_from_disk.get_migration( |
| 314 | + app_label, name |
| 315 | + ).operations |
| 316 | + if ( |
| 317 | + isinstance(operation, RunPython) |
| 318 | + or isinstance(operation, RunSQL) |
| 319 | + or isinstance(operation, SeparateDatabaseAndState) |
| 320 | + ) |
| 321 | + ] |
302 | 322 | else:
|
303 | 323 | self.write_migration_files(changes)
|
| 324 | + |
| 325 | + self.write_migration_files(changes) |
304 | 326 | if check_changes:
|
305 | 327 | sys.exit(1)
|
306 | 328 |
|
|
0 commit comments