summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAttila Tovt <uran198@gmail.com>2016-04-02 14:46:59 +0200
committerTim Graham <timograham@gmail.com>2016-04-05 08:52:08 -0400
commit02ae5fd31a56ffb42feadb49c1f3870ba0a24869 (patch)
treeb13a6d9f0b4d6f5da286c9360ddb6acd16613037 /tests
parent6448873197fa4e3df3f5f03201538dc57d7643d6 (diff)
Fixed #25850 -- Made migrate/makemigrations error on inconsistent history.
Diffstat (limited to 'tests')
-rw-r--r--tests/migrations/test_commands.py27
-rw-r--r--tests/migrations/test_loader.py17
2 files changed, 43 insertions, 1 deletions
diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py
index c7bb2e603c..88aff5d021 100644
--- a/tests/migrations/test_commands.py
+++ b/tests/migrations/test_commands.py
@@ -8,6 +8,7 @@ import os
from django.apps import apps
from django.core.management import CommandError, call_command
from django.db import DatabaseError, connection, connections, models
+from django.db.migrations.exceptions import InconsistentMigrationHistory
from django.db.migrations.recorder import MigrationRecorder
from django.test import ignore_warnings, mock, override_settings
from django.utils import six
@@ -462,6 +463,20 @@ class MigrateTests(MigrationTestBase):
)
# No changes were actually applied so there is nothing to rollback
+ @override_settings(MIGRATION_MODULES={'migrations': 'migrations.test_migrations'})
+ def test_migrate_inconsistent_history(self):
+ """
+ Running migrate with some migrations applied before their dependencies
+ should not be allowed.
+ """
+ recorder = MigrationRecorder(connection)
+ recorder.record_applied("migrations", "0002_second")
+ msg = "Migration migrations.0002_second is applied before its dependency migrations.0001_initial"
+ with self.assertRaisesMessage(InconsistentMigrationHistory, msg):
+ call_command("migrate")
+ applied_migrations = recorder.applied_migrations()
+ self.assertNotIn(("migrations", "0001_initial"), applied_migrations)
+
class MakeMigrationsTests(MigrationTestBase):
"""
@@ -1055,6 +1070,18 @@ class MakeMigrationsTests(MigrationTestBase):
call_command("makemigrations", "migrations", stdout=out)
self.assertIn(os.path.join(migration_dir, '0001_initial.py'), out.getvalue())
+ def test_makemigrations_inconsistent_history(self):
+ """
+ makemigrations should raise InconsistentMigrationHistory exception if
+ there are some migrations applied before their dependencies.
+ """
+ recorder = MigrationRecorder(connection)
+ recorder.record_applied('migrations', '0002_second')
+ msg = "Migration migrations.0002_second is applied before its dependency migrations.0001_initial"
+ with self.temporary_migration_module(module="migrations.test_migrations"):
+ with self.assertRaisesMessage(InconsistentMigrationHistory, msg):
+ call_command("makemigrations")
+
class SquashMigrationsTests(MigrationTestBase):
"""
diff --git a/tests/migrations/test_loader.py b/tests/migrations/test_loader.py
index bcb30bffd8..ec9c30adee 100644
--- a/tests/migrations/test_loader.py
+++ b/tests/migrations/test_loader.py
@@ -3,7 +3,9 @@ from __future__ import unicode_literals
from unittest import skipIf
from django.db import ConnectionHandler, connection, connections
-from django.db.migrations.exceptions import AmbiguityError, NodeNotFoundError
+from django.db.migrations.exceptions import (
+ AmbiguityError, InconsistentMigrationHistory, NodeNotFoundError,
+)
from django.db.migrations.loader import MigrationLoader
from django.db.migrations.recorder import MigrationRecorder
from django.test import TestCase, modify_settings, override_settings
@@ -382,3 +384,16 @@ class LoaderTests(TestCase):
recorder.record_applied("migrations", "7_auto")
loader.build_graph()
self.assertEqual(num_nodes(), 0)
+
+ @override_settings(
+ MIGRATION_MODULES={'migrations': 'migrations.test_migrations'},
+ INSTALLED_APPS=['migrations'],
+ )
+ def test_check_consistent_history(self):
+ loader = MigrationLoader(connection=None)
+ loader.check_consistent_history(connection)
+ recorder = MigrationRecorder(connection)
+ recorder.record_applied('migrations', '0002_second')
+ msg = "Migration migrations.0002_second is applied before its dependency migrations.0001_initial"
+ with self.assertRaisesMessage(InconsistentMigrationHistory, msg):
+ loader.check_consistent_history(connection)