From b1fac105172e49a123f84413bdc76deb382c0ca9 Mon Sep 17 00:00:00 2001 From: oleole39 Date: Thu, 19 Jan 2023 03:54:09 +0100 Subject: [PATCH] Modified the function removing PostgreSQL databases in the 'remove' script to take into account that this app creates several databases by default and not a unique one --- scripts/remove | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/scripts/remove b/scripts/remove index 6e562aa..289c409 100755 --- a/scripts/remove +++ b/scripts/remove @@ -22,12 +22,35 @@ db_user=$db_name final_path=$(ynh_app_setting_get --app=$app --key=final_path) #================================================= -# REMOVE THE POSTGRESQL DATABASE +# REMOVE THE POSTGRESQL DATABASE(S) & ROLE #================================================= -ynh_script_progression --message="Removing the PostgreSQL database" +ynh_script_progression --message="Removing all associated PostgreSQL database(s) and role" -# Remove a database if it exists, along with the associated user -ynh_psql_remove_db --db_user=$db_user --db_name=$db_name +# Remove all existing databases associated with the app's dedicated user a database if it exists +sql="COPY (SELECT datname FROM pg_database JOIN pg_authid ON pg_database.datdba = pg_authid.oid WHERE rolname = '${db_user}') TO STDOUT" +dbs_to_drop=ynh_psql_execute_as_root --sql="$sql" #Fetch database(s) associated to role $db_user + +if [[ -n "$dbs_to_drop" ]]; then # Check that the list of database(s) is not empty + for $db_name in $dbs_to_drop # Iterate through the list of database(s) to remove + do + if ynh_psql_database_exists --database=$db_name; then # Check if the database exists + ynh_psql_drop_db $db_name # Remove the database + ynh_print_info --message="Removed database $db_name associated to role $db_user" + else + ynh_print_warn --message="Database $db_name not found" + fi + done +else + ynh_print_warn --message="No associated database to role $db_user was found" +fi + +# Remove dedicated PostgreSQL role +if ynh_psql_user_exists --user=$db_user; then + ynh_psql_drop_user $db_user + ynh_print_info --message="Removed PostgreSQL role $db_user" +else + ynh_print_warn --message="User $db_user not found" +fi #================================================= # REMOVE APP MAIN DIR