Ticket #16961: ticket_16961.3.diff

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

4th time must work.

  • django/db/backends/mysql/base.py

    diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
    index c1ef625..bacd998 100644
    a b class DatabaseOperations(BaseDatabaseOperations): 
    261261                sql.append('%s %s;' % (style.SQL_KEYWORD('TRUNCATE'), style.SQL_FIELD(self.quote_name(table))))
    262262            sql.append('SET FOREIGN_KEY_CHECKS = 1;')
    263263
    264             # 'ALTER TABLE table AUTO_INCREMENT = 1;'... style SQL statements
    265             # to reset sequence indices
    266             sql.extend(["%s %s %s %s %s;" % \
    267                 (style.SQL_KEYWORD('ALTER'),
    268                  style.SQL_KEYWORD('TABLE'),
    269                  style.SQL_TABLE(self.quote_name(sequence['table'])),
    270                  style.SQL_KEYWORD('AUTO_INCREMENT'),
    271                  style.SQL_FIELD('= 1'),
    272                 ) for sequence in sequences])
     264            # Truncate already resets the AUTO_INCREMENT field from
     265            # MySQL version 5.0.13 onwards. Refs #16961.
     266            if self.connection.mysql_version < (5,0,13):
     267                sql.extend(
     268                    ["%s %s %s %s %s;" % \
     269                     (style.SQL_KEYWORD('ALTER'),
     270                      style.SQL_KEYWORD('TABLE'),
     271                      style.SQL_TABLE(self.quote_name(sequence['table'])),
     272                      style.SQL_KEYWORD('AUTO_INCREMENT'),
     273                      style.SQL_FIELD('= 1'),
     274                     ) for sequence in sequences])
    273275            return sql
    274276        else:
    275277            return []
  • tests/regressiontests/backends/tests.py

    diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
    index d5b1ea8..f87b9c4 100644
    a b class OracleChecks(unittest.TestCase): 
    6161    def test_client_encoding(self):
    6262        # If the backend is Oracle, test that the client encoding is set
    6363        # correctly.  This was broken under Cygwin prior to r14781.
    64         c = connection.cursor()  # Ensure the connection is initialized.
     64        connection.cursor()  # Ensure the connection is initialized.
    6565        self.assertEqual(connection.connection.encoding, "UTF-8")
    6666        self.assertEqual(connection.connection.nencoding, "UTF-8")
    6767
     68class MySQLTests(TestCase):
     69    @unittest.skipUnless(connection.vendor == 'mysql',
     70                        "Test valid only for MySQL")
     71    def test_autoincrement(self):
     72        """
     73        Check that auto_increment fields are reset correctly by sql_flush().
     74        Before MySQL version 5.0.13 TRUNCATE did not do auto_increment reset.
     75        Refs #16961.
     76        """
     77        statements = connection.ops.sql_flush(no_style(),
     78                                              tables=['test'],
     79                                              sequences=[{
     80                                                  'table': 'test',
     81                                                  'col': 'somecol',
     82                                              }])
     83        found_reset = False
     84        for sql in statements:
     85            found_reset = found_reset or 'ALTER TABLE' in sql
     86        if connection.mysql_version < (5,0,13):
     87            self.assertTrue(found_reset)
     88        else:
     89            self.assertFalse(found_reset)
     90
     91 
    6892class DateQuotingTest(TestCase):
    6993
    7094    def test_django_date_trunc(self):
  • tests/regressiontests/test_runner/models.py

    diff --git a/tests/regressiontests/test_runner/models.py b/tests/regressiontests/test_runner/models.py
    index e69de29..9a072e6 100644
    a b  
     1from django.db import models
     2
     3class Person(models.Model):
     4    first_name = models.CharField(max_length=20)
     5    last_name = models.CharField(max_length=20)
  • tests/regressiontests/test_runner/tests.py

    diff --git a/tests/regressiontests/test_runner/tests.py b/tests/regressiontests/test_runner/tests.py
    index ccb65b4..3fc8d7f 100644
    a b from optparse import make_option 
    88from django.core.exceptions import ImproperlyConfigured
    99from django.core.management import call_command
    1010from django import db
    11 from django.test import simple
     11from django.db import connection
     12from django.test import simple, TransactionTestCase
    1213from django.test.simple import DjangoTestSuiteRunner, get_tests
    1314from django.test.testcases import connections_support_transactions
    1415from django.utils import unittest
    1516from django.utils.importlib import import_module
    1617
    1718from ..admin_scripts.tests import AdminScriptTestCase
     19from .models import Person
    1820
    1921
    2022TEST_APP_OK = 'regressiontests.test_runner.valid_app.models'
    class Sqlite3InMemoryTestDbs(unittest.TestCase): 
    262264                self.assertTrue(connections_support_transactions(), msg)
    263265            finally:
    264266                db.connections = old_db_connections
     267
     268
     269class AutoIncrementResetTest(TransactionTestCase):
     270    """
     271    Here we test creating the same model two times in different test methods,
     272    and check that both times they get "1" as their PK value. That is, we test
     273    that AutoField values start from 1 for each transactional test case.
     274    """
     275    @unittest.skipIf(connection.vendor == 'oracle',
     276                     "Oracle's auto-increment fields are not reset between "
     277                     "tests")
     278    def test_autoincrement_reset1(self):
     279        p = Person.objects.create(first_name='Jack', last_name='Smith')
     280        self.assertEquals(p.pk, 1)
     281   
     282    @unittest.skipIf(connection.vendor == 'oracle',
     283                     "Oracle's auto-increment fields are not reset between "
     284                     "tests")
     285    def test_autoincrement_reset2(self):
     286        p = Person.objects.create(first_name='Jack', last_name='Smith')
     287        self.assertEquals(p.pk, 1)
Back to Top