Fixed #27858 -- Prevented read-only management commands from creating the django_migrations table.

MigrationRecorder now assumes that if the django_migrations table
doesn't exist, then no migrations are applied.

Reverted documentation change from refs #23808.
This commit is contained in:
Marti Raudsepp 2016-11-05 10:16:23 +02:00 committed by Tim Graham
parent 4f1eb64ad0
commit fda55c71a8
7 changed files with 30 additions and 32 deletions

View file

@ -11,9 +11,7 @@ from django.db import (
ConnectionHandler, DatabaseError, connection, connections, models,
)
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.exceptions import (
InconsistentMigrationHistory, MigrationSchemaMissing,
)
from django.db.migrations.exceptions import InconsistentMigrationHistory
from django.db.migrations.recorder import MigrationRecorder
from django.test import override_settings
@ -697,35 +695,35 @@ class MakeMigrationsTests(MigrationTestBase):
The history consistency checks in makemigrations respect
settings.DATABASE_ROUTERS.
"""
def patched_ensure_schema(migration_recorder):
def patched_has_table(migration_recorder):
if migration_recorder.connection is connections['other']:
raise MigrationSchemaMissing('Patched')
raise Exception('Other connection')
else:
return mock.DEFAULT
self.assertTableNotExists('migrations_unicodemodel')
apps.register_model('migrations', UnicodeModel)
with mock.patch.object(
MigrationRecorder, 'ensure_schema',
autospec=True, side_effect=patched_ensure_schema) as ensure_schema:
MigrationRecorder, 'has_table',
autospec=True, side_effect=patched_has_table) as has_table:
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
self.assertEqual(has_table.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
self.assertEqual(has_table.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'):
with self.assertRaisesMessage(Exception, 'Other connection'):
call_command('makemigrations', 'migrations', verbosity=0)
self.assertEqual(ensure_schema.call_count, 4) # 'default' and 'other'
self.assertEqual(has_table.call_count, 4) # 'default' and 'other'
# With a router that doesn't allow migrating on any database,
# no consistency checks are made.
@ -741,7 +739,7 @@ class MakeMigrationsTests(MigrationTestBase):
self.assertIn(connection_alias, ['default', 'other'])
# Raises an error if invalid app_name/model_name occurs.
apps.get_app_config(app_name).get_model(call_kwargs['model_name'])
self.assertEqual(ensure_schema.call_count, 4)
self.assertEqual(has_table.call_count, 4)
def test_failing_migration(self):
# If a migration fails to serialize, it shouldn't generate an empty file. #21280