|
1 | | -import os |
2 | 1 | import random |
3 | 2 | import re |
4 | 3 | from collections import defaultdict |
@@ -54,15 +53,26 @@ def test_migrations_okay(*args, **kwargs): |
54 | 53 | app_config = apps.get_app_config(app) |
55 | 54 | except LookupError: |
56 | 55 | 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]) |
| 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 | + |
60 | 69 | db_steps = defaultdict(set) |
61 | 70 | for record in MigrationRecorder.Migration.objects.only('app', 'name'): |
62 | 71 | if record.app in app_exceptions: |
63 | 72 | continue |
64 | 73 | app_name = app_exceptions.get(record.app, record.app) |
65 | 74 | db_steps[app_name].add(record.name) |
| 75 | + |
66 | 76 | for app in disk_steps: |
67 | 77 | assert disk_steps[app] == db_steps[app], f'Migrations not expected for app {app}, perhaps you need --create-db?' |
68 | 78 |
|
|
0 commit comments