Skip to content

Commit

Permalink
Add create-max-migration-files --recreate (#99)
Browse files Browse the repository at this point in the history
Fixes #79.
  • Loading branch information
adamchainz authored Aug 6, 2021
1 parent 2d9a927 commit d410c2e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
History
=======

* Added ``--recreate`` flag to ``create-max-migration-files``.

Thanks to Gordon Wrigley for the feature request in `Issue #79
<https://github.com/adamchainz/django-linear-migrations/issues/79>`__.

1.6.0 (2021-04-08)
------------------

Expand Down
17 changes: 16 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ If you see any apps listed that *aren’t* part of your project, define the list
python manage.py create-max-migration-files
In the future, when you add a new app to your project, you’ll need to create its ``max_migration.txt`` file.
If you defined ``FIRST_PARTY_APPS``, add it there, then rerun the creation command for the new app by specifying its label:
Add the new app to ``INSTALLED_APPS`` or ``FIRST_PARTY_APPS`` as appropriate, then rerun the creation command for the new app by specifying its label:

.. code-block:: sh
Expand All @@ -123,6 +123,21 @@ These are:
* ``dlm.E003``: ``<app_label>``'s max_migration.txt points to non-existent migration '``<bad_migration_name>``'.
* ``dlm.E004``: ``<app_label>``'s max_migration.txt contains '``<max_migration_name>``', but the latest migration is '``<real_max_migration_name>``'.

``create-max-migration-files`` command
--------------------------------------

.. code-block:: sh
python manage.py create-max-migration-files [app_label [app_label ...]]
This management command creates ``max_migration.txt`` files for all first party apps, or the given labels.
It’s used in initial installation of django-linear-migrations, and for recreating.
Pass the ``--dry-run`` flag to only list the ``max_migration.txt`` files that would be created.
Pass the ``--recreate`` flag to re-create files that already exist.
This may be useful after altering migrations with merges or manually.
``rebase-migration`` command
----------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@


class Command(BaseCommand):
help = (
"Generate max_migration.txt files for first-party apps that don't"
+ " have one."
)
help = "Generate max_migration.txt files for first-party apps."

# Checks disabled because the django-linear-migrations' checks would
# prevent us continuing
Expand All @@ -33,8 +30,17 @@ def add_arguments(self, parser):
default=False,
help="Actually create the files.",
)
parser.add_argument(
"--recreate",
action="store_true",
default=False,
help=(
"Recreate existing files. By default only non-existing files"
+ " will be created."
),
)

def handle(self, *app_labels, dry_run, **options):
def handle(self, *app_labels, dry_run, recreate, **options):
# Copied check from makemigrations
app_labels = set(app_labels)
has_bad_labels = False
Expand All @@ -57,7 +63,7 @@ def handle(self, *app_labels, dry_run, **options):
continue

max_migration_txt = migration_details.dir / "max_migration.txt"
if not max_migration_txt.exists():
if recreate or not max_migration_txt.exists():
if not dry_run:
max_migration_name = max(migration_details.names)
max_migration_txt.write_text(max_migration_name + "\n")
Expand Down
22 changes: 22 additions & 0 deletions tests/test_create_max_migration_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ def test_success_already_exists(self):
assert err == ""
assert returncode == 0

def test_success_recreate(self):
(self.migrations_dir / "__init__.py").touch()
(self.migrations_dir / "0001_initial.py").touch()
(self.migrations_dir / "max_migration.txt").write_text("0001_initial\n")

out, err, returncode = self.call_command("--recreate")

assert out == "Created max_migration.txt for testapp.\n"
assert err == ""
assert returncode == 0

def test_success_recreate_dry_run(self):
(self.migrations_dir / "__init__.py").touch()
(self.migrations_dir / "0001_initial.py").touch()
(self.migrations_dir / "max_migration.txt").write_text("0001_initial\n")

out, err, returncode = self.call_command("--recreate", "--dry-run")

assert out == "Would create max_migration.txt for testapp.\n"
assert err == ""
assert returncode == 0

def test_success_specific_app_label(self):
(self.migrations_dir / "__init__.py").touch()
(self.migrations_dir / "0001_initial.py").touch()
Expand Down

0 comments on commit d410c2e

Please sign in to comment.