Skip to content

Commit

Permalink
feat: improve logging of remakemigrations command (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
browniebroke committed Jan 24, 2024
1 parent 893399d commit 0688b8b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,23 @@ def handle(self, *args: str, **options: str) -> None:
"""Execute one step after another to avoid side effects between steps."""
# Remove old migration files
self.clear_old_migrations()
call_command("makemigrations")
# Recreate migrations
self.make_migrations()
# Update new files to be squashed of the old ones
self.update_new_migrations()
self.log_info("All done!")

def log_info(self, message: str) -> None:
"""Wrapper to help logging successes."""
self.stdout.write(self.style.SUCCESS(message))

def log_error(self, message: str) -> None:
"""Wrapper to help logging errors."""
self.stderr.write(self.style.ERROR(message))

def clear_old_migrations(self) -> None:
"""Remove all pre-existing migration files in first party apps."""
self.log_info("Removing old migration files...")
loader = MigrationLoader(None, ignore_no_migrations=True)
old_migrations = defaultdict(list)
for (app_label, migration_name), _migration_obj in loader.graph.nodes.items():
Expand All @@ -49,6 +60,11 @@ def clear_old_migrations(self) -> None:
self.remove_migration_file(app_label, migration_name)
self.old_migrations = dict(old_migrations)

def make_migrations(self) -> None:
"""Recreate migrations from scratch."""
self.log_info("Creating new migrations...")
call_command("makemigrations")

@staticmethod
def _is_first_party(app_config: AppConfig) -> bool:
app_path = Path(app_config.path)
Expand Down Expand Up @@ -82,6 +98,7 @@ def update_new_migrations(self) -> None:
This is to mark the new migrations as squashed, so they are not actually
executed by Django, they are simply marked as already applied.
"""
self.log_info("Updating new migrations...")
# Sort old migrations
sorted_old_migrations = self.sort_migrations_map(self.old_migrations)
loader = MigrationLoader(None, ignore_no_migrations=True, load=False)
Expand Down Expand Up @@ -168,7 +185,3 @@ def make_unique_name(auto_name: str) -> str:
# Don't use 'squashed' in the name as Django tries to be clever
# and treats the date as the latest number
return "_".join([number, "remaked", f"{today:%Y%m%d}", *name_parts])

def log_error(self, message: str) -> None:
"""Wrapper to help logging errors."""
self.stderr.write(self.style.ERROR(message))
7 changes: 6 additions & 1 deletion tests/test_remakemigrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ class Migration(migrations.Migration):

out, err, returncode = run_command("remakemigrations")

assert out == ""
assert (
out == "Removing old migration files...\n"
"Creating new migrations...\n"
"Updating new migrations...\n"
"All done!\n"
)
assert err == ""
assert returncode == 0

Expand Down

0 comments on commit 0688b8b

Please sign in to comment.