Fixed #25388 -- Added an option to allow disabling of migrations during test database creation

This commit is contained in:
Berker Peksag 2016-03-22 14:23:22 +02:00 committed by Markus Holtermann
parent 1243fdf5cb
commit 157d7f1f1d
6 changed files with 38 additions and 5 deletions

View File

@ -48,8 +48,10 @@ class MigrationLoader(object):
if load: if load:
self.build_graph() self.build_graph()
@classmethod def migrations_module(self, app_label):
def migrations_module(cls, app_label): if (self.connection is not None and
not self.connection.settings_dict.get('TEST', {}).get('MIGRATE', True)):
return None
if app_label in settings.MIGRATION_MODULES: if app_label in settings.MIGRATION_MODULES:
return settings.MIGRATION_MODULES[app_label] return settings.MIGRATION_MODULES[app_label]
else: else:

View File

@ -37,7 +37,7 @@ class MigrationQuestioner(object):
app_config = apps.get_app_config(app_label) app_config = apps.get_app_config(app_label)
except LookupError: # It's a fake app. except LookupError: # It's a fake app.
return self.defaults.get("ask_initial", False) return self.defaults.get("ask_initial", False)
migrations_import_path = MigrationLoader.migrations_module(app_config.label) migrations_import_path = MigrationLoader(None, load=False).migrations_module(app_config.label)
if migrations_import_path is None: if migrations_import_path is None:
# It's an application with migrations disabled. # It's an application with migrations disabled.
return self.defaults.get("ask_initial", False) return self.defaults.get("ask_initial", False)

View File

@ -221,7 +221,7 @@ class MigrationWriter(object):
@property @property
def basedir(self): def basedir(self):
migrations_package_name = MigrationLoader.migrations_module(self.migration.app_label) migrations_package_name = MigrationLoader(None, load=False).migrations_module(self.migration.app_label)
if migrations_package_name is None: if migrations_package_name is None:
raise ValueError( raise ValueError(

View File

@ -710,6 +710,17 @@ The creation-order dependencies of the database. See the documentation
on :ref:`controlling the creation order of test databases on :ref:`controlling the creation order of test databases
<topics-testing-creation-dependencies>` for details. <topics-testing-creation-dependencies>` for details.
.. setting:: TEST_MIGRATE
``MIGRATE``
^^^^^^^^^^^
.. versionadded:: 1.10
Default: ``True``
If set to ``False``, Django won't use migrations to create the test database.
.. setting:: TEST_MIRROR .. setting:: TEST_MIRROR
``MIRROR`` ``MIRROR``

View File

@ -394,6 +394,9 @@ Tests
and run selectively with the new :option:`test --tag` and :option:`test and run selectively with the new :option:`test --tag` and :option:`test
--exclude-tag` options. --exclude-tag` options.
* Added the :setting:`DATABASES['TEST']['MIGRATE'] <TEST_MIGRATE>` option to
allow disabling of migrations during test database creation.
URLs URLs
~~~~ ~~~~

View File

@ -2,7 +2,7 @@ from __future__ import unicode_literals
from unittest import skipIf from unittest import skipIf
from django.db import connection, connections from django.db import ConnectionHandler, connection, connections
from django.db.migrations.exceptions import AmbiguityError, NodeNotFoundError from django.db.migrations.exceptions import AmbiguityError, NodeNotFoundError
from django.db.migrations.loader import MigrationLoader from django.db.migrations.loader import MigrationLoader
from django.db.migrations.recorder import MigrationRecorder from django.db.migrations.recorder import MigrationRecorder
@ -202,6 +202,23 @@ class LoaderTests(TestCase):
self.assertEqual(migration_loader.migrated_apps, set()) self.assertEqual(migration_loader.migrated_apps, set())
self.assertEqual(migration_loader.unmigrated_apps, {'migrated_app'}) self.assertEqual(migration_loader.unmigrated_apps, {'migrated_app'})
@override_settings(
INSTALLED_APPS=['migrations.migrations_test_apps.migrated_app'],
)
def test_disable_migrations(self):
connections = ConnectionHandler({
'default': {
'NAME': 'dummy',
'ENGINE': 'django.db.backends.sqlite3',
'TEST': {
'MIGRATE': False,
},
},
})
migration_loader = MigrationLoader(connections['default'])
self.assertEqual(migration_loader.migrated_apps, set())
self.assertEqual(migration_loader.unmigrated_apps, {'migrated_app'})
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}) @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"})
def test_loading_squashed(self): def test_loading_squashed(self):
"Tests loading a squashed migration" "Tests loading a squashed migration"