Ticket #16961: ticket_16961.diff.3

File ticket_16961.diff.3, 5.7 KB (added by Anssi Kääriäinen, 6 years ago)

3rd try. Hopefully this diff now contains only relevant changes

Line 
1diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
2index c1ef625..bacd998 100644
3--- a/django/db/backends/mysql/base.py
4+++ b/django/db/backends/mysql/base.py
5@@ -261,15 +261,17 @@ class DatabaseOperations(BaseDatabaseOperations):
6                 sql.append('%s %s;' % (style.SQL_KEYWORD('TRUNCATE'), style.SQL_FIELD(self.quote_name(table))))
7             sql.append('SET FOREIGN_KEY_CHECKS = 1;')
8 
9-            # 'ALTER TABLE table AUTO_INCREMENT = 1;'... style SQL statements
10-            # to reset sequence indices
11-            sql.extend(["%s %s %s %s %s;" % \
12-                (style.SQL_KEYWORD('ALTER'),
13-                 style.SQL_KEYWORD('TABLE'),
14-                 style.SQL_TABLE(self.quote_name(sequence['table'])),
15-                 style.SQL_KEYWORD('AUTO_INCREMENT'),
16-                 style.SQL_FIELD('= 1'),
17-                ) for sequence in sequences])
18+            # Truncate already resets the AUTO_INCREMENT field from
19+            # MySQL version 5.0.13 onwards. Refs #16961.
20+            if self.connection.mysql_version < (5,0,13):
21+                sql.extend(
22+                    ["%s %s %s %s %s;" % \
23+                     (style.SQL_KEYWORD('ALTER'),
24+                      style.SQL_KEYWORD('TABLE'),
25+                      style.SQL_TABLE(self.quote_name(sequence['table'])),
26+                      style.SQL_KEYWORD('AUTO_INCREMENT'),
27+                      style.SQL_FIELD('= 1'),
28+                     ) for sequence in sequences])
29             return sql
30         else:
31             return []
32diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
33index d5b1ea8..f87b9c4 100644
34--- a/tests/regressiontests/backends/tests.py
35+++ b/tests/regressiontests/backends/tests.py
36@@ -61,10 +61,34 @@ class OracleChecks(unittest.TestCase):
37     def test_client_encoding(self):
38         # If the backend is Oracle, test that the client encoding is set
39         # correctly.  This was broken under Cygwin prior to r14781.
40-        c = connection.cursor()  # Ensure the connection is initialized.
41+        connection.cursor()  # Ensure the connection is initialized.
42         self.assertEqual(connection.connection.encoding, "UTF-8")
43         self.assertEqual(connection.connection.nencoding, "UTF-8")
44 
45+class MySQLTests(TestCase):
46+    @unittest.skipUnless(connection.vendor == 'mysql',
47+                        "Test valid only for MySQL")
48+    def test_autoincrement(self):
49+        """
50+        Check that auto_increment fields are reset correctly by sql_flush().
51+        Before MySQL version 5.0.13 TRUNCATE did not do auto_increment reset.
52+        Refs #16961.
53+        """
54+        statements = connection.ops.sql_flush(no_style(),
55+                                              tables=['test'],
56+                                              sequences=[{
57+                                                  'table': 'test',
58+                                                  'col': 'somecol',
59+                                              }])
60+        found_reset = False
61+        for sql in statements:
62+            found_reset = found_reset or 'ALTER TABLE' in sql
63+        if connection.mysql_version < (5,0,13):
64+            self.assertTrue(found_reset)
65+        else:
66+            self.assertFalse(found_reset)
67+
68+
69 class DateQuotingTest(TestCase):
70 
71     def test_django_date_trunc(self):
72diff --git a/tests/regressiontests/test_runner/models.py b/tests/regressiontests/test_runner/models.py
73index e69de29..9a072e6 100644
74--- a/tests/regressiontests/test_runner/models.py
75+++ b/tests/regressiontests/test_runner/models.py
76@@ -0,0 +1,5 @@
77+from django.db import models
78+
79+class Person(models.Model):
80+    first_name = models.CharField(max_length=20)
81+    last_name = models.CharField(max_length=20)
82diff --git a/tests/regressiontests/test_runner/tests.py b/tests/regressiontests/test_runner/tests.py
83index ccb65b4..3fc8d7f 100644
84--- a/tests/regressiontests/test_runner/tests.py
85+++ b/tests/regressiontests/test_runner/tests.py
86@@ -8,13 +8,15 @@ from optparse import make_option
87 from django.core.exceptions import ImproperlyConfigured
88 from django.core.management import call_command
89 from django import db
90-from django.test import simple
91+from django.db import connection
92+from django.test import simple, TransactionTestCase
93 from django.test.simple import DjangoTestSuiteRunner, get_tests
94 from django.test.testcases import connections_support_transactions
95 from django.utils import unittest
96 from django.utils.importlib import import_module
97 
98 from ..admin_scripts.tests import AdminScriptTestCase
99+from .models import Person
100 
101 
102 TEST_APP_OK = 'regressiontests.test_runner.valid_app.models'
103@@ -262,3 +264,24 @@ class Sqlite3InMemoryTestDbs(unittest.TestCase):
104                 self.assertTrue(connections_support_transactions(), msg)
105             finally:
106                 db.connections = old_db_connections
107+
108+
109+class AutoIncrementResetTest(TransactionTestCase):
110+    """
111+    Here we test creating the same model two times in different test methods,
112+    and check that both times they get "1" as their PK value. That is, we test
113+    that AutoField values start from 1 for each transactional test case.
114+    """
115+    @unittest.skipIf(connection.vendor == 'oracle',
116+                     "Oracle's auto-increment fields are not reset between "
117+                     "tests")
118+    def test_autoincrement_reset1(self):
119+        p = Person.objects.create(first_name='Jack', last_name='Smith')
120+        self.assertEquals(p.pk, 1)
121+   
122+    @unittest.skipIf(connection.vendor == 'oracle',
123+                     "Oracle's auto-increment fields are not reset between "
124+                     "tests")
125+    def test_autoincrement_reset2(self):
126+        p = Person.objects.create(first_name='Jack', last_name='Smith')
127+        self.assertEquals(p.pk, 1)
Back to Top