Skip to content

Commit

Permalink
Fix bug editing previous migration files. (#120)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
jackton1 and pre-commit-ci[bot] committed Aug 8, 2021
1 parent e73634a commit d17bb32
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
12 changes: 7 additions & 5 deletions migration_fixer/management/commands/makemigrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
get_filename,
migration_sorter,
no_translations,
sibling_nodes,
)


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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])
)
]

Expand Down
17 changes: 17 additions & 0 deletions migration_fixer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit d17bb32

Please sign in to comment.