postgresql_schema: permit to drop cascade schema (#49469)

* postgresql_schema: permit to drop cascade schema

* Doc fix

* typo fix
pull/49879/head
Loïc Blot 6 years ago committed by ansibot
parent 8d008fed35
commit c97d2c25b4

@ -54,6 +54,12 @@ options:
- The schema state. - The schema state.
default: present default: present
choices: [ "present", "absent" ] choices: [ "present", "absent" ]
cascade_drop:
description:
- Drop schema with CASCADE to remove child objects
type: bool
default: false
version_added: '2.8'
ssl_mode: ssl_mode:
description: description:
- Determines whether or with what priority a secure SSL TCP/IP connection - Determines whether or with what priority a secure SSL TCP/IP connection
@ -91,6 +97,11 @@ EXAMPLES = '''
name: acme name: acme
owner: bob owner: bob
# Drop schema "acme" with cascade
- postgresql_schema:
name: acme
ensure: absent
cascade_drop: yes
''' '''
RETURN = ''' RETURN = '''
@ -149,9 +160,11 @@ def schema_exists(cursor, schema):
return cursor.rowcount == 1 return cursor.rowcount == 1
def schema_delete(cursor, schema): def schema_delete(cursor, schema, cascade):
if schema_exists(cursor, schema): if schema_exists(cursor, schema):
query = "DROP SCHEMA %s" % pg_quote_identifier(schema, 'schema') query = "DROP SCHEMA %s" % pg_quote_identifier(schema, 'schema')
if cascade:
query += " CASCADE"
cursor.execute(query) cursor.execute(query)
return True return True
else: else:
@ -200,6 +213,7 @@ def main():
schema=dict(required=True, aliases=['name']), schema=dict(required=True, aliases=['name']),
owner=dict(default=""), owner=dict(default=""),
database=dict(default="postgres"), database=dict(default="postgres"),
cascade_drop=dict(type="bool", default=False),
state=dict(default="present", choices=["absent", "present"]), state=dict(default="present", choices=["absent", "present"]),
ssl_mode=dict(default='prefer', choices=[ ssl_mode=dict(default='prefer', choices=[
'disable', 'allow', 'prefer', 'require', 'verify-ca', 'verify-full']), 'disable', 'allow', 'prefer', 'require', 'verify-ca', 'verify-full']),
@ -215,6 +229,7 @@ def main():
owner = module.params["owner"] owner = module.params["owner"]
state = module.params["state"] state = module.params["state"]
sslrootcert = module.params["ssl_rootcert"] sslrootcert = module.params["ssl_rootcert"]
cascade_drop = module.params["cascade_drop"]
changed = False changed = False
# To use defaults values, keyword arguments must be absent, so # To use defaults values, keyword arguments must be absent, so
@ -272,7 +287,7 @@ def main():
if state == "absent": if state == "absent":
try: try:
changed = schema_delete(cursor, schema) changed = schema_delete(cursor, schema, cascade_drop)
except SQLParseError as e: except SQLParseError as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc()) module.fail_json(msg=to_native(e), exception=traceback.format_exc())

Loading…
Cancel
Save