From d17bb3210ae446cbceb982f6f17c5d60ba033b62 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 8 Aug 2021 07:38:40 -0400 Subject: [PATCH] Fix bug editing previous migration files. (#120) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .../management/commands/makemigrations.py | 12 +++++++----- migration_fixer/utils.py | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/migration_fixer/management/commands/makemigrations.py b/migration_fixer/management/commands/makemigrations.py index c7745ff5..31cdb465 100644 --- a/migration_fixer/management/commands/makemigrations.py +++ b/migration_fixer/management/commands/makemigrations.py @@ -19,6 +19,7 @@ get_filename, migration_sorter, no_translations, + sibling_nodes, ) @@ -161,9 +162,10 @@ def handle(self, *app_labels, **options): ): loader.check_consistent_history(connection) - # Before anything else, see if there's conflicting apps and drop out - # hard if there are any and they don't want to merge - conflicts = loader.detect_conflicts() + conflicts = { + app_name: sibling_nodes(loader.graph, app_name) + for app_name in loader.detect_conflicts() + } for app_label in conflicts: conflict = conflicts.get(app_label) @@ -213,8 +215,8 @@ def handle(self, *app_labels, **options): path for path in sorted_changed_files if ( - int(conflict_base.split("_")[0]) - >= int(get_filename(path).split("_")[0]) + int(get_filename(path).split("_")[0]) + >= int(conflict_base.split("_")[0]) ) ] diff --git a/migration_fixer/utils.py b/migration_fixer/utils.py index cb00ede4..b4cc560b 100644 --- a/migration_fixer/utils.py +++ b/migration_fixer/utils.py @@ -123,3 +123,20 @@ def wrapped(*args, **kwargs): def get_filename(path: str) -> str: """Return the file name from a path.""" return os.path.splitext(os.path.basename(path))[0] + + +def sibling_nodes(graph, app_name=None): + """ + Return all sibling nodes that have the same parent + - it's usually the result of a VCS merge and needs some user input. + """ + siblings = set() + + for node in graph.nodes: + if len(graph.node_map[node].children) > 1 and ( + not app_name or app_name == node[0] + ): + for child in graph.node_map[node].children: + siblings.add(child[-1]) + + return sorted(siblings)