- name:postgresql_sequence - check that the sequence "foobar" not exists
become_user:"{{ pg_user }}"
become:yes
postgresql_query:
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
query:"SELECT sequence_name FROM information_schema.sequences WHERE sequence_name = 'foobar'"
register:result
- name:postgresql_sequence - check with assert the output
assert:
that:
- result.rowcount == 0
####################
# Test: drop nonexistent sequence
- name:postgresql_sequence - drop a sequence called foobar which does not exists
become_user:"{{ pg_user }}"
become:yes
postgresql_sequence:
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
name:foobar
state:absent
register:result
# Checks
- name:postgresql_sequence - check with assert the output
assert:
that:
- result is not changed
- result.sequence == 'foobar'
- result.queries == []
# Real SQL check
- name:postgresql_sequence - check that the sequence "foobar" not exists
become_user:"{{ pg_user }}"
become:yes
postgresql_query:
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
query:"SELECT sequence_name FROM information_schema.sequences WHERE sequence_name = 'foobar'"
register:result
- name:postgresql_sequence - check with assert the output
assert:
that:
- result.rowcount == 0
####################
# Test: create sequence with options
- name:postgresql_sequence - create an descending sequence called foobar_desc, starting at 101 and which cycle between 1 to 1000
become_user:"{{ pg_user }}"
become:yes
postgresql_sequence:
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
name:foobar_desc
increment:-1
start:101
minvalue:1
maxvalue:1000
cycle:yes
register:result
# Checks
- name:postgresql_sequence - check with assert the output
assert:
that:
- result is changed
- result.sequence == 'foobar_desc'
- result.increment == '-1'
- result.minvalue == '1'
- result.maxvalue == '1000'
- result.cycle == 'YES'
- result.queries == ["CREATE SEQUENCE \"public\".\"foobar_desc\" INCREMENT BY -1 MINVALUE 1 MAXVALUE 1000 START WITH 101 CYCLE"]
# Real SQL check
- name:postgresql_sequence - check that the new sequence "foobar_desc" exists
postgresql_query:
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
query:"SELECT sequence_name FROM information_schema.sequences WHERE sequence_name = 'foobar_desc'"
register:result
- name:postgresql_sequence - check with assert the output
assert:
that:
- result.rowcount == 1
####################
# Test: rename a sequence in checkmode
- name:postgresql_sequence - rename an existing sequence named foobar_desc to foobar_with_options
become_user:"{{ pg_user }}"
become:yes
check_mode:yes
postgresql_sequence:
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
name:foobar_desc
rename_to:foobar_with_options
register:result
# Checks
- name:postgresql_sequence - check with assert the output
assert:
that:
- result is changed
- result.sequence == 'foobar_desc'
- result.newname == 'foobar_with_options'
- result.queries == ["ALTER SEQUENCE \"public\".\"foobar_desc\" RENAME TO \"foobar_with_options\""]
# Real SQL check
- name:postgresql_sequence - check that the sequence "foobar_desc" still exists and is not renamed
become_user:"{{ pg_user }}"
become:yes
postgresql_query:
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
query:"SELECT sequence_name FROM information_schema.sequences WHERE sequence_name = 'foobar_desc'"
register:result
- name:postgresql_sequence - check with assert the output
assert:
that:
- result.rowcount == 1
####################
# Test: rename a sequence
- name:postgresql_sequence - rename an existing sequence named foobar_desc to foobar_with_options
become_user:"{{ pg_user }}"
become:yes
postgresql_sequence:
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
name:foobar_desc
rename_to:foobar_with_options
register:result
# Checks
- name:postgresql_sequence - check with assert the output
assert:
that:
- result is changed
- result.sequence == 'foobar_desc'
- result.newname == 'foobar_with_options'
- result.queries == ["ALTER SEQUENCE \"public\".\"foobar_desc\" RENAME TO \"foobar_with_options\""]
# Real SQL check
- name:postgresql_sequence - check that the renamed sequence "foobar_with_options" exists
become_user:"{{ pg_user }}"
become:yes
postgresql_query:
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
query:"SELECT sequence_name FROM information_schema.sequences WHERE sequence_name = 'foobar_with_options'"
register:result
- name:postgresql_sequence - check with assert the output
assert:
that:
- result.rowcount == 1
####################
# Test: change schema of a sequence in checkmode
- name:postgresql_sequence - change schema of an existing sequence from public to foobar_schema
become_user:"{{ pg_user }}"
become:yes
check_mode:yes
postgresql_sequence:
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
name:foobar_with_options
newschema:foobar_schema
register:result
# Checks
- name:postgresql_sequence - check with assert the output
assert:
that:
- result is changed
- result.sequence == 'foobar_with_options'
- result.schema == 'public'
- result.newschema == 'foobar_schema'
- result.queries == ["ALTER SEQUENCE \"public\".\"foobar_with_options\" SET SCHEMA \"foobar_schema\""]
# Real SQL check
- name:postgresql_sequence - check that the sequence "foobar_with_options" still exists in the old schema
become_user:"{{ pg_user }}"
become:yes
postgresql_query:
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
query:"SELECT sequence_name,sequence_schema FROM information_schema.sequences WHERE sequence_name = 'foobar_with_options' AND sequence_schema = 'public'"
register:result
- name:postgresql_sequence - check with assert the output
assert:
that:
- result.rowcount == 1
####################
# Test: change schema of a sequence
- name:postgresql_sequence - change schema of an existing sequence from public to foobar_schema
become_user:"{{ pg_user }}"
become:yes
postgresql_sequence:
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
name:foobar_with_options
newschema:foobar_schema
register:result
# Checks
- name:postgresql_sequence - check with assert the output
assert:
that:
- result is changed
- result.sequence == 'foobar_with_options'
- result.schema == 'public'
- result.newschema == 'foobar_schema'
- result.queries == ["ALTER SEQUENCE \"public\".\"foobar_with_options\" SET SCHEMA \"foobar_schema\""]
# Real SQL check
- name:postgresql_sequence - check that the sequence "foobar_with_options" exists in new schema
become_user:"{{ pg_user }}"
become:yes
postgresql_query:
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
query:"SELECT sequence_name,sequence_schema FROM information_schema.sequences WHERE sequence_name = 'foobar_with_options' AND sequence_schema = 'foobar_schema'"
register:result
- name:postgresql_sequence - check with assert the output
assert:
that:
- result.rowcount == 1
####################
# Test: change owner of a sequence in checkmode
- name:postgresql_sequence - change owner of an existing sequence from "{{ pg_user }}" to "{{ db_user1 }}"
become_user:"{{ pg_user }}"
become:yes
check_mode:yes
postgresql_sequence:
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
name:foobar_with_options
schema:foobar_schema
owner:"{{ db_user1 }}"
register:result
# Checks
- name:postgresql_sequence - check with assert the output