Skip to content

Commit

Permalink
Enforce that the safe property must be valid (#9)
Browse files Browse the repository at this point in the history
It is acceptable for the safe property to not be given, and get the
default. However, if the safe property is given, then it must be
set correctly. Enforce this by ensuring that the value we will use
is part of the Safe enum.
  • Loading branch information
ryanhiebert authored Jan 17, 2019
1 parent 284d6d4 commit db7b11f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
because they don't require any database changes.
* Multiple dependent ``Safe.after_deploy`` migrations do not block deployment
as long as there are no dependent ``Safe.before_deploy`` migrations.
* Enforce that any given value of safe is valid.

1.0 (2019-01-13)
++++++++++++++++
Expand Down
13 changes: 13 additions & 0 deletions src/django_safemigrate/management/commands/safemigrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ def pre_migrate_receiver(self, *, plan, **_):

# Pull the migrations into a new list
migrations = [migration for migration, backward in plan]

# Check for invalid safe properties
invalid = [
migration for migration in migrations if safety(migration) not in Safe
]
if invalid:
self.stdout.write(self.style.MIGRATE_HEADING("Invalid migrations:"))
for migration in invalid:
self.stdout.write(f" {migration.app_label}.{migration.name}")
raise CommandError(
"Aborting due to migrations with invalid safe properties."
)

protected = [
migration
for migration in migrations
Expand Down
12 changes: 12 additions & 0 deletions tests/safemigrate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,15 @@ def test_blocked_by_after_nonstrict(self, settings, receiver):
]
receiver(plan=plan)
assert len(plan) == 1

def test_string_invalid(self, receiver):
"""Invalid settings of the safe property will raise an error."""
plan = [(Migration("spam", "0001_initial", safe="before_deploy"), False)]
with pytest.raises(CommandError):
receiver(plan=plan)

def test_boolean_invalid(self, receiver):
"""Booleans are invalid for the safe property."""
plan = [(Migration("spam", "0001_initial", safe=False), False)]
with pytest.raises(CommandError):
receiver(plan=plan)

0 comments on commit db7b11f

Please sign in to comment.