Fix PG Restore Force Drop DB flag (#1665)

- Previously, if the flag was set to true, the bash conditional failed
  because the boolean was not correctly interpreted.
- Use pg_restore return code to determine if the task should be marked as failed

Signed-off-by: Christian M. Adams <chadams@redhat.com>
This commit is contained in:
Christian Adams
2023-12-08 16:38:47 -05:00
committed by GitHub
parent ef17865f12
commit d0c7f343b2
2 changed files with 34 additions and 24 deletions

View File

@@ -82,25 +82,34 @@
-p {{ awx_postgres_port }} -p {{ awx_postgres_port }}
no_log: "{{ no_log }}" no_log: "{{ no_log }}"
- name: Set drop db command - name: Force drop and create database if force_drop_db is true
set_fact: block:
pg_drop_db: >- - name: Set drop db command
echo 'DROP DATABASE {{ awx_postgres_database }} WITH (FORCE);' | PGPASSWORD='{{ awx_postgres_pass }}' psql set_fact:
-U {{ awx_postgres_user }} pg_drop_db: >-
-h {{ resolvable_db_host }} echo 'DROP DATABASE {{ awx_postgres_database }} WITH (FORCE);' | PGPASSWORD='{{ awx_postgres_pass }}' psql
-d postgres -U {{ awx_postgres_user }}
-p {{ awx_postgres_port }} -h {{ resolvable_db_host }}
no_log: "{{ no_log }}" -d postgres
-p {{ awx_postgres_port }} ;
no_log: "{{ no_log }}"
- name: Set create db command - name: Set create db command
set_fact: set_fact:
pg_create_db: >- pg_create_db: >-
echo 'CREATE DATABASE {{ awx_postgres_database }} WITH OWNER = {{ awx_postgres_user }};' | PGPASSWORD='{{ awx_postgres_pass }}' psql echo 'CREATE DATABASE {{ awx_postgres_database }} WITH OWNER = {{ awx_postgres_user }};' | PGPASSWORD='{{ awx_postgres_pass }}' psql
-U {{ awx_postgres_user }} -U {{ awx_postgres_user }}
-h {{ resolvable_db_host }} -h {{ resolvable_db_host }}
-d postgres -d postgres
-p {{ awx_postgres_port }} -p {{ awx_postgres_port }} ;
no_log: "{{ no_log }}" no_log: "{{ no_log }}"
- name: Set complete pg restore command
set_fact:
pg_drop_create: >-
{{ pg_drop_db }}
{{ pg_create_db }}
when: force_drop_db
- name: Restore database dump to the new postgresql container - name: Restore database dump to the new postgresql container
k8s_exec: k8s_exec:
@@ -124,14 +133,11 @@
trap 'end_keepalive \"$keepalive_file\" \"$keepalive_pid\"' EXIT SIGINT SIGTERM trap 'end_keepalive \"$keepalive_file\" \"$keepalive_pid\"' EXIT SIGINT SIGTERM
echo keepalive_pid: $keepalive_pid echo keepalive_pid: $keepalive_pid
set -e -o pipefail set -e -o pipefail
if {{ force_drop_db }}; then {{ pg_drop_create }}
{{ pg_drop_db }}
{{ pg_create_db }}
fi
cat {{ backup_dir }}/tower.db | PGPASSWORD='{{ awx_postgres_pass }}' {{ pg_restore }} cat {{ backup_dir }}/tower.db | PGPASSWORD='{{ awx_postgres_pass }}' {{ pg_restore }}
PG_RC=$?
set +e +o pipefail set +e +o pipefail
echo 'Successful' exit $PG_RC
" "
register: data_migration register: data_migration
no_log: "{{ no_log }}" no_log: "{{ no_log }}"
failed_when: "'Successful' not in data_migration.stdout"

View File

@@ -14,3 +14,7 @@ broadcast_websocket_secret: '{{ deployment_name }}-broadcast-websocket'
postgres_configuration_secret: '{{ deployment_name }}-postgres-configuration' postgres_configuration_secret: '{{ deployment_name }}-postgres-configuration'
supported_pg_version: 13 supported_pg_version: 13
image_pull_policy: IfNotPresent image_pull_policy: IfNotPresent
# If set to true, the restore process will delete the existing database and create a new one
force_drop_db: false
pg_drop_create: ''