Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17343 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 4629668ffacddb0b8ddb5d8cc7fb8d01f039a4e3 1 parent 33f839b
Aymeric Augustin authored January 07, 2012
20  django/contrib/sites/management.py
@@ -3,22 +3,34 @@
3 3
 """
4 4
 
5 5
 from django.db.models import signals
  6
+from django.db import connections
6 7
 from django.db import router
7 8
 from django.contrib.sites.models import Site
8 9
 from django.contrib.sites import models as site_app
  10
+from django.core.management.color import no_style
9 11
 
10 12
 def create_default_site(app, created_models, verbosity, db, **kwargs):
11 13
     # Only create the default sites in databases where Django created the table
12 14
     if Site in created_models and router.allow_syncdb(db, Site) :
13  
-        if verbosity >= 2:
14  
-            print "Creating example.com Site object"
15 15
         # The default settings set SITE_ID = 1, and some tests in Django's test
16 16
         # suite rely on this value. However, if database sequences are reused
17 17
         # (e.g. in the test suite after flush/syncdb), it isn't guaranteed that
18 18
         # the next id will be 1, so we coerce it. See #15573 and #16353. This
19 19
         # can also crop up outside of tests - see #15346.
20  
-        s = Site(pk=1, domain="example.com", name="example.com")
21  
-        s.save(using=db)
  20
+        if verbosity >= 2:
  21
+            print "Creating example.com Site object"
  22
+        Site(pk=1, domain="example.com", name="example.com").save(using=db)
  23
+
  24
+        # We set an explicit pk instead of relying on auto-incrementation,
  25
+        # so we need to reset the database sequence. See #17415.
  26
+        sequence_sql = connections[db].ops.sequence_reset_sql(no_style(), [Site])
  27
+        if sequence_sql:
  28
+            if verbosity >= 2:
  29
+                print "Resetting sequence"
  30
+            cursor = connections[db].cursor()
  31
+            for command in sequence_sql:
  32
+                cursor.execute(command)
  33
+
22 34
     Site.objects.clear_cache()
23 35
 
24 36
 signals.post_syncdb.connect(create_default_site, sender=site_app)
6  django/contrib/sites/tests.py
@@ -15,6 +15,12 @@ def setUp(self):
15 15
     def tearDown(self):
16 16
         Site._meta.installed = self.old_Site_meta_installed
17 17
 
  18
+    def test_save_another(self):
  19
+        # Regression for #17415
  20
+        # On some backends the sequence needs reset after save with explicit ID.
  21
+        # Test that there is no sequence collisions by saving another site.
  22
+        Site(domain="example2.com", name="example2.com").save()
  23
+
18 24
     def test_site_manager(self):
19 25
         # Make sure that get_current() does not return a deleted Site object.
20 26
         s = Site.objects.get_current()

0 notes on commit 4629668

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