Skip to content

Commit 4f395e7

Browse files
committed
[soc2010/query-refactor] Merged up to trunk r13350.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/query-refactor@13351 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 28499bb commit 4f395e7

File tree

6 files changed

+34
-105
lines changed

6 files changed

+34
-105
lines changed

AUTHORS

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ answer newbie questions, and generally made Django that much better:
220220
Kieran Holland <http://www.kieranholland.com>
221221
Sung-Jin Hong <serialx.net@gmail.com>
222222
Leo "hylje" Honkanen <sealage@gmail.com>
223-
Matt Hoskins <skaffenuk@googlemail.com>
224223
Tareque Hossain <http://www.codexn.com>
225224
Richard House <Richard.House@i-logue.com>
226225
Robert Rock Howard <http://djangomojo.com/>

django/db/backends/postgresql/creation.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from django.db.backends.creation import BaseDatabaseCreation
2-
from django.db.backends.util import truncate_name
32

43
class DatabaseCreation(BaseDatabaseCreation):
54
# This dictionary maps Field objects to their associated PostgreSQL column
@@ -52,7 +51,7 @@ def sql_indexes_for_field(self, model, f, style):
5251

5352
def get_index_sql(index_name, opclass=''):
5453
return (style.SQL_KEYWORD('CREATE INDEX') + ' ' +
55-
style.SQL_TABLE(qn(truncate_name(index_name,self.connection.ops.max_name_length()))) + ' ' +
54+
style.SQL_TABLE(qn(index_name)) + ' ' +
5655
style.SQL_KEYWORD('ON') + ' ' +
5756
style.SQL_TABLE(qn(db_table)) + ' ' +
5857
"(%s%s)" % (style.SQL_FIELD(qn(f.column)), opclass) +

django/db/backends/postgresql/operations.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ def field_cast_sql(self, db_type):
5454
return '%s'
5555

5656
def last_insert_id(self, cursor, table_name, pk_name):
57-
# Use pg_get_serial_sequence to get the underlying sequence name
58-
# from the table name and column name (available since PostgreSQL 8)
59-
cursor.execute("SELECT CURRVAL(pg_get_serial_sequence('%s','%s'))" % (table_name, pk_name))
57+
cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name))
6058
return cursor.fetchone()[0]
6159

