2008-08-12 14:31:29 +08:00
|
|
|
"""
|
|
|
|
Extracts the version of the PostgreSQL server.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
2009-05-10 17:22:06 +08:00
|
|
|
# This reg-exp is intentionally fairly flexible here.
|
|
|
|
# Needs to be able to handle stuff like:
|
2014-07-27 04:55:31 +08:00
|
|
|
# PostgreSQL #.#.#
|
|
|
|
# EnterpriseDB #.#
|
|
|
|
# PostgreSQL #.# beta#
|
|
|
|
# PostgreSQL #.#beta#
|
2009-05-10 17:22:06 +08:00
|
|
|
VERSION_RE = re.compile(r'\S+ (\d+)\.(\d+)\.?(\d+)?')
|
|
|
|
|
2011-04-02 16:39:08 +08:00
|
|
|
|
2009-05-10 17:22:06 +08:00
|
|
|
def _parse_version(text):
|
|
|
|
"Internal parsing method. Factored out for testing purposes."
|
|
|
|
major, major2, minor = VERSION_RE.search(text).groups()
|
|
|
|
try:
|
2011-06-20 02:00:09 +08:00
|
|
|
return int(major) * 10000 + int(major2) * 100 + int(minor)
|
2009-05-10 17:22:06 +08:00
|
|
|
except (ValueError, TypeError):
|
2013-07-08 08:39:54 +08:00
|
|
|
return int(major) * 10000 + int(major2) * 100
|
|
|
|
|
2008-08-12 14:31:29 +08:00
|
|
|
|
2011-06-20 02:00:09 +08:00
|
|
|
def get_version(connection):
|
2008-08-12 14:31:29 +08:00
|
|
|
"""
|
2011-06-20 02:00:09 +08:00
|
|
|
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.)
|
|
|
|
|
2014-07-27 04:55:31 +08:00
|
|
|
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
|
2011-06-20 02:00:09 +08:00
|
|
|
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.
|
2008-08-12 14:31:29 +08:00
|
|
|
"""
|
2011-06-20 02:00:09 +08:00
|
|
|
if hasattr(connection, 'server_version'):
|
|
|
|
return connection.server_version
|
|
|
|
else:
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
cursor.execute("SELECT version()")
|
|
|
|
return _parse_version(cursor.fetchone()[0])
|