Skip to content

Commit 0a311b5

Browse files
committed
feat: add special operations in squashed files
1 parent 788930b commit 0a311b5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

django_replace_migrations/management/commands/makemigrations.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
from django.db.migrations.autodetector import MigrationAutodetector
1313
from django.db.migrations.loader import MigrationLoader
1414
from django.db.migrations.migration import SwappableTuple
15+
from django.db.migrations.operations.special import (
16+
RunPython,
17+
RunSQL,
18+
SeparateDatabaseAndState,
19+
)
1520
from django.db.migrations.optimizer import MigrationOptimizer
1621
from django.db.migrations.questioner import (
1722
InteractiveMigrationQuestioner,
@@ -145,6 +150,9 @@ def handle(self, *app_labels, **options):
145150
if has_bad_labels:
146151
sys.exit(2)
147152

153+
loader_from_disk = MigrationLoader(None, ignore_no_migrations=True)
154+
loader_from_disk.load_disk()
155+
148156
# Load the current graph state. Pass in None for the connection so
149157
# the loader doesn't try to resolve replaced migrations from DB.
150158
if self.replace_all:
@@ -299,8 +307,22 @@ def handle(self, *app_labels, **options):
299307
for dependency in app_migration.dependencies
300308
if dependency not in app_migration.replaces
301309
]
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+
]
302322
else:
303323
self.write_migration_files(changes)
324+
325+
self.write_migration_files(changes)
304326
if check_changes:
305327
sys.exit(1)
306328

0 commit comments

Comments
 (0)