From c4ab76f9a6051f9cd1801838cee46c6a10056455 Mon Sep 17 00:00:00 2001 From: Ryan Hiebert Date: Thu, 25 Jul 2024 13:17:58 -0500 Subject: [PATCH] Add compatibility with documented prior API --- HISTORY.rst | 2 +- .../management/commands/safemigrate.py | 4 +++- tests/safemigrate_test.py | 11 +++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index e69f115..068b582 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -4,7 +4,7 @@ Pending * Add support for Django 5.1. * Drop support for Django 3.2, 4.0, 4.1. * Convert ``Safe`` to be a custom class rather than an ``Enum``. -* The valid values for ``safe`` are: +* The preferred values for ``safe`` are now methods: * ``Safe.before_deploy()`` * ``Safe.after_deploy()`` diff --git a/src/django_safemigrate/management/commands/safemigrate.py b/src/django_safemigrate/management/commands/safemigrate.py index e4724f5..e0fd3c2 100644 --- a/src/django_safemigrate/management/commands/safemigrate.py +++ b/src/django_safemigrate/management/commands/safemigrate.py @@ -27,7 +27,9 @@ class Mode(Enum): def safety(migration: Migration): """Determine the safety status of a migration.""" - return getattr(migration, "safe", Safe.always()) + safe = getattr(migration, "safe", Safe.always()) + callables = [Safe.before_deploy, Safe.after_deploy, Safe.always] + return safe() if safe in callables else safe def safemigrate_mode(): diff --git a/tests/safemigrate_test.py b/tests/safemigrate_test.py index bc20b4e..5167cca 100644 --- a/tests/safemigrate_test.py +++ b/tests/safemigrate_test.py @@ -61,6 +61,17 @@ def test_rerun(self, receiver): receiver(plan=plan) assert len(plan) == 1 + def test_callable_compat(self, receiver): + """Understand and do not throw an error when using compatibility syntax.""" + # The plan items aren't dependencies of each other. + plan = [ + (Migration(safe=Safe.before_deploy), False), + (Migration(safe=Safe.always), False), + (Migration(safe=Safe.after_deploy), False), + ] + receiver(plan=plan) + assert len(plan) == 2 + def test_backward(self, receiver): """It should fail to run backward.""" plan = [(Migration(), True)]