Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #13308 -- Ensured that dumpdata correctly interacts with router…

… allow_syncdb directions. Thanks to Francis (wizard_2) for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12940 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7b47609629692241848469fabc24fa798c0ac70b 1 parent e9bbdb3
Russell Keith-Magee authored April 09, 2010
4  django/core/management/commands/dumpdata.py
... ...
@@ -1,7 +1,7 @@
1 1
 from django.core.exceptions import ImproperlyConfigured
2 2
 from django.core.management.base import BaseCommand, CommandError
3 3
 from django.core import serializers
4  
-from django.db import connections, DEFAULT_DB_ALIAS
  4
+from django.db import connections, router, DEFAULT_DB_ALIAS
5 5
 from django.utils.datastructures import SortedDict
6 6
 
7 7
 from optparse import make_option
@@ -79,7 +79,7 @@ def handle(self, *app_labels, **options):
79 79
         # Now collate the objects to be serialized.
80 80
         objects = []
81 81
         for model in sort_dependencies(app_list.items()):
82  
-            if not model._meta.proxy:
  82
+            if not model._meta.proxy and router.allow_syncdb(using, model):
83 83
                 objects.extend(model._default_manager.using(using).all())
84 84
 
85 85
         try:
29  tests/regressiontests/multiple_database/tests.py
... ...
@@ -1,8 +1,11 @@
1 1
 import datetime
2 2
 import pickle
  3
+import sys
  4
+from StringIO import StringIO
3 5
 
4 6
 from django.conf import settings
5 7
 from django.contrib.auth.models import User
  8
+from django.core import management
6 9
 from django.db import connections, router, DEFAULT_DB_ALIAS
7 10
 from django.db.utils import ConnectionRouter
8 11
 from django.test import TestCase
@@ -1211,10 +1214,19 @@ def setUp(self):
1211 1214
         self.old_routers = router.routers
1212 1215
         router.routers = [AuthRouter()]
1213 1216
 
  1217
+        # Redirect stdout to a buffer so we can test
  1218
+        # the output of a management command
  1219
+        self.old_stdout = sys.stdout
  1220
+        self.stdout = StringIO()
  1221
+        sys.stdout = self.stdout
  1222
+
1214 1223
     def tearDown(self):
1215 1224
         # Restore the 'other' database as an independent database
1216 1225
         router.routers = self.old_routers
1217 1226
 
  1227
+        # Restore stdout
  1228
+        sys.stdout = self.old_stdout
  1229
+
1218 1230
     def test_auth_manager(self):
1219 1231
         "The methods on the auth manager obey database hints"
1220 1232
         # Create one user using default allocation policy
@@ -1243,6 +1255,22 @@ def test_auth_manager(self):
1243 1255
         self.assertEquals(User.objects.using('default').count(), 1)
1244 1256
         self.assertEquals(User.objects.using('other').count(), 1)
1245 1257
 
  1258
+    def test_dumpdata(self):
  1259
+        "Check that dumpdata honors allow_syncdb restrictions on the router"
  1260
+        User.objects.create_user('alice', 'alice@example.com')
  1261
+        User.objects.db_manager('default').create_user('bob', 'bob@example.com')
  1262
+
  1263
+        # Check that dumping the default database doesn't try to include auth
  1264
+        # because allow_syncdb prohibits auth on default
  1265
+        self.stdout.flush()
  1266
+        management.call_command('dumpdata', 'auth', format='json', database='default')
  1267
+        self.assertEquals(self.stdout.getvalue(), '[]\n')
  1268
+
  1269
+        # Check that dumping the other database does include auth
  1270
+        self.stdout.flush()
  1271
+        management.call_command('dumpdata', 'auth', format='json', database='other')
  1272
+        self.assertTrue('alice@example.com' in self.stdout.getvalue())
  1273
+
1246 1274
 class UserProfileTestCase(TestCase):
1247 1275
     def setUp(self):
1248 1276
         self.old_auth_profile_module = getattr(settings, 'AUTH_PROFILE_MODULE', None)
@@ -1307,7 +1335,6 @@ def test_fixture_loading(self):
1307 1335
         except Book.DoesNotExist:
1308 1336
             self.fail('"The Definitive Guide to Django" should exist on both databases')
1309 1337
 
1310  
-
1311 1338
 class PickleQuerySetTestCase(TestCase):
1312 1339
     multi_db = True
1313 1340
 

0 notes on commit 7b47609

Please sign in to comment.
Something went wrong with that request. Please try again.