Skip to content

Commit

Permalink
Do not use savepoints with PostgreSQL prior to 8.0.
Browse files Browse the repository at this point in the history
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8317 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Aug 12, 2008
1 parent 97da73a commit 6bebb23
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions django/db/backends/postgresql/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.db.backends.postgresql.creation import DatabaseCreation
from django.db.backends.postgresql.introspection import DatabaseIntrospection
from django.db.backends.postgresql.operations import DatabaseOperations
from django.db.backends.postgresql.version import get_version
from django.utils.encoding import smart_str, smart_unicode

try:
Expand Down Expand Up @@ -115,6 +116,12 @@ def _cursor(self, settings):
cursor = self.connection.cursor()
if set_tz:
cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE])
if not hasattr(self, '_version'):
version = get_version(cursor)
self.__class__._version = version
if version < (8, 0):
# No savepoint support for earlier version of PostgreSQL.
self.features.uses_savepoints = False
cursor.execute("SET client_encoding to 'UNICODE'")
cursor = UnicodeCursorWrapper(cursor, 'utf-8')
return cursor
Expand Down
18 changes: 18 additions & 0 deletions django/db/backends/postgresql/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Extracts the version of the PostgreSQL server.
"""

import re

VERSION_RE = re.compile(r'PostgreSQL (\d+)\.(\d+)\.')

def get_version(cursor):
"""
Returns a tuple representing the major and minor version number of the
server. For example, (7, 4) or (8, 3).
"""
cursor.execute("SELECT version()")
version = cursor.fetchone()[0]
major, minor = VERSION_RE.search(version).groups()
return int(major), int(minor)

7 changes: 7 additions & 0 deletions django/db/backends/postgresql_psycopg2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.db.backends.postgresql.operations import DatabaseOperations as PostgresqlDatabaseOperations
from django.db.backends.postgresql.client import DatabaseClient
from django.db.backends.postgresql.creation import DatabaseCreation
from django.db.backends.postgresql.version import get_version
from django.db.backends.postgresql_psycopg2.introspection import DatabaseIntrospection

from django.utils.safestring import SafeUnicode
Expand Down Expand Up @@ -86,4 +87,10 @@ def _cursor(self, settings):
cursor.tzinfo_factory = None
if set_tz:
cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE])
if not hasattr(self, '_version'):
version = get_version(cursor)
self.__class__._version = version
if version < (8, 0):
# No savepoint support for earlier version of PostgreSQL.
self.features.uses_savepoints = False
return cursor

0 comments on commit 6bebb23

Please sign in to comment.