2020-03-09 10:11:07 +01:00
- name : postgresql_query - drop test table if exists
become_user : '{{ pg_user }}'
become : true
shell : psql postgres -U "{{ pg_user }}" -t -c "DROP TABLE IF EXISTS test_table;"
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - create test table called test_table
become_user : '{{ pg_user }}'
become : true
shell : psql postgres -U "{{ pg_user }}" -t -c "CREATE TABLE test_table (id int, story text);"
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - insert some data into test_table
become_user : '{{ pg_user }}'
become : true
shell : psql postgres -U "{{ pg_user }}" -t -c "INSERT INTO test_table (id, story) VALUES (1, 'first'), (2, 'second'), (3, 'third');"
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - remove SQL script if exists
become : true
file :
path : ~{{ pg_user}}/test.sql
state : absent
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - create an empty file to check permission
become : true
file :
path : ~{{ pg_user}}/test.sql
state : touch
owner : '{{ pg_user }}'
group : '{{ pg_user }}'
mode : '0644'
register : sql_file_created
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - prepare SQL script
become_user : '{{ pg_user }}'
become : true
shell : echo "{{ item }}" >> ~{{ pg_user}}/test.sql
ignore_errors : true
with_items :
- SELECT version();
- SELECT story FROM test_table
- WHERE id = %s OR story = 'Данные';
when : sql_file_created
- name : postgresql_query - analyze test_table
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
query : ANALYZE test_table
register : result
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is changed
- result.query == 'ANALYZE test_table'
2020-10-20 09:37:40 +02:00
- result.query_list == ['ANALYZE test_table']
2020-03-09 10:11:07 +01:00
- result.rowcount == 0
- result.statusmessage == 'ANALYZE'
- result.query_result == {}
2020-10-20 09:37:40 +02:00
- result.query_all_results == [{}]
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - run queries from SQL script
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
path_to_script : ~{{ pg_user }}/test.sql
positional_args :
- 1
encoding : UTF-8
register : result
ignore_errors : true
when : sql_file_created
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is not changed
2020-10-20 09:37:40 +02:00
- result.query == "\nSELECT story FROM test_table\nWHERE id = 1 OR story = 'Данные'"
2020-03-09 10:11:07 +01:00
- result.query_result[0].story == 'first'
2020-10-20 09:37:40 +02:00
- result.query_all_results[0][0].version is search('PostgreSQL')
- result.query_all_results[1][0].story == 'first'
- result.rowcount == 2
- result.statusmessage == 'SELECT 1' or result.statusmessage == 'SELECT'
2020-03-09 10:11:07 +01:00
when : sql_file_created
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - simple select query to test_table
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
query : SELECT * FROM test_table
register : result
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is not changed
- result.query == 'SELECT * FROM test_table'
- result.rowcount == 3
- result.statusmessage == 'SELECT 3' or result.statusmessage == 'SELECT'
- result.query_result[0].id == 1
- result.query_result[1].id == 2
- result.query_result[2].id == 3
- result.query_result[0].story == 'first'
- result.query_result[1].story == 'second'
- result.query_result[2].story == 'third'
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - select query with named args
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
query : SELECT id FROM test_table WHERE id = %(id_val)s AND story = %(story_val)s
named_args :
id_val : 1
story_val : first
register : result
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is not changed
- result.query == "SELECT id FROM test_table WHERE id = 1 AND story = 'first'" or result.query == "SELECT id FROM test_table WHERE id = 1 AND story = E'first'"
- result.rowcount == 1
- result.statusmessage == 'SELECT 1' or result.statusmessage == 'SELECT'
- result.query_result[0].id == 1
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - select query with positional arguments
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
query : SELECT story FROM test_table WHERE id = %s AND story = %s
positional_args :
- 2
- second
register : result
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is not changed
- result.query == "SELECT story FROM test_table WHERE id = 2 AND story = 'second'" or result.query == "SELECT story FROM test_table WHERE id = 2 AND story = E'second'"
- result.rowcount == 1
- result.statusmessage == 'SELECT 1' or result.statusmessage == 'SELECT'
- result.query_result[0].story == 'second'
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - simple update query
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
query : UPDATE test_table SET story = 'new' WHERE id = 3
register : result
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is changed
- result.query == "UPDATE test_table SET story = 'new' WHERE id = 3"
- result.rowcount == 1
- result.statusmessage == 'UPDATE 1'
- result.query_result == {}
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : check the previous update
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
query : SELECT * FROM test_table WHERE story = 'new' AND id = 3
register : result
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result.rowcount == 1
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - simple update query in check_mode
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
query : UPDATE test_table SET story = 'CHECK_MODE' WHERE id = 3
register : result
check_mode : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is changed
- result.query == "UPDATE test_table SET story = 'CHECK_MODE' WHERE id = 3"
- result.rowcount == 1
- result.statusmessage == 'UPDATE 1'
- result.query_result == {}
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : check the previous update that nothing has been changed
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
query : SELECT * FROM test_table WHERE story = 'CHECK_MODE' AND id = 3
register : result
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result.rowcount == 0
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - try to update not existing row
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
query : UPDATE test_table SET story = 'new' WHERE id = 100
register : result
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is not changed
- result.query == "UPDATE test_table SET story = 'new' WHERE id = 100"
- result.rowcount == 0
- result.statusmessage == 'UPDATE 0'
- result.query_result == {}
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - insert query
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
query : INSERT INTO test_table (id, story) VALUES (%s, %s)
positional_args :
- 4
- fourth
register : result
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is changed
- result.query == "INSERT INTO test_table (id, story) VALUES (4, 'fourth')" or result.query == "INSERT INTO test_table (id, story) VALUES (4, E'fourth')"
- result.rowcount == 1
- result.statusmessage == 'INSERT 0 1'
- result.query_result == {}
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - truncate test_table
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
query : TRUNCATE test_table
register : result
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is changed
- result.query == "TRUNCATE test_table"
- result.rowcount == 0
- result.statusmessage == 'TRUNCATE TABLE'
- result.query_result == {}
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - alter test_table
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
query : ALTER TABLE test_table ADD COLUMN foo int
register : result
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is changed
- result.query == "ALTER TABLE test_table ADD COLUMN foo int"
- result.rowcount == 0
- result.statusmessage == 'ALTER TABLE'
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - vacuum without autocommit must fail
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
query : VACUUM
register : result
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result.failed == true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - autocommit in check_mode must fail
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
query : VACUUM
autocommit : true
check_mode : true
register : result
ignore_errors : true
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result.failed == true
- result.msg == "Using autocommit is mutually exclusive with check_mode"
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - vacuum with autocommit
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
db : postgres
query : VACUUM
autocommit : true
register : result
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is changed
- result.query == "VACUUM"
- result.rowcount == 0
- result.statusmessage == 'VACUUM'
- result.query_result == {}
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - create test table for issue 59955
become_user : '{{ pg_user }}'
become : true
postgresql_table :
login_user : '{{ pg_user }}'
login_db : postgres
name : test_array_table
columns :
- arr_col int[]
when : postgres_version_resp.stdout is version('9.4', '>=')
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- set_fact :
my_list :
- 1
- 2
- 3
my_arr : '{1, 2, 3}'
when : postgres_version_resp.stdout is version('9.4', '>=')
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - insert array into test table by positional args
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
login_db : postgres
query : INSERT INTO test_array_table (arr_col) VALUES (%s)
positional_args :
- '{{ my_list }}'
register : result
when : postgres_version_resp.stdout is version('9.4', '>=')
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is changed
- result.query == "INSERT INTO test_array_table (arr_col) VALUES ('{1, 2, 3}')"
when : postgres_version_resp.stdout is version('9.4', '>=')
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - select array from test table by passing positional_args
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
login_db : postgres
query : SELECT * FROM test_array_table WHERE arr_col = %s
positional_args :
- '{{ my_list }}'
register : result
when : postgres_version_resp.stdout is version('9.4', '>=')
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is not changed
- result.query == "SELECT * FROM test_array_table WHERE arr_col = '{1, 2, 3}'"
- result.rowcount == 1
when : postgres_version_resp.stdout is version('9.4', '>=')
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - select array from test table by passing named_args
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
login_db : postgres
query : SELECT * FROM test_array_table WHERE arr_col = %(arr_val)s
named_args :
arr_val :
- '{{ my_list }}'
register : result
when : postgres_version_resp.stdout is version('9.4', '>=')
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is not changed
- result.query == "SELECT * FROM test_array_table WHERE arr_col = '{1, 2, 3}'"
- result.rowcount == 1
when : postgres_version_resp.stdout is version('9.4', '>=')
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- name : postgresql_query - select array from test table by passing positional_args as a string
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
login_db : postgres
query : SELECT * FROM test_array_table WHERE arr_col = %s
positional_args :
- '{{ my_arr|string }}'
2020-05-07 11:49:53 +02:00
trust_input : yes
2020-03-09 10:11:07 +01:00
register : result
when : postgres_version_resp.stdout is version('9.4', '>=')
2020-05-07 11:49:53 +02:00
2020-03-09 10:11:07 +01:00
- assert :
that :
- result is not changed
- result.query == "SELECT * FROM test_array_table WHERE arr_col = '{1, 2, 3}'"
- result.rowcount == 1
when : postgres_version_resp.stdout is version('9.4', '>=')
2020-05-07 11:49:53 +02:00
- name : postgresql_query - test trust_input parameter
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
login_db : postgres
session_role : 'curious.anonymous"; SELECT * FROM information_schema.tables; --'
query : SELECT version()
trust_input : no
ignore_errors : yes
register : result
- assert :
that :
- result is failed
- result.msg is search('is potentially dangerous')
2020-03-09 10:11:07 +01:00
- name : postgresql_query - clean up
become_user : '{{ pg_user }}'
become : true
postgresql_table :
login_user : '{{ pg_user }}'
login_db : postgres
name : test_array_table
state : absent
when : postgres_version_resp.stdout is version('9.4', '>=')
2020-07-20 09:08:02 +02:00
#############################
# Check search_path parameter
- name : postgresql_set - create test schemas
become_user : '{{ pg_user }}'
become : true
postgresql_schema :
login_user : '{{ pg_user }}'
login_db : postgres
name : '{{ item }}'
loop :
- query_test1
- query_test2
- name : postgresql_set - create test tables
become_user : '{{ pg_user }}'
become : true
postgresql_table :
login_user : '{{ pg_user }}'
login_db : postgres
name : '{{ item }}'
columns :
- id int
loop :
- 'query_test1.test1'
- 'query_test2.test2'
- name : postgresql_query - insert data
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
login_db : postgres
query : 'INSERT INTO {{ item }} (id) VALUES (1)'
search_path :
- query_test1
- query_test2
loop :
- test1
- test2
- name : postgresql_query - get data
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
login_db : postgres
query : 'SELECT id FROM test1'
search_path :
- query_test1
- query_test2
register : result
- assert :
that :
- result.rowcount == 1
- name : postgresql_query - get data, must fail
become_user : '{{ pg_user }}'
become : true
postgresql_query :
login_user : '{{ pg_user }}'
login_db : postgres
query : 'SELECT id FROM test1'
register : result
ignore_errors : yes
- assert :
that :
- result is failed