@ -312,11 +312,9 @@ class Connection(object):
self . connection = psycopg2 . connect ( * * kw )
self . cursor = self . connection . cursor ( )
def commit ( self ) :
self . connection . commit ( )
def rollback ( self ) :
self . connection . rollback ( )
@ -325,8 +323,7 @@ class Connection(object):
""" Connection encoding in Python-compatible form """
return psycopg2 . extensions . encodings [ self . connection . encoding ]
### Methods for querying database objects
# Methods for querying database objects
# PostgreSQL < 9.0 doesn't support "ALL TABLES IN SCHEMA schema"-like
# phrases in GRANT or REVOKE statements, therefore alternative methods are
@ -338,7 +335,6 @@ class Connection(object):
self . cursor . execute ( query , ( schema , ) )
return self . cursor . fetchone ( ) [ 0 ] > 0
def get_all_tables_in_schema ( self , schema ) :
if not self . schema_exists ( schema ) :
raise Error ( ' Schema " %s " does not exist. ' % schema )
@ -349,7 +345,6 @@ class Connection(object):
self . cursor . execute ( query , ( schema , ) )
return [ t [ 0 ] for t in self . cursor . fetchall ( ) ]
def get_all_sequences_in_schema ( self , schema ) :
if not self . schema_exists ( schema ) :
raise Error ( ' Schema " %s " does not exist. ' % schema )
@ -360,9 +355,7 @@ class Connection(object):
self . cursor . execute ( query , ( schema , ) )
return [ t [ 0 ] for t in self . cursor . fetchall ( ) ]
### Methods for getting access control lists and group membership info
# Methods for getting access control lists and group membership info
# To determine whether anything has changed after granting/revoking
# privileges, we compare the access control lists of the specified database
@ -379,7 +372,6 @@ class Connection(object):
self . cursor . execute ( query , ( schema , tables ) )
return [ t [ 0 ] for t in self . cursor . fetchall ( ) ]
def get_sequence_acls ( self , schema , sequences ) :
query = """ SELECT relacl
FROM pg_catalog . pg_class c
@ -389,7 +381,6 @@ class Connection(object):
self . cursor . execute ( query , ( schema , sequences ) )
return [ t [ 0 ] for t in self . cursor . fetchall ( ) ]
def get_function_acls ( self , schema , function_signatures ) :
funcnames = [ f . split ( ' ( ' , 1 ) [ 0 ] for f in function_signatures ]
query = """ SELECT proacl
@ -400,35 +391,30 @@ class Connection(object):
self . cursor . execute ( query , ( schema , funcnames ) )
return [ t [ 0 ] for t in self . cursor . fetchall ( ) ]
def get_schema_acls ( self , schemas ) :
query = """ SELECT nspacl FROM pg_catalog.pg_namespace
WHERE nspname = ANY ( % s ) ORDER BY nspname """
self . cursor . execute ( query , ( schemas , ) )
return [ t [ 0 ] for t in self . cursor . fetchall ( ) ]
def get_language_acls ( self , languages ) :
query = """ SELECT lanacl FROM pg_catalog.pg_language
WHERE lanname = ANY ( % s ) ORDER BY lanname """
self . cursor . execute ( query , ( languages , ) )
return [ t [ 0 ] for t in self . cursor . fetchall ( ) ]
def get_tablespace_acls ( self , tablespaces ) :
query = """ SELECT spcacl FROM pg_catalog.pg_tablespace
WHERE spcname = ANY ( % s ) ORDER BY spcname """
self . cursor . execute ( query , ( tablespaces , ) )
return [ t [ 0 ] for t in self . cursor . fetchall ( ) ]
def get_database_acls ( self , databases ) :
query = """ SELECT datacl FROM pg_catalog.pg_database
WHERE datname = ANY ( % s ) ORDER BY datname """
self . cursor . execute ( query , ( databases , ) )
return [ t [ 0 ] for t in self . cursor . fetchall ( ) ]
def get_group_memberships ( self , groups ) :
query = """ SELECT roleid, grantor, member, admin_option
FROM pg_catalog . pg_auth_members am
@ -438,8 +424,7 @@ class Connection(object):
self . cursor . execute ( query , ( groups , ) )
return self . cursor . fetchall ( )
### Manipulating privileges
# Manipulating privileges
def manipulate_privs ( self , obj_type , privs , objs , roles ,
state , grant_option , schema_qualifier = None ) :
@ -658,9 +643,7 @@ def main():
except psycopg2 . Error as e :
conn . rollback ( )
# psycopg2 errors come in connection encoding
msg = to_text ( e . message ( encoding = conn . encoding ) )
module . fail_json ( msg = msg )
module . fail_json ( msg = to_native ( e . message ) )
if module . check_mode :
conn . rollback ( )