Skip to content

Commit dd5e718

Browse files
committed
[soc2010/query-refactor] Merged up to trunk r13366.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/query-refactor@13367 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 72c6a43 commit dd5e718

File tree

14 files changed

+164
-25
lines changed

14 files changed

+164
-25
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ 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>
223224
Tareque Hossain <http://www.codexn.com>
224225
Richard House <Richard.House@i-logue.com>
225226
Robert Rock Howard <http://djangomojo.com/>

django/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = (1, 2, 1, 'final', 0)
1+
VERSION = (1, 3, 0, 'alpha', 0)
22

33
def get_version():
44
version = '%s.%s' % (VERSION[0], VERSION[1])

django/contrib/markup/tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_textile(self):
2222
t = Template("{{ textile_content|textile }}")
2323
rendered = t.render(Context(locals())).strip()
2424
if textile:
25-
self.assertEqual(rendered, """<p>Paragraph 1</p>
25+
self.assertEqual(rendered.replace('\t', ''), """<p>Paragraph 1</p>
2626
2727
<p>Paragraph 2 with &#8220;quotes&#8221; and <code>code</code></p>""")
2828
else:

django/core/management/commands/testserver.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class Command(BaseCommand):
66
option_list = BaseCommand.option_list + (
7+
make_option('--noinput', action='store_false', dest='interactive', default=True,
8+
help='Tells Django to NOT prompt the user for input of any kind.'),
79
make_option('--addrport', action='store', dest='addrport',
810
type='string', default='',
911
help='port number or ipaddr:port to run the server on'),
@@ -18,10 +20,11 @@ def handle(self, *fixture_labels, **options):
1820
from django.db import connection
1921

2022
verbosity = int(options.get('verbosity', 1))
23+
interactive = options.get('interactive', True)
2124
addrport = options.get('addrport')
2225

2326
# Create a test database.
24-
db_name = connection.creation.create_test_db(verbosity=verbosity)
27+
db_name = connection.creation.create_test_db(verbosity=verbosity, autoclobber=not interactive)
2528

2629
# Import the fixture data into the test database.
2730
call_command('loaddata', *fixture_labels, **{'verbosity': verbosity})

django/db/backends/postgresql/creation.py

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

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

5253
def get_index_sql(index_name, opclass=''):
5354
return (style.SQL_KEYWORD('CREATE INDEX') + ' ' +
54-
style.SQL_TABLE(qn(index_name)) + ' ' +
55+
style.SQL_TABLE(qn(truncate_name(index_name,self.connection.ops.max_name_length()))) + ' ' +
5556
style.SQL_KEYWORD('ON') + ' ' +
5657
style.SQL_TABLE(qn(db_table)) + ' ' +
5758
"(%s%s)" % (style.SQL_FIELD(qn(f.column)), opclass) +

django/db/backends/postgresql/operations.py

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

5656
def last_insert_id(self, cursor, table_name, pk_name):
57-
cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (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))
5860
return cursor.fetchone()[0]
5961

6062
def no_limit_value(self):
@@ -90,13 +92,14 @@ def sql_flush(self, style, tables, sequences):
9092
for sequence_info in sequences:
9193
table_name = sequence_info['table']
9294
column_name = sequence_info['column']
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);" % \
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);" % \
98100
(style.SQL_KEYWORD('SELECT'),
99-
style.SQL_FIELD(self.quote_name(sequence_name)))
101+
style.SQL_TABLE(table_name),
102+
style.SQL_FIELD(column_name))
100103
)
101104
return sql
102105
else:
@@ -110,11 +113,15 @@ def sequence_reset_sql(self, style, model_list):
110113
# Use `coalesce` to set the sequence for each model to the max pk value if there are records,
111114
# or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true
112115
# 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+
113119
for f in model._meta.local_fields:
114120
if isinstance(f, models.AutoField):
115-
output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
121+
output.append("%s setval(pg_get_serial_sequence('%s','%s'), coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
116122
(style.SQL_KEYWORD('SELECT'),
117-
style.SQL_FIELD(qn('%s_%s_seq' % (model._meta.db_table, f.column))),
123+
style.SQL_TABLE(model._meta.db_table),
124+
style.SQL_FIELD(f.column),
118125
style.SQL_FIELD(qn(f.column)),
119126
style.SQL_FIELD(qn(f.column)),
120127
style.SQL_KEYWORD('IS NOT'),
@@ -123,9 +130,10 @@ def sequence_reset_sql(self, style, model_list):
123130
break # Only one AutoField is allowed per model, so don't bother continuing.
124131
for f in model._meta.many_to_many:
125132
if not f.rel.through:
126-
output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
133+
output.append("%s setval(pg_get_serial_sequence('%s','%s'), coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
127134
(style.SQL_KEYWORD('SELECT'),
128-
style.SQL_FIELD(qn('%s_id_seq' % f.m2m_db_table())),
135+
style.SQL_TABLE(model._meta.db_table),
136+
style.SQL_FIELD('id'),
129137
style.SQL_FIELD(qn('id')),
130138
style.SQL_FIELD(qn('id')),
131139
style.SQL_KEYWORD('IS NOT'),

docs/ref/databases.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ documentation or reference manuals.
1818
PostgreSQL notes
1919
================
2020

21+
.. versionchanged:: 1.3
22+
23+
Django supports PostgreSQL 8.0 and higher. If you want to use
24+
:ref:`database-level autocommit <postgresql-autocommit-mode>`, a
25+
minimum version of PostgreSQL 8.2 is required.
26+
27+
.. admonition:: Improvements in recent PostgreSQL versions
28+
29+
PostgreSQL 8.0 and 8.1 `will soon reach end-of-life`_; there have
30+
also been a number of significant performance improvements added
31+
in recent PostgreSQL versions. Although PostgreSQL 8.0 is the minimum
32+
supported version, you would be well advised to use a more recent
33+
version if at all possible.
34+
35+
.. _will soon reach end-of-life: http://wiki.postgresql.org/wiki/PostgreSQL_Release_Support_Policy
36+
2137
PostgreSQL 8.2 to 8.2.4
2238
-----------------------
2339

@@ -39,6 +55,8 @@ database connection is first used and commits the result at the end of the
3955
request/response handling. The PostgreSQL backends normally operate the same
4056
as any other Django backend in this respect.
4157

58+
.. _postgresql-autocommit-mode:
59+
4260
Autocommit mode
4361
~~~~~~~~~~~~~~~
4462

@@ -84,6 +102,7 @@ protection for multi-call operations.
84102

85103
Indexes for ``varchar`` and ``text`` columns
86104
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
105+
87106
.. versionadded:: 1.1.2
88107

89108
When specifying ``db_index=True`` on your model fields, Django typically

docs/ref/django-admin.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,6 @@ with an appropriate extension (e.g. ``json`` or ``xml``). See the
804804
documentation for ``loaddata`` for details on the specification of fixture
805805
data files.
806806

807-
--noinput
808-
~~~~~~~~~
809807
The :djadminopt:`--noinput` option may be provided to suppress all user
810808
prompts.
811809

@@ -889,6 +887,11 @@ To run on 1.2.3.4:7000 with a ``test`` fixture::
889887

890888
django-admin.py testserver --addrport 1.2.3.4:7000 test
891889

890+
.. versionadded:: 1.3
891+
892+
The :djadminopt:`--noinput` option may be provided to suppress all user
893+
prompts.
894+
892895
validate
893896
--------
894897

docs/ref/models/instances.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ of to a specific field. You can access these errors with ``NON_FIELD_ERRORS``::
109109

110110
from django.core.validators import ValidationError, NON_FIELD_ERRORS
111111
try:
112-
article.full_clean():
112+
article.full_clean()
113113
except ValidationError, e:
114114
non_field_errors = e.message_dict[NON_FIELD_ERRORS]
115115

docs/ref/request-response.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ a subclass of dictionary. Exceptions are outlined here:
286286
.. method:: QueryDict.setdefault(key, default)
287287

288288
Just like the standard dictionary ``setdefault()`` method, except it uses
289-
``__setitem__`` internally.
289+
``__setitem__()`` internally.
290290

291291
.. method:: QueryDict.update(other_dict)
292292

@@ -305,7 +305,7 @@ a subclass of dictionary. Exceptions are outlined here:
305305
.. method:: QueryDict.items()
306306

307307
Just like the standard dictionary ``items()`` method, except this uses the
308-
same last-value logic as ``__getitem()__``. For example::
308+
same last-value logic as ``__getitem__()``. For example::
309309

310310
>>> q = QueryDict('a=1&a=2&a=3')
311311
>>> q.items()
@@ -315,7 +315,7 @@ a subclass of dictionary. Exceptions are outlined here:
315315

316316
Just like the standard dictionary ``iteritems()`` method. Like
317317
:meth:`QueryDict.items()` this uses the same last-value logic as
318-
:meth:`QueryDict.__getitem()__`.
318+
:meth:`QueryDict.__getitem__()`.
319319

320320
.. method:: QueryDict.iterlists()
321321

@@ -325,7 +325,7 @@ a subclass of dictionary. Exceptions are outlined here:
325325
.. method:: QueryDict.values()
326326

327327
Just like the standard dictionary ``values()`` method, except this uses the
328-
same last-value logic as ``__getitem()__``. For example::
328+
same last-value logic as ``__getitem__()``. For example::
329329

330330
>>> q = QueryDict('a=1&a=2&a=3')
331331
>>> q.values()

docs/releases/1.3.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
.. _releases-1.3:
2+
3+
============================================
4+
Django 1.3 release notes - UNDER DEVELOPMENT
5+
============================================
6+
7+
This page documents release notes for the as-yet-unreleased Django
8+
1.3. As such, it's tentative and subject to change. It provides
9+
up-to-date information for those who are following trunk.
10+
11+
Django 1.3 includes a number of nifty `new features`_, lots of bug
12+
fixes and an easy upgrade path from Django 1.2.
13+
14+
.. _new features: `What's new in Django 1.3`_
15+
16+
.. _backwards-incompatible-changes-1.3:
17+
18+
Backwards-incompatible changes in 1.3
19+
=====================================
20+
21+
22+
23+
Features deprecated in 1.3
24+
==========================
25+
26+
27+
28+
What's new in Django 1.3
29+
========================
30+
31+

docs/releases/index.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ up to and including the new version.
1616
Final releases
1717
==============
1818

19+
1.3 release
20+
-----------
21+
.. toctree::
22+
:maxdepth: 1
23+
24+
1.3
25+
1926
1.2 release
2027
-----------
2128
.. toctree::

tests/regressiontests/backends/models.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from django.conf import settings
12
from django.db import models
2-
from django.db import connection
3+
from django.db import connection, DEFAULT_DB_ALIAS
4+
35

46
class Square(models.Model):
57
root = models.IntegerField()
@@ -8,18 +10,33 @@ class Square(models.Model):
810
def __unicode__(self):
911
return "%s ** 2 == %s" % (self.root, self.square)
1012

13+
1114
class Person(models.Model):
1215
first_name = models.CharField(max_length=20)
1316
last_name = models.CharField(max_length=20)
1417

1518
def __unicode__(self):
1619
return u'%s %s' % (self.first_name, self.last_name)
1720

21+
1822
class SchoolClass(models.Model):
1923
year = models.PositiveIntegerField()
2024
day = models.CharField(max_length=9, blank=True)
2125
last_updated = models.DateTimeField()
2226

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+
2340
qn = connection.ops.quote_name
2441

2542
__test__ = {'API_TESTS': """

tests/regressiontests/backends/tests.py

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

13+
from regressiontests.backends import models
14+
1115
class Callproc(unittest.TestCase):
1216

1317
def test_dbms_session(self):
@@ -76,6 +80,7 @@ def test_django_extract(self):
7680
classes = models.SchoolClass.objects.filter(last_updated__day=20)
7781
self.assertEqual(len(classes), 1)
7882

83+
7984
class ParameterHandlingTest(TestCase):
8085
def test_bad_parameter_count(self):
8186
"An executemany call with too many/not enough parameters will raise an exception (Refs #12612)"
@@ -88,6 +93,50 @@ def test_bad_parameter_count(self):
8893
self.assertRaises(Exception, cursor.executemany, query, [(1,2,3),])
8994
self.assertRaises(Exception, cursor.executemany, query, [(1,),])
9095

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+
91140

92141
def connection_created_test(sender, **kwargs):
93142
print 'connection_created signal'

0 commit comments

Comments
 (0)