postgresql_table: added cascade option (#56068)

This commit is contained in:
Andrey Klychkov 2019-05-07 13:38:03 +03:00 committed by Abhijeet Kasurde
parent 9271b4e368
commit f79b6b06f1
2 changed files with 59 additions and 2 deletions

View file

@ -93,6 +93,14 @@ options:
- Permissions checking for SQL commands is carried out as though
the session_role were the one that had logged in originally.
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:
- If you do not pass db parameter, tables will be created in the database
named postgres.
@ -174,6 +182,12 @@ EXAMPLES = r'''
postgresql_table:
name: foo
state: absent
- name: Drop table bar cascade
postgresql_table:
name: bar
state: absent
cascade: yes
'''
RETURN = r'''
@ -402,8 +416,10 @@ class Table(object):
self.executed_queries.append(query)
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')
if cascade:
query += " CASCADE"
self.executed_queries.append(query)
return self.__exec_sql(query, ddl=True)
@ -451,6 +467,7 @@ def main():
columns=dict(type='list'),
storage_params=dict(type='list'),
session_role=dict(type='str'),
cascade=dict(type='bool'),
)
module = AnsibleModule(
argument_spec=argument_spec,
@ -468,6 +485,10 @@ def main():
storage_params = module.params["storage_params"]
truncate = module.params["truncate"]
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:
if state == 'absent' and (truncate or newname or columns or tablespace or
@ -522,7 +543,7 @@ def main():
)
if state == 'absent':
changed = table_obj.drop()
changed = table_obj.drop(cascade=cascade)
elif truncate:
changed = table_obj.truncate()

View file

@ -691,3 +691,39 @@
that:
- result.rowcount == 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', '>=')