Skip to content

Commit 76770c3

Browse files
authored
Added support to skip pulling remote changes. (#127)
* Added support to skip pulling remote changes. * Fixed lint error. * Fixed test. * Update makemigrations.py * Update makemigrations.py
1 parent 0f6631e commit 76770c3

File tree

3 files changed

+85
-20
lines changed

3 files changed

+85
-20
lines changed

migration_fixer/management/commands/makemigrations.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ def add_arguments(self, parser):
4747
help="The name of the default branch.",
4848
default="main",
4949
)
50+
parser.add_argument(
51+
"-s",
52+
"--skip-default-branch-update",
53+
help="Skip pulling the latest changes from the default branch.",
54+
action="store_true",
55+
)
5056
parser.add_argument(
5157
"-r",
5258
"--remote",
@@ -66,6 +72,7 @@ def handle(self, *app_labels, **options):
6672
self.merge = options["merge"]
6773
self.fix = options["fix"]
6874
self.force_update = options["force_update"]
75+
self.skip_default_branch_update = options["skip_default_branch_update"]
6976
self.default_branch = options["default_branch"]
7077
self.remote = options["remote"]
7178

@@ -107,23 +114,24 @@ def handle(self, *app_labels, **options):
107114
)
108115
)
109116

110-
if self.verbosity >= 2:
111-
self.stdout.write(
112-
f"Fetching git remote origin changes on: {self.default_branch}"
113-
)
117+
if not self.skip_default_branch_update:
118+
if self.verbosity >= 2:
119+
self.stdout.write(
120+
f"Fetching git remote {self.remote} changes on: {self.default_branch}"
121+
)
114122

115-
if current_branch == self.default_branch: # pragma: no cover
116-
remote = self.repo.remotes[self.remote]
117-
remote.pull(
118-
self.default_branch,
119-
force=self.force_update,
120-
)
121-
else:
122-
remote = self.repo.remotes[self.remote]
123-
remote.fetch(
124-
f"{self.default_branch}:{self.default_branch}",
125-
force=self.force_update,
126-
)
123+
if current_branch == self.default_branch: # pragma: no cover
124+
remote = self.repo.remotes[self.remote]
125+
remote.pull(
126+
self.default_branch,
127+
force=self.force_update,
128+
)
129+
else:
130+
remote = self.repo.remotes[self.remote]
131+
remote.fetch(
132+
f"{self.default_branch}:{self.default_branch}",
133+
force=self.force_update,
134+
)
127135

128136
if self.verbosity >= 2:
129137
self.stdout.write(
@@ -168,7 +176,7 @@ def handle(self, *app_labels, **options):
168176
}
169177

170178
for app_label in conflicts:
171-
conflict = conflicts.get(app_label)
179+
conflict = conflicts[app_label]
172180
migration_module, _ = loader.migrations_module(app_label)
173181
migration_absolute_path = os.path.join(
174182
*migration_module.split(".")
@@ -269,7 +277,7 @@ def handle(self, *app_labels, **options):
269277
start_name=last_remote_filename,
270278
changed_files=changed_files,
271279
writer=(
272-
lambda message: self.stdout.write(message)
280+
lambda m: self.stdout.write(m)
273281
if self.verbosity >= 2
274282
else lambda x: x
275283
),

migration_fixer/tests/management/commands/test_makemigrations.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,61 @@ def test_run_makemigrations_with_fix_is_valid_for_conflicts_verbose(git_repo):
114114
assert output == expected_output
115115

116116

117+
@pytest.mark.env("test_02")
118+
@pytest.mark.django_db
119+
def test_run_makemigrations_with_fix_and_skip_update_is_valid_for_conflicts(git_repo):
120+
cmd = Command(repo=git_repo.api)
121+
122+
with temporary_checkout(
123+
git_repo,
124+
default_branch_name=TEST_01_MIGRATION_BRANCH,
125+
target_branch_name=TEST_02_MIGRATION_BRANCH,
126+
) as target_branch:
127+
output = execute_command(
128+
cmd,
129+
default_branch=TEST_01_MIGRATION_BRANCH,
130+
fix=True,
131+
skip_default_branch_update=True,
132+
)
133+
134+
assert target_branch.name == TEST_02_MIGRATION_BRANCH
135+
assert output == f"{cmd.success_msg}\n"
136+
137+
138+
@pytest.mark.env("test_02")
139+
@pytest.mark.django_db
140+
def test_run_makemigrations_with_fix_and_skip_update_is_valid_for_conflicts_verbose(
141+
git_repo,
142+
):
143+
cmd = Command(repo=git_repo.api)
144+
expected_output = f"""Verifying git repository...
145+
Retrieving the current branch...
146+
Retrieving the last commit sha on: {TEST_01_MIGRATION_BRANCH}
147+
Retrieving changed files between the current branch and {TEST_01_MIGRATION_BRANCH}
148+
Retrieving the last migration on: {TEST_01_MIGRATION_BRANCH}
149+
Fixing numbered migration...
150+
Updating migration "0002_alter_testmodel_active.py" dependency to 0002_alter_testmodel_age
151+
Renaming migration "0002_alter_testmodel_active.py" to "0003_alter_testmodel_active.py"
152+
Successfully fixed migrations.
153+
"""
154+
155+
with temporary_checkout(
156+
git_repo,
157+
default_branch_name=TEST_01_MIGRATION_BRANCH,
158+
target_branch_name=TEST_02_MIGRATION_BRANCH,
159+
) as target_branch:
160+
output = execute_command(
161+
cmd,
162+
default_branch=TEST_01_MIGRATION_BRANCH,
163+
verbosity=2,
164+
fix=True,
165+
skip_default_branch_update=True,
166+
)
167+
168+
assert target_branch.name == TEST_02_MIGRATION_BRANCH
169+
assert output == expected_output
170+
171+
117172
@pytest.mark.env("test_03")
118173
@pytest.mark.django_db
119174
def test_run_makemigrations_with_fix_is_valid_for_multiple_file_conflicts(git_repo):

migration_fixer/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import re
33
from itertools import count
44
from pathlib import Path
5-
from typing import Callable, List
5+
from typing import Callable, List, Optional
6+
7+
from django.db.migrations.graph import MigrationGraph
68

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

127129

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

0 commit comments

Comments
 (0)