Skip to content

Commit

Permalink
Fix Force DB Drop command
Browse files Browse the repository at this point in the history
  • Loading branch information
rooftopcellist committed Jan 31, 2024
1 parent 11e0563 commit bc6bf42
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
55 changes: 31 additions & 24 deletions roles/restore/tasks/postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,34 @@
-p {{ eda_postgres_port }}
no_log: "{{ no_log }}"

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

- name: Set create db command
set_fact:
pg_create_db: >-
echo 'CREATE DATABASE {{ eda_postgres_database }} WITH OWNER = {{ eda_postgres_user }};' | PGPASSWORD='{{ eda_postgres_pass }}' psql
-U {{ eda_postgres_user }}
-h {{ resolvable_db_host }}
-d postgres
-p {{ eda_postgres_port }}
no_log: "{{ no_log }}"
- name: Set create db command
set_fact:
pg_create_db: >-
echo 'CREATE DATABASE {{ eda_postgres_database }} WITH OWNER = {{ eda_postgres_user }};' | PGPASSWORD='{{ eda_postgres_pass }}' psql
-U {{ eda_postgres_user }}
-h {{ resolvable_db_host }}
-d postgres
-p {{ eda_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 @@ -122,13 +131,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 }}/eda.db | PGPASSWORD='{{ eda_postgres_pass }}' {{ pg_restore }}
echo 'Successful'
PG_RC=$?
set +e +o pipefail
exit $PG_RC
"
register: data_migration
no_log: "{{ no_log }}"
failed_when: "'Successful' not in data_migration.stdout"
3 changes: 3 additions & 0 deletions roles/restore/vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ _postgres_image_version: latest

backup_api_version: '{{ deployment_type }}.ansible.com/v1alpha1'
backup_kind: 'EDABackup'

force_drop_db: false
pg_drop_create: ''

0 comments on commit bc6bf42

Please sign in to comment.