Skip to content

Commit

Permalink
Added support to skip pulling remote changes. (#127)
Browse files Browse the repository at this point in the history
* Added support to skip pulling remote changes.

* Fixed lint error.

* Fixed test.

* Update makemigrations.py

* Update makemigrations.py
  • Loading branch information
jackton1 committed Aug 22, 2021
1 parent 0f6631e commit 76770c3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 20 deletions.
44 changes: 26 additions & 18 deletions migration_fixer/management/commands/makemigrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def add_arguments(self, parser):
help="The name of the default branch.",
default="main",
)
parser.add_argument(
"-s",
"--skip-default-branch-update",
help="Skip pulling the latest changes from the default branch.",
action="store_true",
)
parser.add_argument(
"-r",
"--remote",
Expand All @@ -66,6 +72,7 @@ def handle(self, *app_labels, **options):
self.merge = options["merge"]
self.fix = options["fix"]
self.force_update = options["force_update"]
self.skip_default_branch_update = options["skip_default_branch_update"]
self.default_branch = options["default_branch"]
self.remote = options["remote"]

Expand Down Expand Up @@ -107,23 +114,24 @@ def handle(self, *app_labels, **options):
)
)

if self.verbosity >= 2:
self.stdout.write(
f"Fetching git remote origin changes on: {self.default_branch}"
)
if not self.skip_default_branch_update:
if self.verbosity >= 2:
self.stdout.write(
f"Fetching git remote {self.remote} changes on: {self.default_branch}"
)

if current_branch == self.default_branch: # pragma: no cover
remote = self.repo.remotes[self.remote]
remote.pull(
self.default_branch,
force=self.force_update,
)
else:
remote = self.repo.remotes[self.remote]
remote.fetch(
f"{self.default_branch}:{self.default_branch}",
force=self.force_update,
)
if current_branch == self.default_branch: # pragma: no cover
remote = self.repo.remotes[self.remote]
remote.pull(
self.default_branch,
force=self.force_update,
)
else:
remote = self.repo.remotes[self.remote]
remote.fetch(
f"{self.default_branch}:{self.default_branch}",
force=self.force_update,
)

if self.verbosity >= 2:
self.stdout.write(
Expand Down Expand Up @@ -168,7 +176,7 @@ def handle(self, *app_labels, **options):
}

for app_label in conflicts:
conflict = conflicts.get(app_label)
conflict = conflicts[app_label]
migration_module, _ = loader.migrations_module(app_label)
migration_absolute_path = os.path.join(
*migration_module.split(".")
Expand Down Expand Up @@ -269,7 +277,7 @@ def handle(self, *app_labels, **options):
start_name=last_remote_filename,
changed_files=changed_files,
writer=(
lambda message: self.stdout.write(message)
lambda m: self.stdout.write(m)
if self.verbosity >= 2
else lambda x: x
),
Expand Down
55 changes: 55 additions & 0 deletions migration_fixer/tests/management/commands/test_makemigrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,61 @@ def test_run_makemigrations_with_fix_is_valid_for_conflicts_verbose(git_repo):
assert output == expected_output


@pytest.mark.env("test_02")
@pytest.mark.django_db
def test_run_makemigrations_with_fix_and_skip_update_is_valid_for_conflicts(git_repo):
cmd = Command(repo=git_repo.api)

with temporary_checkout(
git_repo,
default_branch_name=TEST_01_MIGRATION_BRANCH,
target_branch_name=TEST_02_MIGRATION_BRANCH,
) as target_branch:
output = execute_command(
cmd,
default_branch=TEST_01_MIGRATION_BRANCH,
fix=True,
skip_default_branch_update=True,
)

assert target_branch.name == TEST_02_MIGRATION_BRANCH
assert output == f"{cmd.success_msg}\n"


@pytest.mark.env("test_02")
@pytest.mark.django_db
def test_run_makemigrations_with_fix_and_skip_update_is_valid_for_conflicts_verbose(
git_repo,
):
cmd = Command(repo=git_repo.api)
expected_output = f"""Verifying git repository...
Retrieving the current branch...
Retrieving the last commit sha on: {TEST_01_MIGRATION_BRANCH}
Retrieving changed files between the current branch and {TEST_01_MIGRATION_BRANCH}
Retrieving the last migration on: {TEST_01_MIGRATION_BRANCH}
Fixing numbered migration...
Updating migration "0002_alter_testmodel_active.py" dependency to 0002_alter_testmodel_age
Renaming migration "0002_alter_testmodel_active.py" to "0003_alter_testmodel_active.py"
Successfully fixed migrations.
"""

with temporary_checkout(
git_repo,
default_branch_name=TEST_01_MIGRATION_BRANCH,
target_branch_name=TEST_02_MIGRATION_BRANCH,
) as target_branch:
output = execute_command(
cmd,
default_branch=TEST_01_MIGRATION_BRANCH,
verbosity=2,
fix=True,
skip_default_branch_update=True,
)

assert target_branch.name == TEST_02_MIGRATION_BRANCH
assert output == expected_output


@pytest.mark.env("test_03")
@pytest.mark.django_db
def test_run_makemigrations_with_fix_is_valid_for_multiple_file_conflicts(git_repo):
Expand Down
6 changes: 4 additions & 2 deletions migration_fixer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import re
from itertools import count
from pathlib import Path
from typing import Callable, List
from typing import Callable, List, Optional

from django.db.migrations.graph import MigrationGraph

DEFAULT_TIMEOUT = 120
MIGRATION_REGEX = "\\((?P<comma>['\"]){app_label}(['\"]),\\s(['\"])(?P<conflict_migration>.*)(['\"])\\),"
Expand Down Expand Up @@ -125,7 +127,7 @@ def get_filename(path: str) -> str:
return os.path.splitext(os.path.basename(path))[0]


def sibling_nodes(graph, app_name=None):
def sibling_nodes(graph: MigrationGraph, app_name: Optional[str] = None) -> List[str]:
"""
Return all sibling nodes that have the same parent
- it's usually the result of a VCS merge and needs some user input.
Expand Down

0 comments on commit 76770c3

Please sign in to comment.