Skip to content

Commit a6f7cb7

Browse files
committed
Load migration modules very python-like, handle replaces
Satisfy linters
1 parent 63a7f34 commit a6f7cb7

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

test_app/tests/conftest.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import os
21
import random
3-
import re
42
from collections import defaultdict
53
from datetime import datetime, timedelta
64
from unittest import mock
@@ -54,15 +52,26 @@ def test_migrations_okay(*args, **kwargs):
5452
app_config = apps.get_app_config(app)
5553
except LookupError:
5654
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])
55+
56+
migration_module = app_config.module.migrations
57+
for step in dir(migration_module):
58+
if not step.startswith('00'):
59+
continue
60+
step_module = getattr(migration_module, step)
61+
migration_name = step_module.__name__.rsplit('.')[-1]
62+
disk_steps[app].add(migration_name)
63+
64+
migration_cls = step_module.Migration
65+
for replaces_app_name, replaces_migration_name in getattr(migration_cls, 'replaces', []):
66+
disk_steps[app].add(replaces_migration_name)
67+
6068
db_steps = defaultdict(set)
6169
for record in MigrationRecorder.Migration.objects.only('app', 'name'):
6270
if record.app in app_exceptions:
6371
continue
6472
app_name = app_exceptions.get(record.app, record.app)
6573
db_steps[app_name].add(record.name)
74+
6675
for app in disk_steps:
6776
assert disk_steps[app] == db_steps[app], f'Migrations not expected for app {app}, perhaps you need --create-db?'
6877

0 commit comments

Comments
 (0)