6260
def no_limit_value(self):
@@ -92,14 +90,13 @@ def sql_flush(self, style, tables, sequences):
9290
for sequence_info in sequences:
9391
table_name = sequence_info['table']
9492
column_name = sequence_info['column']
95-
if not (column_name and len(column_name) > 0):
96-
# This will be the case if it's an m2m using an autogenerated
97-
# intermediate table (see BaseDatabaseIntrospection.sequence_list)
98-
column_name = 'id'
99-
sql.append("%s setval(pg_get_serial_sequence('%s','%s'), 1, false);" % \
93+
if column_name and len(column_name) > 0:
94+
sequence_name = '%s_%s_seq' % (table_name, column_name)
95+
else:
96+
sequence_name = '%s_id_seq' % table_name
97+
sql.append("%s setval('%s', 1, false);" % \
10098
(style.SQL_KEYWORD('SELECT'),
101-
style.SQL_TABLE(table_name),
102-
style.SQL_FIELD(column_name))
99+
style.SQL_FIELD(self.quote_name(sequence_name)))
103100
)
104101
return sql
105102
else:
@@ -113,15 +110,11 @@ def sequence_reset_sql(self, style, model_list):
113110
# Use `coalesce` to set the sequence for each model to the max pk value if there are records,
114111
# or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true
115112
# if there are records (as the max pk value is already in use), otherwise set it to false.
116-
# Use pg_get_serial_sequence to get the underlying sequence name from the table name
117-
# and column name (available since PostgreSQL 8)
118-
119113
for f in model._meta.local_fields:
120114
if isinstance(f, models.AutoField):
121-
output.append("%s setval(pg_get_serial_sequence('%s','%s'), coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
115+
output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
122116
(style.SQL_KEYWORD('SELECT'),
123-
style.SQL_TABLE(model._meta.db_table),
124-
style.SQL_FIELD(f.column),
117+
style.SQL_FIELD(qn('%s_%s_seq' % (model._meta.db_table, f.column))),
125118
style.SQL_FIELD(qn(f.column)),
126119
style.SQL_FIELD(qn(f.column)),
127120
style.SQL_KEYWORD('IS NOT'),
@@ -130,10 +123,9 @@ def sequence_reset_sql(self, style, model_list):
130123
break # Only one AutoField is allowed per model, so don't bother continuing.
131124
for f in model._meta.many_to_many:
132125
if not f.rel.through:
133-
output.append("%s setval(pg_get_serial_sequence('%s','%s'), coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
126+
output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
134127
(style.SQL_KEYWORD('SELECT'),
135-
style.SQL_TABLE(model._meta.db_table),
136-
style.SQL_FIELD('id'),
128+
style.SQL_FIELD(qn('%s_id_seq' % f.m2m_db_table())),
137129
style.SQL_FIELD(qn('id')),
138130
style.SQL_FIELD(qn('id')),
139131
style.SQL_KEYWORD('IS NOT'),

docs/releases/1.2.txt

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ Overview
2121
Django 1.2 introduces several large, important new features, including:
2222

2323
* Support for `multiple database connections`_ in a single Django instance.
24-
24+
2525
* `Model validation`_ inspired by Django's form validation.
26-
26+
2727
* Vastly `improved protection against Cross-Site Request Forgery`_ (CSRF).
28-
28+
2929
* A new `user "messages" framework`_ with support for cookie- and session-based
3030
message for both anonymous and authenticated users.
3131

@@ -49,9 +49,9 @@ be found below`_.
4949

5050
.. seealso::
5151

52-
`Django Advent`_ covered the release of Django 1.2 with a series of
52+
`Django Advent`_ covered the release of Django 1.2 with a series of
5353
articles and tutorials that cover some of the new features in depth.
54-
54+
5555
.. _django advent: http://djangoadvent.com/
5656

5757
Wherever possible these features have been introduced in a backwards-compatible
@@ -66,20 +66,20 @@ backwards-incompatible. The big changes are:
6666
* The new CSRF protection framework is not backwards-compatible with
6767
the old system. Users of the old system will not be affected until
6868
the old system is removed in Django 1.4.
69-
69+
7070
However, upgrading to the new CSRF protection framework requires a few
7171
important backwards-incompatible changes, detailed in `CSRF Protection`_,
7272
below.
7373

7474
* Authors of custom :class:`~django.db.models.Field` subclasses should be
7575
aware that a number of methods have had a change in prototype, detailed
7676
under `get_db_prep_*() methods on Field`_, below.
77-
77+
7878
* The internals of template tags have changed somewhat; authors of custom
7979
template tags that need to store state (e.g. custom control flow tags)
8080
should ensure that their code follows the new rules for `stateful template
8181
tags`_
82-
82+
8383
* The :func:`~django.contrib.auth.decorators.user_passes_test`,
8484
:func:`~django.contrib.auth.decorators.login_required`, and
8585
:func:`~django.contrib.auth.decorators.permission_required`, decorators
@@ -435,6 +435,8 @@ database-compatible values. A custom field might look something like::
435435

436436
class CustomModelField(models.Field):
437437
# ...
438+
def db_type(self):
439+
# ...
438440

439441
def get_db_prep_save(self, value):
440442
# ...
@@ -451,6 +453,9 @@ two extra methods have been introduced::
451453
class CustomModelField(models.Field):
452454
# ...
453455

456+
def db_type(self, connection):
457+
# ...
458+
454459
def get_prep_value(self, value):
455460
# ...
456461

@@ -467,10 +472,10 @@ two extra methods have been introduced::
467472
# ...
468473

469474
These changes are required to support multiple databases --
470-
``get_db_prep_*`` can no longer make any assumptions regarding the
471-
database for which it is preparing. The ``connection`` argument now
472-
provides the preparation methods with the specific connection for
473-
which the value is being prepared.
475+
``db_type`` and ``get_db_prep_*`` can no longer make any assumptions
476+
regarding the database for which it is preparing. The ``connection``
477+
argument now provides the preparation methods with the specific
478+
connection for which the value is being prepared.
474479

475480
The two new methods exist to differentiate general data-preparation
476481
requirements from requirements that are database-specific. The
@@ -603,13 +608,13 @@ new keyword and so is not a valid variable name in this tag.
603608
--------------
604609

605610
``LazyObject`` is an undocumented-but-often-used utility class used for lazily
606-
wrapping other objects of unknown type.
611+
wrapping other objects of unknown type.
607612

608613
In Django 1.1 and earlier, it handled introspection in a non-standard way,
609614
depending on wrapped objects implementing a public method named
610615
``get_all_members()``. Since this could easily lead to name clashes, it has been
611616
changed to use the standard Python introspection method, involving
612-
``__members__`` and ``__dir__()``.
617+
``__members__`` and ``__dir__()``.
613618

614619
If you used ``LazyObject`` in your own code
615620
and implemented the ``get_all_members()`` method for wrapped objects, you'll need

tests/regressiontests/backends/models.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
from django.conf import settings
21
from django.db import models
3-
from django.db import connection, DEFAULT_DB_ALIAS
4-
2+
from django.db import connection
53

64
class Square(models.Model):
75
root = models.IntegerField()
@@ -10,33 +8,18 @@ class Square(models.Model):
108
def __unicode__(self):
119
return "%s ** 2 == %s" % (self.root, self.square)
1210

13-
1411
class Person(models.Model):
1512
first_name = models.CharField(max_length=20)
1613
last_name = models.CharField(max_length=20)
1714

1815
def __unicode__(self):
1916
return u'%s %s' % (self.first_name, self.last_name)
2017

21-
2218
class SchoolClass(models.Model):
2319
year = models.PositiveIntegerField()
2420
day = models.CharField(max_length=9, blank=True)
2521
last_updated = models.DateTimeField()
2622

27-
# Unfortunately, the following model breaks MySQL hard.
28-
# Until #13711 is fixed, this test can't be run under MySQL.
29-
if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql':
30-
class VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ(models.Model):
31-
class Meta:
32-
# We need to use a short actual table name or
33-
# we hit issue #8548 which we're not testing!
34-
verbose_name = 'model_with_long_table_name'
35-
primary_key_is_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.AutoField(primary_key=True)
36-
charfield_is_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.CharField(max_length=100)
37-
m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.ManyToManyField(Person,blank=True)
38-
39-
4023
qn = connection.ops.quote_name
4124

4225
__test__ = {'API_TESTS': """

tests/regressiontests/backends/tests.py

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
# -*- coding: utf-8 -*-
22
# Unit and doctests for specific database backends.
33
import datetime
4+
import models
45
import unittest
5-
6-
from django.conf import settings
7-
from django.core import management
8-
from django.core.management.color import no_style
96
from django.db import backend, connection, DEFAULT_DB_ALIAS
107
from django.db.backends.signals import connection_created
8+
from django.conf import settings
119
from django.test import TestCase
1210

13-
from regressiontests.backends import models
14-
1511
class Callproc(unittest.TestCase):
1612

1713
def test_dbms_session(self):
@@ -80,7 +76,6 @@ def test_django_extract(self):
8076
classes = models.SchoolClass.objects.filter(last_updated__day=20)
8177
self.assertEqual(len(classes), 1)
8278

83-
8479
class ParameterHandlingTest(TestCase):
8580
def test_bad_parameter_count(self):
8681
"An executemany call with too many/not enough parameters will raise an exception (Refs #12612)"
@@ -93,50 +88,6 @@ def test_bad_parameter_count(self):
9388
self.assertRaises(Exception, cursor.executemany, query, [(1,2,3),])
9489
self.assertRaises(Exception, cursor.executemany, query, [(1,),])
9590

96-
# Unfortunately, the following tests would be a good test to run on all
97-
# backends, but it breaks MySQL hard. Until #13711 is fixed, it can't be run
98-
# everywhere (although it would be an effective test of #13711).
99-
if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql':
100-
class LongNameTest(TestCase):
101-
"""Long primary keys and model names can result in a sequence name
102-
that exceeds the database limits, which will result in truncation
103-
on certain databases (e.g., Postgres). The backend needs to use
104-
the correct sequence name in last_insert_id and other places, so
105-
check it is. Refs #8901.
106-
"""
107-
108-
def test_sequence_name_length_limits_create(self):
109-
"""Test creation of model with long name and long pk name doesn't error. Ref #8901"""
110-
models.VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.objects.create()
111-
112-
def test_sequence_name_length_limits_m2m(self):
113-
"""Test an m2m save of a model with a long name and a long m2m field name doesn't error as on Django >=1.2 this now uses object saves. Ref #8901"""
114-
obj = models.VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.objects.create()
115-
rel_obj = models.Person.objects.create(first_name='Django', last_name='Reinhardt')
116-
obj.m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.add(rel_obj)
117-
118-
def test_sequence_name_length_limits_flush(self):
119-
"""Test that sequence resetting as part of a flush with model with long name and long pk name doesn't error. Ref #8901"""
120-
# A full flush is expensive to the full test, so we dig into the
121-
# internals to generate the likely offending SQL and run it manually
122-
123-
# Some convenience aliases
124-
VLM = models.VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
125-
VLM_m2m = VLM.m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.through
126-
tables = [
127-
VLM._meta.db_table,
128-
VLM_m2m._meta.db_table,
129-
]
130-
sequences = [
131-
{
132-
'column': VLM._meta.pk.column,
133-
'table': VLM._meta.db_table
134-
},
135-
]
136-
cursor = connection.cursor()
137-
for statement in connection.ops.sql_flush(no_style(), tables, sequences):
138-
cursor.execute(statement)
139-
14091

14192
def connection_created_test(sender, **kwargs):
14293
print 'connection_created signal'

0 commit comments

Comments
 (0)