Skip to content

Commit

Permalink
Update psql-db-copy-aurora-job.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoaugustine authored Jul 10, 2024
1 parent 12cb474 commit 58079e9
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions kube/services/jobs/psql-db-copy-aurora-job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ spec:
# 4. The source and the destination databases should be on the same Aurora instance.
#
# 5. The ServiceAccount, roles, and role binding must be set up using the script psql-db-copy-aurora-sa.yaml.
# The psql-db-copy-aurora-sa.yaml script is configured for the default namespace.
# The psql-db-copy-aurora-sa.yaml script is configured for the default namespace.
# Modify the namespace as needed before applying it where the script will run.
# These can be created by executing the command:
# kubectl apply -f ${GEN3_HOME}/kube/services/jobs/psql-db-copy-aurora-sa.yaml
Expand Down Expand Up @@ -93,6 +93,16 @@ spec:
exit 1
fi
# Function to truncate to 63 characters
function truncate_identifier() {
local identifier=$1
if [ ${#identifier} -gt 63 ]; then
echo "${identifier:0:63}"
else
echo "$identifier"
fi
}
# Function to decode Kubernetes secrets
function secrets_decode() {
local namespace=$1
Expand Down Expand Up @@ -153,19 +163,19 @@ spec:
gen3_log_err "One or more required credentials are missing for $database. Skipping."
continue
fi
target_db=$(truncate_identifier $(echo "${database}_${namespace}_${date_str}" | tr '-' '_'))
gen3_log_info "Processing database: $database"
gen3_log_info "Source DB: $source_db_database, Username: $db_username, Current DB: $db_database, Target DB: ${database}_${namespace}_${date_str}"
gen3_log_info "Source DB: $source_db_database, Username: $db_username, Current DB: $db_database, Target DB: $target_db"
# DB commands
gen3 psql aurora -c "GRANT $db_username TO $aurora_master_username"
gen3 psql aurora -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '$source_db_database' AND pid <> pg_backend_pid()"
gen3 psql aurora -c "CREATE DATABASE ${database}_${namespace}_${date_str} WITH TEMPLATE $source_db_database OWNER $db_username"
gen3 psql aurora -c "CREATE DATABASE $target_db WITH TEMPLATE $source_db_database OWNER $db_username"
pg_command="DO \$\$ DECLARE tbl record; BEGIN FOR tbl IN (SELECT table_schema || '.' || table_name AS full_table_name FROM information_schema.tables WHERE table_schema = 'public') LOOP EXECUTE 'ALTER TABLE ' || tbl.full_table_name || ' OWNER TO $db_username;'; END LOOP; END \$\$;"
PGPASSWORD=${aurora_master_password} psql -h $aurora_host_name -U $aurora_master_username -d "${database}_${namespace}_${date_str}" -c "$pg_command"
PGPASSWORD=${aurora_master_password} psql -h $aurora_host_name -U $aurora_master_username -d "$target_db" -c "$pg_command"
if [ $? -eq 0 ]; then
gen3_log_info "Successfully processed $database"
new_databases+=("${database}_${namespace}_${date_str}")
new_databases+=("$target_db")
else
gen3_log_err "Failed to process $database"
fi
Expand Down

0 comments on commit 58079e9

Please sign in to comment.