From 9cb7cbb62ab03cb7bcfba666c44d073c6643e17e Mon Sep 17 00:00:00 2001 From: Prashant Baisla Date: Fri, 25 Jan 2019 14:55:39 +0530 Subject: [PATCH] postgresql_ext: Allow creating extension in a specific schema (#47279) * Allow creating extension in a specific schema * Code Review: Update example to include schema parameter * Code Review: Use simple string concatenation to build query --- .../modules/database/postgresql/postgresql_ext.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/database/postgresql/postgresql_ext.py b/lib/ansible/modules/database/postgresql/postgresql_ext.py index 8e8f71af01e..face12db021 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_ext.py +++ b/lib/ansible/modules/database/postgresql/postgresql_ext.py @@ -28,6 +28,10 @@ options: description: - name of the database to add or remove the extension to/from required: true + schema: + description: + - name of the schema to add the extension to + version_added: "2.8" login_user: description: - The username used to authenticate with @@ -84,6 +88,7 @@ EXAMPLES = ''' - postgresql_ext: name: postgis db: acme + schema: extensions ''' import traceback @@ -123,9 +128,11 @@ def ext_delete(cursor, ext): return False -def ext_create(cursor, ext): +def ext_create(cursor, ext, schema): if not ext_exists(cursor, ext): query = 'CREATE EXTENSION "%s"' % ext + if schema: + query += ' WITH SCHEMA "%s"' % schema cursor.execute(query) return True else: @@ -146,6 +153,7 @@ def main(): port=dict(default="5432"), db=dict(required=True), ext=dict(required=True, aliases=['name']), + schema=dict(default=""), state=dict(default="present", choices=["absent", "present"]), ssl_mode=dict(default='prefer', choices=[ 'disable', 'allow', 'prefer', 'require', 'verify-ca', 'verify-full']), @@ -159,6 +167,7 @@ def main(): db = module.params["db"] ext = module.params["ext"] + schema = module.params["schema"] state = module.params["state"] sslrootcert = module.params["ssl_rootcert"] changed = False @@ -218,7 +227,7 @@ def main(): changed = ext_delete(cursor, ext) elif state == "present": - changed = ext_create(cursor, ext) + changed = ext_create(cursor, ext, schema) except NotSupportedError as e: module.fail_json(msg=to_native(e), exception=traceback.format_exc()) except Exception as e: