Skip to content

Commit 2c14486

Browse files
authored
Add a test to Ensure not have missing migrations
1 parent 8467a50 commit 2c14486

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

pydotorg/tests/test_migrations.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from django.apps.registry import apps
2+
from django.db import connection
3+
from django.db.migrations.autodetector import MigrationAutodetector
4+
from django.db.migrations.executor import MigrationExecutor
5+
from django.db.migrations.state import ProjectState
6+
from django.test import TestCase
7+
8+
9+
class TestMissingMigrationTestCase(TestCase):
10+
11+
def test_missing_migrations(self):
12+
"""
13+
Tests if there are any changes left in the Django models for which the migration script was not generated.
14+
It will always fail if there are any changes to Django models without the respective migration file.
15+
"""
16+
17+
migration_executor = MigrationExecutor(connection)
18+
auto_detector = MigrationAutodetector(
19+
migration_executor.loader.project_state(),
20+
ProjectState.from_apps(apps),
21+
)
22+
changes = auto_detector.changes(graph=migration_executor.loader.graph)
23+
app_names = ', '.join(changes.keys())
24+
self.assertFalse(
25+
changes,
26+
msg=(
27+
f'Missing migrations were detected on the following apps: {app_names}.'
28+
f' Run `python3 manage.py makemigrations` to generate new ones.'
29+
)
30+
)

0 commit comments

Comments
 (0)