Skip to content

Commit ed23011

Browse files
authored
Load migration modules very python-like, handle replaces (ansible#670)
This fixes an issue where tests would error doing branch-switching incorrectly
1 parent 63a7f34 commit ed23011

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

test_app/tests/conftest.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import random
32
import re
43
from collections import defaultdict
@@ -54,15 +53,26 @@ def test_migrations_okay(*args, **kwargs):
5453
app_config = apps.get_app_config(app)
5554
except LookupError:
5655
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+
6069
db_steps = defaultdict(set)
6170
for record in MigrationRecorder.Migration.objects.only('app', 'name'):
6271
if record.app in app_exceptions:
6372
continue
6473
app_name = app_exceptions.get(record.app, record.app)
6574
db_steps[app_name].add(record.name)
75+
6676
for app in disk_steps:
6777
assert disk_steps[app] == db_steps[app], f'Migrations not expected for app {app}, perhaps you need --create-db?'
6878

0 commit comments

Comments
 (0)