Skip to content

Commit d17bb32

Browse files
Fix bug editing previous migration files. (#120)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e73634a commit d17bb32

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

migration_fixer/management/commands/makemigrations.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
get_filename,
2020
migration_sorter,
2121
no_translations,
22+
sibling_nodes,
2223
)
2324

2425

@@ -161,9 +162,10 @@ def handle(self, *app_labels, **options):
161162
):
162163
loader.check_consistent_history(connection)
163164

164-
# Before anything else, see if there's conflicting apps and drop out
165-
# hard if there are any and they don't want to merge
166-
conflicts = loader.detect_conflicts()
165+
conflicts = {
166+
app_name: sibling_nodes(loader.graph, app_name)
167+
for app_name in loader.detect_conflicts()
168+
}
167169

168170
for app_label in conflicts:
169171
conflict = conflicts.get(app_label)
@@ -213,8 +215,8 @@ def handle(self, *app_labels, **options):
213215
path
214216
for path in sorted_changed_files
215217
if (
216-
int(conflict_base.split("_")[0])
217-
>= int(get_filename(path).split("_")[0])
218+
int(get_filename(path).split("_")[0])
219+
>= int(conflict_base.split("_")[0])
218220
)
219221
]
220222

migration_fixer/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,20 @@ def wrapped(*args, **kwargs):
123123
def get_filename(path: str) -> str:
124124
"""Return the file name from a path."""
125125
return os.path.splitext(os.path.basename(path))[0]
126+
127+
128+
def sibling_nodes(graph, app_name=None):
129+
"""
130+
Return all sibling nodes that have the same parent
131+
- it's usually the result of a VCS merge and needs some user input.
132+
"""
133+
siblings = set()
134+
135+
for node in graph.nodes:
136+
if len(graph.node_map[node].children) > 1 and (
137+
not app_name or app_name == node[0]
138+
):
139+
for child in graph.node_map[node].children:
140+
siblings.add(child[-1])
141+
142+
return sorted(siblings)

0 commit comments

Comments
 (0)