Don't expect (postgres) SERVER_VERSION to be M.N (#30417)

It could be something like '10beta4', which StrictVersion() would
reject. When Postgres 10 is released, it will be '10', which
StrictVersion() would STILL reject.

Fortunately, psycopg2 has a 'server_version' connection attribute that
is guaranteed to be an integer like 90605 for version 9.6.5, or 100000
for version 10. We can safely use this for version-specific code.
(cherry picked from commit 0addd53926)
pull/30434/merge
Abhijit Menon-Sen 7 years ago committed by Toshio Kuratomi
parent b14e0cbfe4
commit 91e1a5fe16

@ -207,7 +207,6 @@ EXAMPLES = '''
import itertools import itertools
import re import re
import traceback import traceback
from distutils.version import StrictVersion
from hashlib import md5 from hashlib import md5
try: try:
@ -225,7 +224,7 @@ from ansible.module_utils.six import iteritems
FLAGS = ('SUPERUSER', 'CREATEROLE', 'CREATEUSER', 'CREATEDB', 'INHERIT', 'LOGIN', 'REPLICATION') FLAGS = ('SUPERUSER', 'CREATEROLE', 'CREATEUSER', 'CREATEDB', 'INHERIT', 'LOGIN', 'REPLICATION')
FLAGS_BY_VERSION = {'BYPASSRLS': '9.5.0'} FLAGS_BY_VERSION = {'BYPASSRLS': 90500}
VALID_PRIVS = dict(table=frozenset(('SELECT', 'INSERT', 'UPDATE', 'DELETE', 'TRUNCATE', 'REFERENCES', 'TRIGGER', 'ALL')), VALID_PRIVS = dict(table=frozenset(('SELECT', 'INSERT', 'UPDATE', 'DELETE', 'TRUNCATE', 'REFERENCES', 'TRIGGER', 'ALL')),
database=frozenset( database=frozenset(
@ -687,33 +686,17 @@ def parse_privs(privs, db):
return o_privs return o_privs
def get_pg_server_version(cursor):
"""
Queries Postgres for its server version.
server_version should be just the server version itself:
postgres=# SHOW SERVER_VERSION;
server_version
----------------
9.6.2
(1 row)
"""
cursor.execute("SHOW SERVER_VERSION")
return cursor.fetchone()['server_version']
def get_valid_flags_by_version(cursor): def get_valid_flags_by_version(cursor):
""" """
Some role attributes were introduced after certain versions. We want to Some role attributes were introduced after certain versions. We want to
compile a list of valid flags against the current Postgres version. compile a list of valid flags against the current Postgres version.
""" """
current_version = StrictVersion(get_pg_server_version(cursor)) current_version = cursor.connection.server_version
return [ return [
flag flag
for flag, version_introduced in FLAGS_BY_VERSION.items() for flag, version_introduced in FLAGS_BY_VERSION.items()
if current_version >= StrictVersion(version_introduced) if current_version >= version_introduced
] ]

Loading…
Cancel
Save