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