Skip to content

Commit

Permalink
Load migration modules very python-like, handle replaces (ansible#670)
Browse files Browse the repository at this point in the history
This fixes an issue where tests would error doing branch-switching incorrectly
  • Loading branch information
AlanCoding authored Dec 6, 2024
1 parent 63a7f34 commit ed23011
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions test_app/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import random
import re
from collections import defaultdict
Expand Down Expand Up @@ -54,15 +53,26 @@ def test_migrations_okay(*args, **kwargs):
app_config = apps.get_app_config(app)
except LookupError:
raise RuntimeError(f'App {app} is present in the recorded migrations but not installed, perhaps you need --create-db?')
for path in os.listdir(os.path.join(app_config.path, 'migrations')):
if re.match(r'^\d{4}_.*.py$', path):
disk_steps[app].add(path.rsplit('.')[0])

migration_module = app_config.module.migrations
for step in dir(migration_module):
if not re.match(r'\d{4}_', step):
continue
step_module = getattr(migration_module, step)
migration_name = step_module.__name__.rsplit('.')[-1]
disk_steps[app].add(migration_name)

migration_cls = step_module.Migration
for replaces_app_name, replaces_migration_name in getattr(migration_cls, 'replaces', []):
disk_steps[app].add(replaces_migration_name)

db_steps = defaultdict(set)
for record in MigrationRecorder.Migration.objects.only('app', 'name'):
if record.app in app_exceptions:
continue
app_name = app_exceptions.get(record.app, record.app)
db_steps[app_name].add(record.name)

for app in disk_steps:
assert disk_steps[app] == db_steps[app], f'Migrations not expected for app {app}, perhaps you need --create-db?'

Expand Down

0 comments on commit ed23011

Please sign in to comment.