Skip to content

Commit

Permalink
Fix PG Restore Force Drop DB flag
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
rooftopcellist committed Dec 8, 2023
1 parent ef17865 commit df9b978
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
56 changes: 31 additions & 25 deletions roles/restore/tasks/postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,41 @@
- name: Set pg_restore command
set_fact:
pg_restore: >-
pg_restore --clean --if-exists
pg_restore
-U {{ awx_postgres_user }}
-h {{ resolvable_db_host }}
-d {{ awx_postgres_database }}
-p {{ awx_postgres_port }}
no_log: "{{ no_log }}"

- name: Set drop db command
set_fact:
pg_drop_db: >-
echo 'DROP DATABASE {{ awx_postgres_database }} WITH (FORCE);' | PGPASSWORD='{{ awx_postgres_pass }}' psql
-U {{ awx_postgres_user }}
-h {{ resolvable_db_host }}
-d postgres
-p {{ awx_postgres_port }}
no_log: "{{ no_log }}"
- name: Force drop and create database if force_drop_db is true
block:
- name: Set drop db command

Check warning on line 87 in roles/restore/tasks/postgres.yml

View workflow job for this annotation

GitHub Actions / molecule (--skip-tags=replicas)

87:3 [indentation] wrong indentation: expected 4 but found 2

Check warning on line 87 in roles/restore/tasks/postgres.yml

View workflow job for this annotation

GitHub Actions / molecule (-t replicas)

87:3 [indentation] wrong indentation: expected 4 but found 2
set_fact:
pg_drop_db: >-
echo 'DROP DATABASE {{ awx_postgres_database }} WITH (FORCE);' | PGPASSWORD='{{ awx_postgres_pass }}' psql
-U {{ awx_postgres_user }}
-h {{ resolvable_db_host }}
-d postgres
-p {{ awx_postgres_port }} ;
no_log: "{{ no_log }}"

- name: Set create db command
set_fact:
pg_create_db: >-
echo 'CREATE DATABASE {{ awx_postgres_database }} WITH OWNER = {{ awx_postgres_user }};' | PGPASSWORD='{{ awx_postgres_pass }}' psql
-U {{ awx_postgres_user }}
-h {{ resolvable_db_host }}
-d postgres
-p {{ awx_postgres_port }}
no_log: "{{ no_log }}"
- name: Set create db command
set_fact:
pg_create_db: >-
echo 'CREATE DATABASE {{ awx_postgres_database }} WITH OWNER = {{ awx_postgres_user }};' | PGPASSWORD='{{ awx_postgres_pass }}' psql
-U {{ awx_postgres_user }}
-h {{ resolvable_db_host }}
-d postgres
-p {{ awx_postgres_port }} ;
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
k8s_exec:
Expand All @@ -124,14 +133,11 @@
trap 'end_keepalive \"$keepalive_file\" \"$keepalive_pid\"' EXIT SIGINT SIGTERM
echo keepalive_pid: $keepalive_pid
set -e -o pipefail
if {{ force_drop_db }}; then
{{ pg_drop_db }}
{{ pg_create_db }}
fi
{{ pg_drop_create }}
cat {{ backup_dir }}/tower.db | PGPASSWORD='{{ awx_postgres_pass }}' {{ pg_restore }}
PG_RC=$?
set +e +o pipefail
echo 'Successful'
exit $PG_RC
"
register: data_migration
no_log: "{{ no_log }}"
failed_when: "'Successful' not in data_migration.stdout"
4 changes: 4 additions & 0 deletions roles/restore/vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ broadcast_websocket_secret: '{{ deployment_name }}-broadcast-websocket'
postgres_configuration_secret: '{{ deployment_name }}-postgres-configuration'
supported_pg_version: 13
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: ''

0 comments on commit df9b978

Please sign in to comment.