Skip to content

Commit

Permalink
feat: add setting to run other admin commands at the end (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
browniebroke committed Jan 24, 2024
1 parent 0688b8b commit 1f87053
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# General configuration
extensions = [
"myst_parser",
"sphinx.ext.autodoc",
]

# The suffix of source filenames.
Expand Down
8 changes: 8 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Configuration
=============

.. automodule:: django_remake_migrations.conf
:noindex:

.. autoclass:: AppSettings()
:members:
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
installation
usage
configuration
technical-details
```

Expand Down
50 changes: 50 additions & 0 deletions src/django_remake_migrations/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
These are the available settings.
All attributes prefixed ``REMAKE_MIGRATIONS_*`` can be overridden from your Django
project's settings module by defining a setting with the same name.
For instance, to run ``showmigrations`` and ``migrate`` after remaking,
add the following to your project settings:
.. code-block:: python
REMAKE_MIGRATIONS_POST_COMMANDS = [
["showmigrations"],
["migrate"],
]
"""
from __future__ import annotations

from collections.abc import Sequence
from dataclasses import dataclass
from typing import Any

from django.conf import settings as django_settings

# All attributes accessed with this prefix are possible to overwrite
# through django.conf.settings.
SETTINGS_PREFIX = "REMAKE_MIGRATIONS_"


@dataclass(frozen=True)
class AppSettings:
"""Access this instance as `django_remake_migrations.conf.app_settings`."""

REMAKE_MIGRATIONS_POST_COMMANDS: Sequence[Sequence[str]] = ()
"""Some commands with arguments to run after generating the new migration."""

def __getattribute__(self, __name: str) -> Any:
"""
Check if a Django project settings should override the app default.
In order to avoid returning any random properties of the django settings,
we inspect the prefix firstly.
"""
if __name.startswith(SETTINGS_PREFIX) and hasattr(django_settings, __name):
return getattr(django_settings, __name)

return super().__getattribute__(__name)


app_settings = AppSettings()
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from django.db.migrations.loader import MigrationLoader
from django.db.migrations.writer import MigrationWriter

from django_remake_migrations.conf import app_settings


class Command(BaseCommand):
"""
Expand All @@ -38,6 +40,8 @@ def handle(self, *args: str, **options: str) -> None:
self.make_migrations()
# Update new files to be squashed of the old ones
self.update_new_migrations()
# Run other commands
self.run_post_commands()
self.log_info("All done!")

def log_info(self, message: str) -> None:
Expand Down Expand Up @@ -185,3 +189,14 @@ 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 run_post_commands(self) -> None:
"""Run other management commands at the very end."""
post_commands = app_settings.REMAKE_MIGRATIONS_POST_COMMANDS
if not post_commands:
return

self.log_info("Running post-commands...")
for command_with_args in post_commands:
self.log_info(f"Running: {' '.join(command_with_args)}")
call_command(*command_with_args)

0 comments on commit 1f87053

Please sign in to comment.