Skip to content

Commit fb3963f

Browse files
committed
Improve preformance for migration checker
1 parent ed23011 commit fb3963f

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

test_app/tests/conftest.py

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import random
22
import re
3-
from collections import defaultdict
43
from datetime import datetime, timedelta
54
from unittest import mock
65

@@ -38,43 +37,33 @@ def openapi_schema():
3837
return generator.get_schema(request=drf_request)
3938

4039

41-
def test_migrations_okay(*args, **kwargs):
40+
def test_migrations_okay(apps, app_config, **kwargs):
4241
"""This test is not about the code, but for verifying your own state.
4342
4443
If you are not migrated to the correct state, this may hopefully alert you.
4544
This is targeted toward situations like switching branches.
4645
"""
47-
disk_steps = defaultdict(set)
46+
disk_steps = set()
4847
app_exceptions = {'default': 'auth', 'social_auth': 'social_django'}
49-
for app in MigrationRecorder.Migration.objects.values_list('app', flat=True).distinct():
50-
if app in app_exceptions:
51-
continue
52-
try:
53-
app_config = apps.get_app_config(app)
54-
except LookupError:
55-
raise RuntimeError(f'App {app} is present in the recorded migrations but not installed, perhaps you need --create-db?')
56-
57-
migration_module = app_config.module.migrations
58-
for step in dir(migration_module):
59-
if not re.match(r'\d{4}_', step):
60-
continue
61-
step_module = getattr(migration_module, step)
62-
migration_name = step_module.__name__.rsplit('.')[-1]
63-
disk_steps[app].add(migration_name)
64-
65-
migration_cls = step_module.Migration
66-
for replaces_app_name, replaces_migration_name in getattr(migration_cls, 'replaces', []):
67-
disk_steps[app].add(replaces_migration_name)
68-
69-
db_steps = defaultdict(set)
70-
for record in MigrationRecorder.Migration.objects.only('app', 'name'):
71-
if record.app in app_exceptions:
48+
49+
if app_config.label in app_exceptions or (not hasattr(app_config, 'module')) or (not hasattr(app_config.module, 'migrations')):
50+
return
51+
52+
migration_module = app_config.module.migrations
53+
for step in dir(migration_module):
54+
if not re.match(r'\d{4}_', step):
7255
continue
73-
app_name = app_exceptions.get(record.app, record.app)
74-
db_steps[app_name].add(record.name)
56+
step_module = getattr(migration_module, step)
57+
migration_name = step_module.__name__.rsplit('.')[-1]
58+
disk_steps.add(migration_name)
59+
60+
migration_cls = step_module.Migration
61+
for replaces_app_name, replaces_migration_name in getattr(migration_cls, 'replaces', []):
62+
disk_steps.add(replaces_migration_name)
63+
64+
db_steps = set(MigrationRecorder.Migration.objects.filter(app=app_config.label).values_list('name', flat=True))
7565

76-
for app in disk_steps:
77-
assert disk_steps[app] == db_steps[app], f'Migrations not expected for app {app}, perhaps you need --create-db?'
66+
assert disk_steps == db_steps, f'Migrations not expected for app {app_config.label}, perhaps you need --create-db?'
7867

7968

8069
post_migrate.connect(test_migrations_okay)

0 commit comments

Comments
 (0)