-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make 'makemigrations --merge' update max_migrations.txt (#361)
Fixes #78.
- Loading branch information
1 parent
5921325
commit 8990895
Showing
5 changed files
with
118 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Generator | ||
from contextlib import contextmanager | ||
from typing import Any | ||
|
||
from django.db.migrations.writer import MigrationWriter | ||
|
||
|
||
@contextmanager | ||
def spy_on_migration_writers() -> Generator[dict[str, str]]: | ||
written_migrations = {} | ||
|
||
orig_as_string = MigrationWriter.as_string | ||
|
||
def wrapped_as_string(self: MigrationWriter, *args: Any, **kwargs: Any) -> str: | ||
written_migrations[self.migration.app_label] = self.migration.name | ||
return orig_as_string(self, *args, **kwargs) | ||
|
||
MigrationWriter.as_string = wrapped_as_string # type: ignore [method-assign] | ||
try: | ||
yield written_migrations | ||
finally: | ||
MigrationWriter.as_string = orig_as_string # type: ignore [method-assign] |
68 changes: 23 additions & 45 deletions
68
src/django_linear_migrations/management/commands/makemigrations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,32 @@ | ||
from __future__ import annotations | ||
|
||
import django | ||
from typing import Any | ||
|
||
from django.core.management.commands.makemigrations import Command as BaseCommand | ||
from django.db.migrations import Migration | ||
|
||
from django_linear_migrations.apps import MigrationDetails | ||
from django_linear_migrations.apps import first_party_app_configs | ||
from django_linear_migrations.management.commands import spy_on_migration_writers | ||
|
||
|
||
class Command(BaseCommand): | ||
if django.VERSION >= (4, 2): | ||
|
||
def write_migration_files( | ||
self, | ||
changes: dict[str, list[Migration]], | ||
update_previous_migration_paths: dict[str, str] | None = None, | ||
) -> None: | ||
# django-stubs awaiting new signature: | ||
# https://github.com/typeddjango/django-stubs/pull/1609 | ||
super().write_migration_files( | ||
changes, | ||
update_previous_migration_paths, | ||
) | ||
_post_write_migration_files(self.dry_run, changes) | ||
|
||
else: | ||
|
||
def write_migration_files( # type: ignore[misc,override] | ||
self, | ||
changes: dict[str, list[Migration]], | ||
) -> None: | ||
super().write_migration_files(changes) | ||
_post_write_migration_files(self.dry_run, changes) | ||
|
||
|
||
def _post_write_migration_files( | ||
dry_run: bool, changes: dict[str, list[Migration]] | ||
) -> None: | ||
if dry_run: | ||
return | ||
|
||
first_party_app_labels = { | ||
app_config.label for app_config in first_party_app_configs() | ||
} | ||
|
||
for app_label, app_migrations in changes.items(): | ||
if app_label not in first_party_app_labels: | ||
continue | ||
|
||
# Reload required as we've generated changes | ||
migration_details = MigrationDetails(app_label, do_reload=True) | ||
max_migration_txt = migration_details.dir / "max_migration.txt" | ||
max_migration_txt.write_text(f"{app_migrations[-1].name}\n") | ||
|
||
def handle(self, *app_labels: Any, **options: Any) -> None: | ||
with spy_on_migration_writers() as written_migrations: | ||
super().handle(*app_labels, **options) | ||
|
||
if options["dry_run"]: | ||
return | ||
|
||
first_party_app_labels = { | ||
app_config.label for app_config in first_party_app_configs() | ||
} | ||
|
||
for app_label, migration_name in written_migrations.items(): | ||
if app_label not in first_party_app_labels: | ||
continue | ||
|
||
# Reload required in case of initial migration | ||
migration_details = MigrationDetails(app_label, do_reload=True) | ||
max_migration_txt = migration_details.dir / "max_migration.txt" | ||
max_migration_txt.write_text(f"{migration_name}\n") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters