1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Fixed #27142, #27110 -- Made makemigrations consistency checks respect database routers.

Partially reverted refs #27054 except for one of the tests as this
solution supersedes that one.

Thanks Shai Berger for the review.
This commit is contained in:
Tim Graham
2016-09-01 16:19:29 -04:00
committed by GitHub
parent 32c02f2a0e
commit 098c07a032
6 changed files with 48 additions and 29 deletions

View File

@@ -22,6 +22,7 @@ from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.encoding import force_text
from .models import UnicodeModel, UnserializableModel
from .routers import TestRouter
from .test_base import MigrationTestBase
@@ -597,14 +598,14 @@ class MakeMigrationsTests(MigrationTestBase):
init_file = os.path.join(migration_dir, '__init__.py')
self.assertTrue(os.path.exists(init_file))
def test_makemigrations_other_datbase_is_readonly(self):
def test_makemigrations_consistency_checks_respect_routers(self):
"""
makemigrations ignores the non-default database if it's read-only.
The history consistency checks in makemigrations respect
settings.DATABASE_ROUTERS.
"""
def patched_ensure_schema(migration_recorder):
from django.db import connections
if migration_recorder.connection is connections['other']:
raise MigrationSchemaMissing()
raise MigrationSchemaMissing('Patched')
else:
return mock.DEFAULT
@@ -612,11 +613,33 @@ class MakeMigrationsTests(MigrationTestBase):
apps.register_model('migrations', UnicodeModel)
with mock.patch.object(
MigrationRecorder, 'ensure_schema',
autospec=True, side_effect=patched_ensure_schema):
autospec=True, side_effect=patched_ensure_schema) as ensure_schema:
with self.temporary_migration_module() as migration_dir:
call_command("makemigrations", "migrations", verbosity=0)
initial_file = os.path.join(migration_dir, "0001_initial.py")
self.assertTrue(os.path.exists(initial_file))
self.assertEqual(ensure_schema.call_count, 1) # 'default' is checked
# Router says not to migrate 'other' so consistency shouldn't
# be checked.
with self.settings(DATABASE_ROUTERS=['migrations.routers.TestRouter']):
call_command('makemigrations', 'migrations', verbosity=0)
self.assertEqual(ensure_schema.call_count, 2) # 'default' again
# With a router that doesn't prohibit migrating 'other',
# consistency is checked.
with self.settings(DATABASE_ROUTERS=['migrations.routers.EmptyRouter']):
with self.assertRaisesMessage(MigrationSchemaMissing, 'Patched'):
call_command('makemigrations', 'migrations', verbosity=0)
self.assertEqual(ensure_schema.call_count, 4) # 'default' and 'other'
# With a router that doesn't allow migrating on any database,
# no consistency checks are made.
with self.settings(DATABASE_ROUTERS=['migrations.routers.TestRouter']):
with mock.patch.object(TestRouter, 'allow_migrate', return_value=False) as allow_migrate:
call_command('makemigrations', 'migrations', verbosity=0)
allow_migrate.assert_called_with('other', 'migrations')
self.assertEqual(ensure_schema.call_count, 4)
def test_failing_migration(self):
# If a migration fails to serialize, it shouldn't generate an empty file. #21280