postgresql_table: added cascade option (#56068)

pull/57614/head
Andrey Klychkov 6 years ago committed by Abhijeet Kasurde
parent 9271b4e368
commit f79b6b06f1

@ -93,6 +93,14 @@ options:
- Permissions checking for SQL commands is carried out as though - Permissions checking for SQL commands is carried out as though
the session_role were the one that had logged in originally. the session_role were the one that had logged in originally.
type: str type: str
cascade:
description:
- Automatically drop objects that depend on the table (such as views)
U(https://www.postgresql.org/docs/current/sql-droptable.html).
Used with I(state=absent) only.
type: bool
default: no
version_added: '2.9'
notes: notes:
- If you do not pass db parameter, tables will be created in the database - If you do not pass db parameter, tables will be created in the database
named postgres. named postgres.
@ -174,6 +182,12 @@ EXAMPLES = r'''
postgresql_table: postgresql_table:
name: foo name: foo
state: absent state: absent
- name: Drop table bar cascade
postgresql_table:
name: bar
state: absent
cascade: yes
''' '''
RETURN = r''' RETURN = r'''
@ -402,8 +416,10 @@ class Table(object):
self.executed_queries.append(query) self.executed_queries.append(query)
return self.__exec_sql(query, ddl=True) return self.__exec_sql(query, ddl=True)
def drop(self): def drop(self, cascade=False):
query = "DROP TABLE %s" % pg_quote_identifier(self.name, 'table') query = "DROP TABLE %s" % pg_quote_identifier(self.name, 'table')
if cascade:
query += " CASCADE"
self.executed_queries.append(query) self.executed_queries.append(query)
return self.__exec_sql(query, ddl=True) return self.__exec_sql(query, ddl=True)
@ -451,6 +467,7 @@ def main():
columns=dict(type='list'), columns=dict(type='list'),
storage_params=dict(type='list'), storage_params=dict(type='list'),
session_role=dict(type='str'), session_role=dict(type='str'),
cascade=dict(type='bool'),
) )
module = AnsibleModule( module = AnsibleModule(
argument_spec=argument_spec, argument_spec=argument_spec,
@ -468,6 +485,10 @@ def main():
storage_params = module.params["storage_params"] storage_params = module.params["storage_params"]
truncate = module.params["truncate"] truncate = module.params["truncate"]
columns = module.params["columns"] columns = module.params["columns"]
cascade = module.params["cascade"]
if state == 'present' and cascade:
module.warn("cascade=true is ignored when state=present")
# Check mutual exclusive parameters: # Check mutual exclusive parameters:
if state == 'absent' and (truncate or newname or columns or tablespace or if state == 'absent' and (truncate or newname or columns or tablespace or
@ -522,7 +543,7 @@ def main():
) )
if state == 'absent': if state == 'absent':
changed = table_obj.drop() changed = table_obj.drop(cascade=cascade)
elif truncate: elif truncate:
changed = table_obj.truncate() changed = table_obj.truncate()

@ -691,3 +691,39 @@
that: that:
- result.rowcount == 1 - result.rowcount == 1
when: postgres_version_resp.stdout is version('9.1', '>=') when: postgres_version_resp.stdout is version('9.1', '>=')
# Drop table CASCADE:
- name: postgresql_table - drop table cascade
become: yes
become_user: "{{ pg_user }}"
postgresql_table:
db: postgres
login_user: "{{ pg_user }}"
name: test5
state: absent
cascade: yes
register: result
ignore_errors: yes
- assert:
that:
- result.changed == true
- result.queries == ['DROP TABLE "test5" CASCADE']
when: postgres_version_resp.stdout is version('9.1', '>=')
# Check that the table doesn't exist after the previous step, rowcount must be - 0
- name: postgresql_table - check that table doesn't exist after the previous step
become_user: "{{ pg_user }}"
become: yes
postgresql_query:
db: postgres
login_user: "{{ pg_user }}"
query: "SELECT 1 FROM pg_stat_all_tables WHERE relname = 'test5'"
ignore_errors: yes
register: result
when: postgres_version_resp.stdout is version('9.1', '>=')
- assert:
that:
- result.rowcount == 0
when: postgres_version_resp.stdout is version('9.1', '>=')

Loading…
Cancel
Save