Ticket #16961: ticket_16961.diff

File ticket_16961.diff, 5.7 KB (added by Anssi Kääriäinen, 12 years ago)
  • django/db/backends/mysql/base.py

    diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
    index faaefca..e352ac9 100644
    a b class DatabaseOperations(BaseDatabaseOperations):  
    258258                sql.append('%s %s;' % (style.SQL_KEYWORD('TRUNCATE'), style.SQL_FIELD(self.quote_name(table))))
    259259            sql.append('SET FOREIGN_KEY_CHECKS = 1;')
    260260
    261             # 'ALTER TABLE table AUTO_INCREMENT = 1;'... style SQL statements
    262             # to reset sequence indices
    263             sql.extend(["%s %s %s %s %s;" % \
    264                 (style.SQL_KEYWORD('ALTER'),
    265                  style.SQL_KEYWORD('TABLE'),
    266                  style.SQL_TABLE(self.quote_name(sequence['table'])),
    267                  style.SQL_KEYWORD('AUTO_INCREMENT'),
    268                  style.SQL_FIELD('= 1'),
    269                 ) for sequence in sequences])
     261            # Truncate already resets the AUTO_INCREMENT field from
     262            # MySQL version 5.0.13 onwards. Refs #16961.
     263            if self.connection.get_server_version() < (5,0,13):
     264                sql.extend(
     265                    ["%s %s %s %s %s;" % \
     266                     (style.SQL_KEYWORD('ALTER'),
     267                      style.SQL_KEYWORD('TABLE'),
     268                      style.SQL_TABLE(self.quote_name(sequence['table'])),
     269                      style.SQL_KEYWORD('AUTO_INCREMENT'),
     270                      style.SQL_FIELD('= 1'),
     271                     ) for sequence in sequences])
    270272            return sql
    271273        else:
    272274            return []
  • tests/regressiontests/backends/tests.py

    diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
    index d5b1ea8..3b04780 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.get_server_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