Skip to content

Commit

Permalink
Rename enums for clarity and pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhiebert committed Jul 25, 2024
1 parent 7798942 commit 3de2104
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Pending
* ``Safe.always()``
* Add support for allowing a ``Safe.after_deploy(delay=timedelta())``
migration to be migrated after the delay has passed.
* Rename internal enums for clarity and PEP8.

4.3 (2024-03-28)
++++++++++++++++
Expand Down
10 changes: 5 additions & 5 deletions src/django_safemigrate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
from enum import Enum


class SafeEnum(Enum):
class When(Enum):
always = "always"
before_deploy = "before_deploy"
after_deploy = "after_deploy"


@dataclass
class Safe:
safe: SafeEnum
when: When
delay: timedelta | None = None

@classmethod
def always(cls):
return cls(safe=SafeEnum.always)
return cls(when=When.always)

@classmethod
def before_deploy(cls):
return cls(safe=SafeEnum.before_deploy)
return cls(when=When.before_deploy)

@classmethod
def after_deploy(cls, *, delay: timedelta = None):
return cls(safe=SafeEnum.after_deploy, delay=delay)
return cls(when=When.after_deploy, delay=delay)
38 changes: 17 additions & 21 deletions src/django_safemigrate/management/commands/safemigrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Migration safety is enforced by a pre_migrate signal receiver.
"""

from __future__ import annotations

from enum import Enum
Expand All @@ -14,31 +15,26 @@
from django.utils import timezone
from django.utils.timesince import timeuntil

from django_safemigrate import Safe, SafeEnum
from django_safemigrate import Safe, When
from django_safemigrate.models import SafeMigration


class SafeMigrate(Enum):
strict = "strict"
nonstrict = "nonstrict"
disabled = "disabled"
class Mode(Enum):
STRICT = "strict"
NONSTRICT = "nonstrict"
DISABLED = "disabled"


def safety(migration: Migration):
"""Determine the safety status of a migration."""
return getattr(migration, "safe", Safe.after_deploy())


def safemigrate():
state = getattr(settings, "SAFEMIGRATE", None)
if state is None:
return state
def safemigrate_mode():
try:
return SafeMigrate(state.lower())
except ValueError as e:
raise ValueError(
"Invalid SAFEMIGRATE setting, it must be one of 'strict', 'nonstrict', or 'disabled'."
) from e
return Mode(getattr(settings, "SAFEMIGRATE", "strict").lower())
except:
raise ValueError("SAFEMIGRATE must be 'strict', 'nonstrict', or 'disabled'.")


def filter_migrations(
Expand All @@ -60,7 +56,7 @@ def is_protected(migration):
migration_safe = safety(migration)
detected = detected_map.get((migration.app_label, migration.name))
# A migration is protected if detected is None or delay is not specified.
return migration_safe.safe == SafeEnum.after_deploy and (
return migration_safe.when == When.after_deploy and (
detected is None
or migration_safe.delay is None
or now < (detected + migration_safe.delay)
Expand Down Expand Up @@ -101,13 +97,13 @@ def pre_migrate_receiver(self, *, plan, **_):
return # Only run once
self.receiver_has_run = True

safemigrate_state = safemigrate()
if safemigrate_state == SafeMigrate.disabled:
mode = safemigrate_mode()
if mode == Mode.DISABLED:
# When disabled, run migrate
return

# strict by default
strict = safemigrate_state != SafeMigrate.nonstrict
strict = mode != Mode.NONSTRICT

if any(backward for mig, backward in plan):
raise CommandError("Backward migrations are not supported.")
Expand All @@ -120,7 +116,7 @@ def pre_migrate_receiver(self, *, plan, **_):
migration
for migration in migrations
if not isinstance(safety(migration), Safe)
or safety(migration).safe not in SafeEnum
or safety(migration).when not in When
]
if invalid:
self.stdout.write(self.style.MIGRATE_HEADING("Invalid migrations:"))
Expand Down Expand Up @@ -158,7 +154,7 @@ def pre_migrate_receiver(self, *, plan, **_):

for migration in block:
ready.remove(migration)
if safety(migration).safe == SafeEnum.before_deploy:
if safety(migration).when == When.before_deploy:
blocked.append(migration)
else:
delayed.append(migration)
Expand Down Expand Up @@ -194,7 +190,7 @@ def delayed(self, migrations):
for migration in migrations:
migration_safe = safety(migration)
if (
migration_safe.safe == SafeEnum.after_deploy
migration_safe.when == When.after_deploy
and migration_safe.delay is not None
):
now = timezone.localtime()
Expand Down

0 comments on commit 3de2104

Please sign in to comment.