Skip to content

Commit 24e4b1b

Browse files
committed
Load migration modules very python-like, handle replaces
1 parent 63a7f34 commit 24e4b1b

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

test_app/tests/conftest.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,27 @@ def test_migrations_okay(*args, **kwargs):
5454
app_config = apps.get_app_config(app)
5555
except LookupError:
5656
raise RuntimeError(f'App {app} is present in the recorded migrations but not installed, perhaps you need --create-db?')
57-
for path in os.listdir(os.path.join(app_config.path, 'migrations')):
58-
if re.match(r'^\d{4}_.*.py$', path):
59-
disk_steps[app].add(path.rsplit('.')[0])
57+
58+
migration_module = app_config.module.migrations
59+
for step in dir(migration_module):
60+
if not step.startswith('00'):
61+
continue
62+
step_module = getattr(migration_module, step)
63+
migration_name = step_module.__name__.rsplit('.')[-1]
64+
disk_steps[app].add(migration_name)
65+
66+
migration_cls = step_module.Migration
67+
for replaces_app_name, replaces_migration_name in getattr(migration_cls, 'replaces', []):
68+
disk_steps[app].add(replaces_migration_name)
69+
6070
db_steps = defaultdict(set)
6171
for record in MigrationRecorder.Migration.objects.only('app', 'name'):
6272
if record.app in app_exceptions:
6373
continue
6474
app_name = app_exceptions.get(record.app, record.app)
6575
db_steps[app_name].add(record.name)
76+
77+
6678
for app in disk_steps:
6779
assert disk_steps[app] == db_steps[app], f'Migrations not expected for app {app}, perhaps you need --create-db?'
6880

0 commit comments

Comments
 (0)