1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #16039 -- Made post_syncdb handlers multi-db aware.

Also reverted 8fb7a90026. Refs #17055.
This commit is contained in:
Aymeric Augustin
2012-11-22 20:09:40 +01:00
parent ea6b95dbec
commit a026e480da
5 changed files with 116 additions and 33 deletions

View File

@@ -16,16 +16,6 @@ from django.utils.six import StringIO
from .models import Book, Person, Pet, Review, UserProfile
def copy_content_types_from_default_to_other():
# On post_syncdb, content types are created in the 'default' database.
# However, tests of generic foreign keys require them in 'other' too.
# The problem is masked on backends that defer constraints checks: at the
# end of each test, there's a rollback, and constraints are never checked.
# It only appears on MySQL + InnoDB.
for ct in ContentType.objects.using('default').all():
ct.save(using='other')
class QueryTestCase(TestCase):
multi_db = True
@@ -705,8 +695,6 @@ class QueryTestCase(TestCase):
def test_generic_key_separation(self):
"Generic fields are constrained to a single database"
copy_content_types_from_default_to_other()
# Create a book and author on the default database
pro = Book.objects.create(title="Pro Django",
published=datetime.date(2008, 12, 16))
@@ -734,8 +722,6 @@ class QueryTestCase(TestCase):
def test_generic_key_reverse_operations(self):
"Generic reverse manipulations are all constrained to a single DB"
copy_content_types_from_default_to_other()
dive = Book.objects.using('other').create(title="Dive into Python",
published=datetime.date(2009, 5, 4))
@@ -780,8 +766,6 @@ class QueryTestCase(TestCase):
def test_generic_key_cross_database_protection(self):
"Operations that involve sharing generic key objects across databases raise an error"
copy_content_types_from_default_to_other()
# Create a book and author on the default database
pro = Book.objects.create(title="Pro Django",
published=datetime.date(2008, 12, 16))
@@ -833,8 +817,6 @@ class QueryTestCase(TestCase):
def test_generic_key_deletion(self):
"Cascaded deletions of Generic Key relations issue queries on the right database"
copy_content_types_from_default_to_other()
dive = Book.objects.using('other').create(title="Dive into Python",
published=datetime.date(2009, 5, 4))
review = Review.objects.using('other').create(source="Python Weekly", content_object=dive)
@@ -1402,8 +1384,6 @@ class RouterTestCase(TestCase):
def test_generic_key_cross_database_protection(self):
"Generic Key operations can span databases if they share a source"
copy_content_types_from_default_to_other()
# Create a book and author on the default database
pro = Book.objects.using('default'
).create(title="Pro Django", published=datetime.date(2008, 12, 16))
@@ -1515,8 +1495,6 @@ class RouterTestCase(TestCase):
def test_generic_key_managers(self):
"Generic key relations are represented by managers, and can be controlled like managers"
copy_content_types_from_default_to_other()
pro = Book.objects.using('other').create(title="Pro Django",
published=datetime.date(2008, 12, 16))
@@ -1922,3 +1900,36 @@ class RouterModelArgumentTestCase(TestCase):
pet = Pet.objects.create(owner=person, name='Wart')
# test related FK collection
person.delete()
class SyncOnlyDefaultDatabaseRouter(object):
def allow_syncdb(self, db, model):
return db == DEFAULT_DB_ALIAS
class SyncDBTestCase(TestCase):
multi_db = True
def test_syncdb_to_other_database(self):
"""Regression test for #16039: syncdb with --database option."""
count = ContentType.objects.count()
self.assertGreater(count, 0)
ContentType.objects.using('other').delete()
management.call_command('syncdb', verbosity=0, interactive=False,
load_initial_data=False, database='other')
self.assertEqual(ContentType.objects.using("other").count(), count)
def test_syncdb_to_other_database_with_router(self):
"""Regression test for #16039: syncdb with --database option."""
ContentType.objects.using('other').delete()
try:
old_routers = router.routers
router.routers = [SyncOnlyDefaultDatabaseRouter()]
management.call_command('syncdb', verbosity=0, interactive=False,
load_initial_data=False, database='other')
finally:
router.routers = old_routers
self.assertEqual(ContentType.objects.using("other").count(), 0)