|
1 | 1 | import random |
2 | 2 | import re |
3 | | -from collections import defaultdict |
4 | 3 | from datetime import datetime, timedelta |
5 | 4 | from unittest import mock |
6 | 5 |
|
@@ -38,43 +37,33 @@ def openapi_schema(): |
38 | 37 | return generator.get_schema(request=drf_request) |
39 | 38 |
|
40 | 39 |
|
41 | | -def test_migrations_okay(*args, **kwargs): |
| 40 | +def test_migrations_okay(apps, app_config, **kwargs): |
42 | 41 | """This test is not about the code, but for verifying your own state. |
43 | 42 |
|
44 | 43 | If you are not migrated to the correct state, this may hopefully alert you. |
45 | 44 | This is targeted toward situations like switching branches. |
46 | 45 | """ |
47 | | - disk_steps = defaultdict(set) |
| 46 | + disk_steps = set() |
48 | 47 | 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): |
72 | 55 | 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)) |
75 | 65 |
|
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?' |
78 | 67 |
|
79 | 68 |
|
80 | 69 | post_migrate.connect(test_migrations_okay) |
|
0 commit comments