Author: aaugustin
Date: 2012-01-07 00:53:36 -0800 (Sat, 07 Jan 2012)
New Revision: 17343

Modified:
   django/trunk/django/contrib/sites/management.py
   django/trunk/django/contrib/sites/tests.py
Log:
Fixed #17415 -- Reset database sequence for Site's pk after creating the 
default site with an explicit pk. Thanks niko AT neagee net for the report, 
Russell and Karen for describing the fix, and Anssi for drafting the patch.


Modified: django/trunk/django/contrib/sites/management.py
===================================================================
--- django/trunk/django/contrib/sites/management.py     2012-01-06 17:15:15 UTC 
(rev 17342)
+++ django/trunk/django/contrib/sites/management.py     2012-01-07 08:53:36 UTC 
(rev 17343)
@@ -3,22 +3,34 @@
 """
 
 from django.db.models import signals
+from django.db import connections
 from django.db import router
 from django.contrib.sites.models import Site
 from django.contrib.sites import models as site_app
+from django.core.management.color import no_style
 
 def create_default_site(app, created_models, verbosity, db, **kwargs):
     # Only create the default sites in databases where Django created the table
     if Site in created_models and router.allow_syncdb(db, Site) :
-        if verbosity >= 2:
-            print "Creating example.com Site object"
         # The default settings set SITE_ID = 1, and some tests in Django's test
         # suite rely on this value. However, if database sequences are reused
         # (e.g. in the test suite after flush/syncdb), it isn't guaranteed that
         # the next id will be 1, so we coerce it. See #15573 and #16353. This
         # can also crop up outside of tests - see #15346.
-        s = Site(pk=1, domain="example.com", name="example.com")
-        s.save(using=db)
+        if verbosity >= 2:
+            print "Creating example.com Site object"
+        Site(pk=1, domain="example.com", name="example.com").save(using=db)
+
+        # We set an explicit pk instead of relying on auto-incrementation,
+        # so we need to reset the database sequence. See #17415.
+        sequence_sql = connections[db].ops.sequence_reset_sql(no_style(), 
[Site])
+        if sequence_sql:
+            if verbosity >= 2:
+                print "Resetting sequence"
+            cursor = connections[db].cursor()
+            for command in sequence_sql:
+                cursor.execute(command)
+
     Site.objects.clear_cache()
 
 signals.post_syncdb.connect(create_default_site, sender=site_app)

Modified: django/trunk/django/contrib/sites/tests.py
===================================================================
--- django/trunk/django/contrib/sites/tests.py  2012-01-06 17:15:15 UTC (rev 
17342)
+++ django/trunk/django/contrib/sites/tests.py  2012-01-07 08:53:36 UTC (rev 
17343)
@@ -15,6 +15,12 @@
     def tearDown(self):
         Site._meta.installed = self.old_Site_meta_installed
 
+    def test_save_another(self):
+        # Regression for #17415
+        # On some backends the sequence needs reset after save with explicit 
ID.
+        # Test that there is no sequence collisions by saving another site.
+        Site(domain="example2.com", name="example2.com").save()
+
     def test_site_manager(self):
         # Make sure that get_current() does not return a deleted Site object.
         s = Site.objects.get_current()

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to