Skip to content

Commit

Permalink
Write detected migrations even when all are safe (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhiebert authored Jul 25, 2024
1 parent 17056c1 commit 442336b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/django_safemigrate/management/commands/safemigrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def pre_migrate_receiver(self, *, plan, **_):
ready, protected = filter_migrations(migrations)

if not protected:
return # No migrations to protect.
self.detect(migrations)
return # Run all the migrations

# Display the migrations that are protected
self.stdout.write(self.style.MIGRATE_HEADING("Protected migrations:"))
Expand Down Expand Up @@ -171,17 +172,21 @@ def pre_migrate_receiver(self, *, plan, **_):

# Only mark migrations as detected if not faking
if not self.fake:
# The detection datetime is what's used to determine if an
# after_deploy() with a delay can be migrated or not.
for migration in migrations:
SafeMigration.objects.get_or_create(
app=migration.app_label, name=migration.name
)
self.detect(migrations)

# Swap out the items in the plan with the safe migrations.
# None are backward, so we can always set backward to False.
plan[:] = [(migration, False) for migration in ready]

def detect(self, migrations):
"""Detect and record migrations to the database."""
# The detection datetime is what's used to determine if an
# after_deploy() with a delay can be migrated or not.
for migration in migrations:
SafeMigration.objects.get_or_create(
app=migration.app_label, name=migration.name
)

def delayed(self, migrations):
"""Handle delayed migrations."""
# Display delayed migrations if they exist:
Expand Down
18 changes: 18 additions & 0 deletions tests/safemigrate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,24 @@ def test_migrations_are_detected(self, receiver):
assert SafeMigration.objects.filter(detected__gt=existing.detected).count() == 2


def test_migrations_are_detected_when_no_delays(self, receiver):
"""Migrations should be marked as detected when there are no delays."""
plan = [
(Migration("spam", "0001_initial", safe=Safe.before_deploy()), False),
(
Migration(
"spam",
"0002_followup",
safe=Safe.always(),
dependencies=[("spam", "0001_initial")],
),
False,
),
]
receiver(plan=plan)
assert SafeMigration.objects.count() == 2


class TestCheckMissingSafe:
"""
Test the check command for migrations
Expand Down

0 comments on commit 442336b

Please sign in to comment.