summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql/version.py
diff options
context:
space:
mode:
authorCaio Ariede <caio.ariede@gmail.com>2015-08-05 11:08:56 -0300
committerTim Graham <timograham@gmail.com>2015-08-07 09:33:17 -0400
commitec9004728ee136e3b7e2b7cd2610203e16b6ce9b (patch)
treebd3fd3d8c729e5e01fc3111696a7d4eaa56d85b0 /django/db/backends/postgresql/version.py
parent8656cfc4e01332426e5e4b78c20a4e9ec443b293 (diff)
downloaddjango-ec9004728ee136e3b7e2b7cd2610203e16b6ce9b.tar.gz
Fixed #25175 -- Renamed the postgresql_psycopg2 database backend to postgresql.
Diffstat (limited to 'django/db/backends/postgresql/version.py')
-rw-r--r--django/db/backends/postgresql/version.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/django/db/backends/postgresql/version.py b/django/db/backends/postgresql/version.py
new file mode 100644
index 0000000000..d558fb2e51
--- /dev/null
+++ b/django/db/backends/postgresql/version.py
@@ -0,0 +1,44 @@
+"""
+Extracts the version of the PostgreSQL server.
+"""
+
+import re
+
+# This reg-exp is intentionally fairly flexible here.
+# Needs to be able to handle stuff like:
+# PostgreSQL #.#.#
+# EnterpriseDB #.#
+# PostgreSQL #.# beta#
+# PostgreSQL #.#beta#
+VERSION_RE = re.compile(r'\S+ (\d+)\.(\d+)\.?(\d+)?')
+
+
+def _parse_version(text):
+ "Internal parsing method. Factored out for testing purposes."
+ major, major2, minor = VERSION_RE.search(text).groups()
+ try:
+ return int(major) * 10000 + int(major2) * 100 + int(minor)
+ except (ValueError, TypeError):
+ return int(major) * 10000 + int(major2) * 100
+
+
+def get_version(connection):
+ """
+ Returns an integer representing the major, minor and revision number of the
+ server. Format is the one used for the return value of libpq
+ PQServerVersion()/``server_version`` connection attribute (available in
+ newer psycopg2 versions.)
+
+ For example, 90304 for 9.3.4. The last two digits will be 00 in the case of
+ releases (e.g., 90400 for 'PostgreSQL 9.4') or in the case of beta and
+ prereleases (e.g. 90100 for 'PostgreSQL 9.1beta2').
+
+ PQServerVersion()/``server_version`` doesn't execute a query so try that
+ first, then fallback to a ``SELECT version()`` query.
+ """
+ if hasattr(connection, 'server_version'):
+ return connection.server_version
+ else:
+ with connection.cursor() as cursor:
+ cursor.execute("SELECT version()")
+ return _parse_version(cursor.fetchone()[0])