| 1 |
# 0. Check necessary options. |
| 2 |
# 1. When both databases do not exists, it must fail. |
| 3 |
# 2. When both databases exist, it must fail. |
| 4 |
# 3. When the source database exists and the target does not, rename it. |
| 5 |
# 4. When the source database doesn't exist and the target does, do nothing. |
| 6 |
# 5. Check mode |
| 7 |
|
| 8 |
- become_user: '{{ pg_user }}' |
| 9 |
become: true |
| 10 |
vars: |
| 11 |
db_source_name: acme |
| 12 |
db_target_name: acme1 |
| 13 |
|
| 14 |
task_parameters: &task_parameters |
| 15 |
register: result |
| 16 |
|
| 17 |
pg_parameters: &pg_parameters |
| 18 |
login_user: '{{ pg_user }}' |
| 19 |
|
| 20 |
block: |
| 21 |
# 0. Check necessary options. |
| 22 |
- name: Miss target option, must fail |
| 23 |
<<: *task_parameters |
| 24 |
postgresql_db: |
| 25 |
<<: *pg_parameters |
| 26 |
name: '{{ db_source_name }}' |
| 27 |
state: rename |
| 28 |
ignore_errors: true |
| 29 |
|
| 30 |
- assert: |
| 31 |
that: |
| 32 |
- result is failed |
| 33 |
- result.msg == 'The "target" option must be defined when the "rename" option is used.' |
| 34 |
|
| 35 |
- name: Target and name options are the same, must fail |
| 36 |
<<: *task_parameters |
| 37 |
postgresql_db: |
| 38 |
<<: *pg_parameters |
| 39 |
name: '{{ db_source_name }}' |
| 40 |
state: rename |
| 41 |
target: '{{ db_source_name }}' |
| 42 |
ignore_errors: true |
| 43 |
|
| 44 |
- assert: |
| 45 |
that: |
| 46 |
- result is failed |
| 47 |
- result.msg == 'The "name/db" option and the "target" option cannot be the same.' |
| 48 |
|
| 49 |
- name: Maintenance_db and name options are the same, must fail |
| 50 |
<<: *task_parameters |
| 51 |
postgresql_db: |
| 52 |
<<: *pg_parameters |
| 53 |
name: postgres |
| 54 |
state: rename |
| 55 |
target: '{{ db_source_name }}' |
| 56 |
ignore_errors: true |
| 57 |
|
| 58 |
- assert: |
| 59 |
that: |
| 60 |
- result is failed |
| 61 |
- result.msg == 'The "maintenance_db" option and the "name/db" option cannot be the same.' |
| 62 |
|
| 63 |
# 1. When both databases do not exists, it must fail. |
| 64 |
- name: Try to rename when both do not exist, must fail |
| 65 |
<<: *task_parameters |
| 66 |
postgresql_db: |
| 67 |
<<: *pg_parameters |
| 68 |
name: '{{ db_source_name }}' |
| 69 |
state: rename |
| 70 |
target: '{{ db_target_name}}' |
| 71 |
ignore_errors: true |
| 72 |
|
| 73 |
- assert: |
| 74 |
that: |
| 75 |
- result is failed |
| 76 |
- result.msg == 'The source and the target databases do not exist.' |
| 77 |
|
| 78 |
- name: Try to rename when both do not exist, must fail, check_mode |
| 79 |
<<: *task_parameters |
| 80 |
postgresql_db: |
| 81 |
<<: *pg_parameters |
| 82 |
name: '{{ db_source_name }}' |
| 83 |
state: rename |
| 84 |
target: '{{ db_target_name}}' |
| 85 |
ignore_errors: true |
| 86 |
check_mode: true |
| 87 |
|
| 88 |
- assert: |
| 89 |
that: |
| 90 |
- result is failed |
| 91 |
- result.msg == 'The source and the target databases do not exist.' |
| 92 |
|
| 93 |
# 2. When both databases exist, it must fail. |
| 94 |
- name: Create test DBs |
| 95 |
<<: *task_parameters |
| 96 |
postgresql_db: |
| 97 |
<<: *pg_parameters |
| 98 |
name: '{{ item }}' |
| 99 |
state: present |
| 100 |
loop: |
| 101 |
- '{{ db_source_name }}' |
| 102 |
- '{{ db_target_name }}' |
| 103 |
|
| 104 |
- name: Try to rename when both exist, must fail |
| 105 |
<<: *task_parameters |
| 106 |
postgresql_db: |
| 107 |
<<: *pg_parameters |
| 108 |
name: '{{ db_source_name }}' |
| 109 |
state: rename |
| 110 |
target: '{{ db_target_name}}' |
| 111 |
ignore_errors: true |
| 112 |
|
| 113 |
- assert: |
| 114 |
that: |
| 115 |
- result is failed |
| 116 |
- result.msg == 'Both the source and the target databases exist.' |
| 117 |
|
| 118 |
- name: Try to rename when both exist, must fail |
| 119 |
<<: *task_parameters |
| 120 |
postgresql_db: |
| 121 |
<<: *pg_parameters |
| 122 |
name: '{{ db_source_name }}' |
| 123 |
state: rename |
| 124 |
target: '{{ db_target_name}}' |
| 125 |
ignore_errors: true |
| 126 |
|
| 127 |
- assert: |
| 128 |
that: |
| 129 |
- result is failed |
| 130 |
- result.msg == 'Both the source and the target databases exist.' |
| 131 |
|
| 132 |
# 3. When the source database exists and the target does not, rename it. |
| 133 |
# 4. When the source database doesn't exist and the target does, do nothing. |
| 134 |
- name: Drop the target DB |
| 135 |
<<: *task_parameters |
| 136 |
postgresql_db: |
| 137 |
<<: *pg_parameters |
| 138 |
name: '{{ db_target_name }}' |
| 139 |
state: absent |
| 140 |
|
| 141 |
- name: Rename DB in check mode |
| 142 |
<<: *task_parameters |
| 143 |
postgresql_db: |
| 144 |
<<: *pg_parameters |
| 145 |
name: '{{ db_source_name }}' |
| 146 |
state: rename |
| 147 |
target: '{{ db_target_name }}' |
| 148 |
check_mode: true |
| 149 |
|
| 150 |
- assert: |
| 151 |
that: |
| 152 |
- result is succeeded |
| 153 |
- result.executed_commands == [] |
| 154 |
|
| 155 |
- name: Check that nothing really happened |
| 156 |
<<: *task_parameters |
| 157 |
postgresql_query: |
| 158 |
<<: *pg_parameters |
| 159 |
query: "SELECT * FROM pg_database WHERE datname = '{{ db_source_name }}'" |
| 160 |
|
| 161 |
- assert: |
| 162 |
that: |
| 163 |
- result.rowcount == 1 |
| 164 |
|
| 165 |
- name: Check that nothing really happened |
| 166 |
<<: *task_parameters |
| 167 |
postgresql_query: |
| 168 |
<<: *pg_parameters |
| 169 |
query: "SELECT * FROM pg_database WHERE datname = '{{ db_target_name }}'" |
| 170 |
|
| 171 |
- assert: |
| 172 |
that: |
| 173 |
- result.rowcount == 0 |
| 174 |
|
| 175 |
- name: Rename DB in actual mode |
| 176 |
<<: *task_parameters |
| 177 |
postgresql_db: |
| 178 |
<<: *pg_parameters |
| 179 |
name: '{{ db_source_name }}' |
| 180 |
state: rename |
| 181 |
target: '{{ db_target_name}}' |
| 182 |
|
| 183 |
- assert: |
| 184 |
that: |
| 185 |
- result is changed |
| 186 |
- result.executed_commands == ['ALTER DATABASE "{{ db_source_name }}" RENAME TO "{{ db_target_name}}"'] |
| 187 |
|
| 188 |
- name: Check the changes have been made |
| 189 |
<<: *task_parameters |
| 190 |
postgresql_query: |
| 191 |
<<: *pg_parameters |
| 192 |
query: "SELECT * FROM pg_database WHERE datname = '{{ db_source_name }}'" |
| 193 |
|
| 194 |
- assert: |
| 195 |
that: |
| 196 |
- result.rowcount == 0 |
| 197 |
|
| 198 |
- name: Check the changes have been made |
| 199 |
<<: *task_parameters |
| 200 |
postgresql_query: |
| 201 |
<<: *pg_parameters |
| 202 |
query: "SELECT * FROM pg_database WHERE datname = '{{ db_target_name }}'" |
| 203 |
|
| 204 |
- assert: |
| 205 |
that: |
| 206 |
- result.rowcount == 1 |
| 207 |
|
| 208 |
- name: Try to rename same DBs again in check mode |
| 209 |
<<: *task_parameters |
| 210 |
postgresql_db: |
| 211 |
<<: *pg_parameters |
| 212 |
name: '{{ db_source_name }}' |
| 213 |
state: rename |
| 214 |
target: '{{ db_target_name}}' |
| 215 |
check_mode: true |
| 216 |
|
| 217 |
- assert: |
| 218 |
that: |
| 219 |
- result is not changed |
| 220 |
- result.executed_commands == [] |
| 221 |
|
| 222 |
- name: Try to rename same DBs again in actual mode |
| 223 |
<<: *task_parameters |
| 224 |
postgresql_db: |
| 225 |
<<: *pg_parameters |
| 226 |
name: '{{ db_source_name }}' |
| 227 |
state: rename |
| 228 |
target: '{{ db_target_name}}' |
| 229 |
|
| 230 |
- assert: |
| 231 |
that: |
| 232 |
- result is not changed |
| 233 |
- result.executed_commands == [] |
| 234 |
|
| 235 |
- name: Check the state is the same |
| 236 |
<<: *task_parameters |
| 237 |
postgresql_query: |
| 238 |
<<: *pg_parameters |
| 239 |
query: "SELECT * FROM pg_database WHERE datname = '{{ db_source_name }}'" |
| 240 |
|
| 241 |
- assert: |
| 242 |
that: |
| 243 |
- result.rowcount == 0 |
| 244 |
|
| 245 |
- name: Check the state is the same |
| 246 |
<<: *task_parameters |
| 247 |
postgresql_query: |
| 248 |
<<: *pg_parameters |
| 249 |
query: "SELECT * FROM pg_database WHERE datname = '{{ db_target_name }}'" |
| 250 |
|
| 251 |
- assert: |
| 252 |
that: |
| 253 |
- result.rowcount == 1 |
| 254 |
|
| 255 |
# Clean up |
| 256 |
- name: Remove test DB |
| 257 |
<<: *task_parameters |
| 258 |
postgresql_db: |
| 259 |
<<: *pg_parameters |
| 260 |
name: '{{ db_target_name }}' |
| 261 |
state: absent |