File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
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
+ )
You can’t perform that action at this time.
0 commit comments