Backport PR of 57391 postgresql_table - bugfix of schema handling

This commit is contained in:
Andrey Klychkov 2019-06-05 22:06:23 +03:00 committed by Toshio Kuratomi
parent 5e177d73a9
commit 5ebc83bce6
3 changed files with 184 additions and 6 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- postgresql_table - fix schema handling (https://github.com/ansible/ansible/pull/57391)

View file

@ -143,9 +143,9 @@ EXAMPLES = r'''
- fillfactor=10 - fillfactor=10
- autovacuum_analyze_threshold=1 - autovacuum_analyze_threshold=1
- name: Create an unlogged table - name: Create an unlogged table in schema acme
postgresql_table: postgresql_table:
name: useless_data name: acme.useless_data
columns: waste_id int columns: waste_id int
unlogged: true unlogged: true
@ -154,6 +154,11 @@ EXAMPLES = r'''
table: foo table: foo
rename: bar rename: bar
- name: Rename table foo from schema acme to bar
postgresql_table:
name: acme.foo
rename: bar
- name: Set owner to someuser - name: Set owner to someuser
postgresql_table: postgresql_table:
name: foo name: foo
@ -170,9 +175,9 @@ EXAMPLES = r'''
name: foo name: foo
truncate: yes truncate: yes
- name: Drop table foo - name: Drop table foo from schema acme
postgresql_table: postgresql_table:
name: foo name: acme.foo
state: absent state: absent
''' '''
@ -246,12 +251,19 @@ class Table(object):
def __exists_in_db(self): def __exists_in_db(self):
"""Check table exists and refresh info""" """Check table exists and refresh info"""
if "." in self.name:
schema = self.name.split('.')[-2]
tblname = self.name.split('.')[-1]
else:
schema = 'public'
tblname = self.name
query = ("SELECT t.tableowner, t.tablespace, c.reloptions " query = ("SELECT t.tableowner, t.tablespace, c.reloptions "
"FROM pg_tables AS t " "FROM pg_tables AS t "
"INNER JOIN pg_class AS c ON c.relname = t.tablename " "INNER JOIN pg_class AS c ON c.relname = t.tablename "
"INNER JOIN pg_namespace AS n ON c.relnamespace = n.oid " "INNER JOIN pg_namespace AS n ON c.relnamespace = n.oid "
"WHERE t.tablename = '%s' " "WHERE t.tablename = '%s' "
"AND n.nspname = 'public'" % self.name) "AND n.nspname = '%s'" % (tblname, schema))
res = self.__exec_sql(query) res = self.__exec_sql(query)
if res: if res:
self.exists = True self.exists = True
@ -402,7 +414,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):
if not self.exists:
return False
query = "DROP TABLE %s" % pg_quote_identifier(self.name, 'table') query = "DROP TABLE %s" % pg_quote_identifier(self.name, 'table')
self.executed_queries.append(query) self.executed_queries.append(query)
return self.__exec_sql(query, ddl=True) return self.__exec_sql(query, ddl=True)

View file

@ -12,6 +12,14 @@
login_user: "{{ pg_user }}" login_user: "{{ pg_user }}"
name: alice name: alice
- name: postgresql_table - create test schema
become_user: "{{ pg_user }}"
become: yes
postgresql_schema:
database: postgres
login_user: "{{ pg_user }}"
name: acme
# #
# Check table creation # Check table creation
# #
@ -691,3 +699,156 @@
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', '>=')
#
# Create, drop, and rename table in a specific schema:
#
- name: postgresql_table - create table in a specific schema
postgresql_table:
db: postgres
login_user: "{{ pg_user }}"
name: acme.test_schema_table
register: result
- assert:
that:
- result.changed == true
- result.queries == ['CREATE TABLE "acme"."test_schema_table" ()']
- name: postgresql_table - check that table exists 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 = 'test_schema_table' and schemaname = 'acme'"
ignore_errors: yes
register: result
- assert:
that:
- result.rowcount == 1
- name: postgresql_table - try to create a table with the same name and schema again
postgresql_table:
db: postgres
login_user: "{{ pg_user }}"
name: acme.test_schema_table
register: result
- assert:
that:
- result.changed == false
- name: postgresql_table - create a table in the default schema for the next test
postgresql_table:
db: postgres
login_user: "{{ pg_user }}"
name: test_schema_table
register: result
- assert:
that:
- result.changed == true
- name: postgresql_table - drop the table from schema acme
postgresql_table:
db: postgres
login_user: "{{ pg_user }}"
name: postgres.acme.test_schema_table
state: absent
register: result
- assert:
that:
- result.changed == true
- result.queries == ['DROP TABLE "postgres"."acme"."test_schema_table"']
- name: postgresql_table - check that the 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 = 'test_schema_table' and schemaname = 'acme'"
ignore_errors: yes
register: result
- assert:
that:
- result.rowcount == 0
- name: postgresql_table - try to drop the table from schema acme again
postgresql_table:
db: postgres
login_user: "{{ pg_user }}"
name: acme.test_schema_table
state: absent
register: result
- assert:
that:
- result.changed == false
- name: postgresql_table - check that the table with the same name in schema public exists
become_user: "{{ pg_user }}"
become: yes
postgresql_query:
db: postgres
login_user: "{{ pg_user }}"
query: "SELECT 1 FROM pg_stat_all_tables WHERE relname = 'test_schema_table' and schemaname = 'public'"
ignore_errors: yes
register: result
- assert:
that:
- result.rowcount == 1
- name: postgresql_table - rename the table that contents a schema name
postgresql_table:
db: postgres
login_user: "{{ pg_user }}"
name: public.test_schema_table
rename: new_test_schema_table
register: result
- assert:
that:
- result.changed == true
- result.queries == ['ALTER TABLE "public"."test_schema_table" RENAME TO "new_test_schema_table"']