Code

Ticket #10842: fix_unify_pgsql_re-option_a-django-1.1-dev.diff

File fix_unify_pgsql_re-option_a-django-1.1-dev.diff, 3.3 KB (added by hgdeoro, 5 years ago)
Line 
1diff --git a/django/db/backends/postgresql/operations.py b/django/db/backends/postgresql/operations.py
2index 3582460..0c40a58 100644
3--- a/django/db/backends/postgresql/operations.py
4+++ b/django/db/backends/postgresql/operations.py
5@@ -2,8 +2,6 @@ import re
6 
7 from django.db.backends import BaseDatabaseOperations
8 
9-server_version_re = re.compile(r'PostgreSQL (\d{1,2})\.(\d{1,2})\.?(\d{1,2})?')
10-
11 # This DatabaseOperations class lives in here instead of base.py because it's
12 # used by both the 'postgresql' and 'postgresql_psycopg2' backends.
13 
14@@ -14,13 +12,9 @@ class DatabaseOperations(BaseDatabaseOperations):
15     def _get_postgres_version(self):
16         if self._postgres_version is None:
17             from django.db import connection
18+            from django.db.backends.postgresql.version import get_version
19             cursor = connection.cursor()
20-            cursor.execute("SELECT version()")
21-            version_string = cursor.fetchone()[0]
22-            m = server_version_re.match(version_string)
23-            if not m:
24-                raise Exception('Unable to determine PostgreSQL version from version() function string: %r' % version_string)
25-            self._postgres_version = [int(val) for val in m.groups() if val]
26+            self._postgres_version = get_version(cursor, precision=3)
27         return self._postgres_version
28     postgres_version = property(_get_postgres_version)
29 
30@@ -157,5 +151,6 @@ class DatabaseOperations(BaseDatabaseOperations):
31         NotImplementedError if this is the database in use.
32         """
33         if aggregate.sql_function == 'STDDEV_POP' or aggregate.sql_function == 'VAR_POP':
34-            if self.postgres_version[0] == 8 and self.postgres_version[1] == 2 and self.postgres_version[2] <= 4:
35+            if self.postgres_version[0] == 8 and self.postgres_version[1] == 2 and \
36+                    (self.postgres_version[2] is None or self.postgres_version[2] <= 4):
37                 raise NotImplementedError('PostgreSQL 8.2 to 8.2.4 is known to have a faulty implementation of %s. Please upgrade your version of PostgreSQL.' % aggregate.sql_function)
38diff --git a/django/db/backends/postgresql/version.py b/django/db/backends/postgresql/version.py
39index ed2ad9e..102ee03 100644
40--- a/django/db/backends/postgresql/version.py
41+++ b/django/db/backends/postgresql/version.py
42@@ -9,15 +9,24 @@ import re
43 #   PostgreSQL 8.3.6
44 #   EnterpriseDB 8.3
45 #   PostgreSQL 8.3 beta4
46-VERSION_RE = re.compile(r'\S+ (\d+)\.(\d+)')
47+#   PostgreSQL 8.4beta1
48+VERSION_RE = re.compile(r'\S+ (\d+)\.(\d+)\.?(\d+)?')
49 
50-def get_version(cursor):
51+def get_version(cursor, precision=2):
52     """
53     Returns a tuple representing the major and minor version number of the
54     server. For example, (7, 4) or (8, 3).
55     """
56     cursor.execute("SELECT version()")
57     version = cursor.fetchone()[0]
58-    major, minor = VERSION_RE.search(version).groups()
59-    return int(major), int(minor)
60-
61+    major, minor, minor2 = VERSION_RE.search(version).groups()
62+    if precision == 1:
63+        return major
64+    elif precision == 2:
65+        return int(major), int(minor)
66+    else:
67+        try:
68+            return int(major), int(minor), int(minor2)
69+        except (ValueError, TypeError):
70+           #raise Exception('Unable to determine PostgreSQL version from version() function string: %r' % version)
71+            return int(major), int(minor), None