helpers2.1: further simplify mysql/postgresql helper: no keyword arg, remove ynh_foo_setup_db and ynh_foo_remove_db (the other helpers are enough), replace ynh_foosql_connect_as/ynh_foosql_execute* with a single ynh_foosql_db_shell that reads stdin

This commit is contained in:
Alexandre Aubin 2024-06-13 12:39:05 +02:00
parent 3584e6a5b1
commit 800f93d12e
4 changed files with 76 additions and 360 deletions

View file

@ -174,6 +174,19 @@ ynh_mysql_user_exists() {
fi
}
# Check if a mysql database exists
#
# [internal]
#
# usage: ynh_mysql_database_exists database
# | arg: database - the database for which to check existence
# | exit: Return 1 if the database doesn't exist, 0 otherwise
#
ynh_mysql_database_exists() {
local database=$1
mysqlshow | grep -q "^| $database "
}
# Drop a user
#
# [internal]
@ -236,7 +249,7 @@ ynh_mysql_remove_db() {
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
if mysqlshow | grep -q "^| $db_name "; then
if ynh_mysql_database_exists "$db_name"; then
ynh_mysql_drop_db $db_name
else
ynh_print_warn --message="Database $db_name not found"

View file

@ -1,76 +1,17 @@
#!/bin/bash
# Open a connection as a user
# Run SQL instructions in a database ($db_name by default)
#
# usage: ynh_mysql_execute --user=user --password=password [--database=database]
# | arg: -u, --user= - the user name to connect as (by default, $db_user)
# | arg: -p, --password= - the user password (by default, $db_pwd)
# | arg: -d, --database= - the database to connect to (by default, $db_name)
# usage: ynh_mysql_db_shell [database] <<< "instructions"
# | arg: database= - the database to connect to (by default, $db_name)
#
# examples:
# ynh_mysql_execute <<< "UPDATE ...;"
# ynh_mysql_execute < /path/to/file.sql
# ynh_mysql_db_shell $db_name <<< "UPDATE ...;"
# ynh_mysql_db_shell < /path/to/file.sql
#
# Requires YunoHost version 2.2.4 or higher.
ynh_mysql_execute() {
# ============ Argument parsing =============
local -A args_array=([u]=user= [p]=password= [d]=database=)
local user
local password
local database
ynh_handle_getopts_args "$@"
user="${database:-$db_name}"
password="${database:-$db_pwd}"
database="${database:-$db_name}"
# ===========================================
mysql --user="$user" --password="$password" --batch "$database"
}
# Execute a command as root user
#
# usage: ynh_mysql_execute_as_root --sql=sql [--database=database]
# | arg: -s, --sql= - the SQL command to execute
# | arg: -d, --database= - the database to connect to (by default, $db_name)
#
# Requires YunoHost version 2.2.4 or higher.
ynh_mysql_execute_as_root() {
# ============ Argument parsing =============
local -A args_array=([s]=sql= [d]=database=)
local sql
local database
ynh_handle_getopts_args "$@"
database="${database:-$db_name}"
# ===========================================
if [ -n "$database" ]; then
database="--database=$database"
fi
mysql -B "$database" <<<"$sql"
}
# Execute a command from a file as root user
#
# usage: ynh_mysql_execute_file_as_root --file=file [--database=database]
# | arg: -f, --file= - the file containing SQL commands
# | arg: -d, --database= - the database to connect to (by default, $db_name)
#
# Requires YunoHost version 2.2.4 or higher.
ynh_mysql_execute_file_as_root() {
# ============ Argument parsing =============
local -A args_array=([f]=file= [d]=database=)
local file
local database
ynh_handle_getopts_args "$@"
database="${database:-$db_name}"
# ===========================================
if [ -n "$database" ]; then
database="--database=$database"
fi
mysql -B "$database" <"$file"
ynh_mysql_db_shell() {
local database=${1:-$db_name}
mysql -B $database
}
# Create a database and grant optionnaly privilegies to a user
@ -82,7 +23,6 @@ ynh_mysql_execute_file_as_root() {
# | arg: user - the user to grant privilegies
# | arg: pwd - the password to identify user by
#
# Requires YunoHost version 2.2.4 or higher.
ynh_mysql_create_db() {
local db=$1
@ -97,7 +37,7 @@ ynh_mysql_create_db() {
sql+=" WITH GRANT OPTION;"
fi
ynh_mysql_execute_as_root --sql="$sql"
mysql -B <<< "$sql"
}
# Drop a database
@ -110,28 +50,20 @@ ynh_mysql_create_db() {
# usage: ynh_mysql_drop_db db
# | arg: db - the database name to drop
#
# Requires YunoHost version 2.2.4 or higher.
ynh_mysql_drop_db() {
ynh_mysql_execute_as_root --sql="DROP DATABASE ${1};"
mysql -B <<< "DROP DATABASE ${1};"
}
# Dump a database
#
# usage: ynh_mysql_dump_db --database=database
# usage: ynh_mysql_dump_db database
# | arg: -d, --database= - the database name to dump (by default, $db_name)
# | ret: The mysqldump output
#
# example: ynh_mysql_dump_db --database=roundcube > ./dump.sql
# example: ynh_mysql_dump_db "roundcube" > ./dump.sql
#
# Requires YunoHost version 2.2.4 or higher.
ynh_mysql_dump_db() {
# ============ Argument parsing =============
local -A args_array=([d]=database=)
local database
ynh_handle_getopts_args "$@"
database="${database:-$db_name}"
# ===========================================
local database=${1:-$db_name}
mysqldump --single-transaction --skip-dump-date --routines "$database"
}
@ -143,33 +75,31 @@ ynh_mysql_dump_db() {
# | arg: user - the user name to create
# | arg: pwd - the password to identify user by
#
# Requires YunoHost version 2.2.4 or higher.
ynh_mysql_create_user() {
ynh_mysql_execute_as_root \
--sql="CREATE USER '${1}'@'localhost' IDENTIFIED BY '${2}';"
mysql -B <<< "CREATE USER '${1}'@'localhost' IDENTIFIED BY '${2}';"
}
# Check if a mysql user exists
#
# [internal]
#
# usage: ynh_mysql_user_exists --user=user
# | arg: -u, --user= - the user for which to check existence
# usage: ynh_mysql_user_exists user
# | arg: user= - the user for which to check existence
# | ret: 0 if the user exists, 1 otherwise.
#
# Requires YunoHost version 2.2.4 or higher.
ynh_mysql_user_exists() {
# ============ Argument parsing =============
local -A args_array=([u]=user=)
local user
ynh_handle_getopts_args "$@"
# ===========================================
local user=$1
[[ -n "$(mysql -B <<< "SELECT User from mysql.user WHERE User = '$user';")" ]]
}
if [[ -z $(ynh_mysql_execute_as_root --sql="SELECT User from mysql.user WHERE User = '$user';") ]]; then
return 1
else
return 0
fi
# Check if a mysql database exists
#
# usage: ynh_mysql_database_exists database
# | arg: database - the database for which to check existence
# | exit: Return 1 if the database doesn't exist, 0 otherwise
#
ynh_mysql_database_exists() {
local database=$1
mysqlshow | grep -q "^| $database "
}
# Drop a user
@ -179,70 +109,6 @@ ynh_mysql_user_exists() {
# usage: ynh_mysql_drop_user user
# | arg: user - the user name to drop
#
# Requires YunoHost version 2.2.4 or higher.
ynh_mysql_drop_user() {
ynh_mysql_execute_as_root --sql="DROP USER '${1}'@'localhost';"
}
# Create a database, an user and its password. Then store the password in the app's config
#
# [packagingv1]
#
# usage: ynh_mysql_setup_db --db_user=user --db_name=name [--db_pwd=pwd]
# | arg: -u, --db_user= - Owner of the database
# | arg: -n, --db_name= - Name of the database
# | arg: -p, --db_pwd= - Password of the database. If not provided, a password will be generated
#
# After executing this helper, the password of the created database will be available in `$db_pwd`
# It will also be stored as "`mysqlpwd`" into the app settings.
#
# Requires YunoHost version 2.6.4 or higher.
ynh_mysql_setup_db() {
# ============ Argument parsing =============
local -A args_array=([u]=db_user= [n]=db_name= [p]=db_pwd=)
local db_user
local db_name
db_pwd=""
ynh_handle_getopts_args "$@"
# ===========================================
# Generate a random password
local new_db_pwd=$(ynh_string_random)
# If $db_pwd is not provided, use new_db_pwd instead for db_pwd
db_pwd="${db_pwd:-$new_db_pwd}"
# Dirty patch for super-legacy apps
dpkg --list | grep -q "^ii mariadb-server" || { ynh_print_warn --message="Packager: you called ynh_mysql_setup_db without declaring a dependency to mariadb-server. Please add it to your apt dependencies !"; ynh_apt install mariadb-server; }
ynh_mysql_create_db "$db_name" "$db_user" "$db_pwd"
ynh_app_setting_set --key=mysqlpwd --value=$db_pwd
}
# Remove a database if it exists, and the associated user
#
# [packagingv1]
#
# usage: ynh_mysql_remove_db --db_user=user --db_name=name
# | arg: -u, --db_user= - Owner of the database
# | arg: -n, --db_name= - Name of the database
#
# Requires YunoHost version 2.6.4 or higher.
ynh_mysql_remove_db() {
# ============ Argument parsing =============
local -Ar args_array=([u]=db_user= [n]=db_name=)
local db_user
local db_name
ynh_handle_getopts_args "$@"
# ===========================================
if mysqlshow | grep -q "^| $db_name "; then
ynh_mysql_drop_db $db_name
else
ynh_print_warn --message="Database $db_name not found"
fi
# Remove mysql user if it exists
if ynh_mysql_user_exists --user=$db_user; then
ynh_mysql_drop_user $db_user
fi
mysql -B <<< "DROP USER '${1}'@'localhost';"
}

View file

@ -3,79 +3,18 @@
PSQL_ROOT_PWD_FILE=/etc/yunohost/psql
PSQL_VERSION=13
# Open a connection as a user
# Run SQL instructions in a database ($db_name by default)
#
# usage: ynh_psql_execute --user=user --password=password [--database=database]
# | arg: -u, --user= - the user name to connect as (by default, $db_user)
# | arg: -p, --password= - the user password (by default, $db_pw)
# | arg: -d, --database= - the database to connect to (by default, $db_name)
# usage: ynh_psql_db_shell database <<< "instructions"
# | arg: database - the database to connect to (by default, $db_name)
#
# examples:
# ynh_psql_execute <<< "UPDATE ...;"
# ynh_psql_execute < /path/to/file.sql
# ynh_psql_db_shell $db_name <<< "UPDATE ...;"
# ynh_psql_db_shell < /path/to/file.sql
#
# Requires YunoHost version 3.5.0 or higher.
ynh_psql_execute() {
# ============ Argument parsing =============
local -A args_array=([u]=user= [p]=password= [d]=database=)
local user
local password
local database
ynh_handle_getopts_args "$@"
user="${user:-$db_user}"
password="${password:-$db_pwd}"
database="${database:-$db_name}"
# ===========================================
sudo --login --user=postgres PGUSER="$user" PGPASSWORD="$password" psql "$database"
}
# Execute a command as root user
#
# usage: ynh_psql_execute_as_root --sql=sql [--database=database]
# | arg: -s, --sql= - the SQL command to execute
# | arg: -d, --database= - the database to connect to (by default, $db_name)
#
# Requires YunoHost version 3.5.0 or higher.
ynh_psql_execute_as_root() {
# ============ Argument parsing =============
local -A args_array=([s]=sql= [d]=database=)
local sql
local database
ynh_handle_getopts_args "$@"
database="${database:-$db_name}"
# ===========================================
if [ -n "$database" ]; then
database="--database=$database"
fi
ynh_psql_execute --user="postgres" --password="$(cat $PSQL_ROOT_PWD_FILE)" \
--database="$database" <<<"$sql"
}
# Execute a command from a file as root user
#
# usage: ynh_psql_execute_file_as_root --file=file [--database=database]
# | arg: -f, --file= - the file containing SQL commands
# | arg: -d, --database= - the database to connect to (by default, $db_name)
#
# Requires YunoHost version 3.5.0 or higher.
ynh_psql_execute_file_as_root() {
# ============ Argument parsing =============
local -A args_array=([f]=file= [d]=database=)
local file
local database
ynh_handle_getopts_args "$@"
database="${database:-$db_name}"
# ===========================================
if [ -n "$database" ]; then
database="--database=$database"
fi
ynh_psql_execute --user="postgres" --password="$(cat $PSQL_ROOT_PWD_FILE)" \
--database="$database" <"$file"
ynh_psql_db_shell() {
local database="${1:-$db_name}"
sudo --login --user=postgres psql "$database"
}
# Create a database and grant optionnaly privilegies to a user
@ -86,7 +25,6 @@ ynh_psql_execute_file_as_root() {
# | arg: db - the database name to create
# | arg: user - the user to grant privilegies
#
# Requires YunoHost version 3.5.0 or higher.
ynh_psql_create_db() {
local db=$1
local user=${2:-}
@ -99,7 +37,7 @@ ynh_psql_create_db() {
sql+="GRANT ALL PRIVILEGES ON DATABASE ${db} TO ${user} WITH GRANT OPTION;"
fi
ynh_psql_execute_as_root --sql="$sql"
sudo --login --user=postgres psql <<< "$sql"
}
# Drop a database
@ -112,33 +50,25 @@ ynh_psql_create_db() {
# usage: ynh_psql_drop_db db
# | arg: db - the database name to drop
#
# Requires YunoHost version 3.5.0 or higher.
ynh_psql_drop_db() {
local db=$1
# First, force disconnection of all clients connected to the database
# https://stackoverflow.com/questions/17449420/postgresql-unable-to-drop-database-because-of-some-auto-connections-to-db
ynh_psql_execute_as_root --sql="REVOKE CONNECT ON DATABASE $db FROM public;" --database="$db"
ynh_psql_execute_as_root --sql="SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '$db' AND pid <> pg_backend_pid();" --database="$db"
sudo --login --user=postgres psql $db <<< "REVOKE CONNECT ON DATABASE $db FROM public;"
sudo --login --user=postgres psql $db <<< "SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '$db' AND pid <> pg_backend_pid();"
sudo --login --user=postgres dropdb $db
}
# Dump a database
#
# usage: ynh_psql_dump_db --database=database
# | arg: -d, --database= - the database name to dump (by default, $db_name)
# usage: ynh_psql_dump_db database
# | arg: database - the database name to dump (by default, $db_name)
# | ret: the psqldump output
#
# example: ynh_psql_dump_db 'roundcube' > ./dump.sql
#
# Requires YunoHost version 3.5.0 or higher.
ynh_psql_dump_db() {
# ============ Argument parsing =============
local -A args_array=([d]=database=)
local database
ynh_handle_getopts_args "$@"
database="${database:-$db_name}"
# ===========================================
local database="${1:-$db_name}"
sudo --login --user=postgres pg_dump "$database"
}
@ -150,62 +80,32 @@ ynh_psql_dump_db() {
# | arg: user - the user name to create
# | arg: pwd - the password to identify user by
#
# Requires YunoHost version 3.5.0 or higher.
ynh_psql_create_user() {
local user=$1
local pwd=$2
ynh_psql_execute_as_root --sql="CREATE USER $user WITH ENCRYPTED PASSWORD '$pwd'"
sudo --login --user=postgres psql <<< "CREATE USER $user WITH ENCRYPTED PASSWORD '$pwd'"
}
# Check if a psql user exists
#
# [packagingv1]
#
# usage: ynh_psql_user_exists --user=user
# | arg: -u, --user= - the user for which to check existence
# usage: ynh_psql_user_exists user
# | arg: user= - the user for which to check existence
# | exit: Return 1 if the user doesn't exist, 0 otherwise
#
# Requires YunoHost version 3.5.0 or higher.
ynh_psql_user_exists() {
# ============ Argument parsing =============
local -A args_array=([u]=user=)
local user
ynh_handle_getopts_args "$@"
# ===========================================
if ! sudo --login --user=postgres PGUSER="postgres" PGPASSWORD="$(cat $PSQL_ROOT_PWD_FILE)" psql -tAc "SELECT rolname FROM pg_roles WHERE rolname='$user';" | grep --quiet "$user"; then
return 1
else
return 0
fi
local user=$1
sudo --login --user=postgres psql -tAc "SELECT rolname FROM pg_roles WHERE rolname='$user';" | grep --quiet "$user"
}
# Check if a psql database exists
#
# usage: ynh_psql_database_exists --database=database
# | arg: -d, --database= - the database for which to check existence (by default, $db_name)
# usage: ynh_psql_database_exists database
# | arg: database - the database for which to check existence
# | exit: Return 1 if the database doesn't exist, 0 otherwise
#
# Requires YunoHost version 3.5.0 or higher.
ynh_psql_database_exists() {
# ============ Argument parsing =============
local -A args_array=([d]=database=)
local database
ynh_handle_getopts_args "$@"
database="${database:-$db_name}"
# ===========================================
# if psql is not there, we cannot check the db
# though it could exists.
if ! command -v psql
then
ynh_print_warn --message="PostgreSQL is not installed, impossible to check for db existence."
return 1
elif ! sudo --login --user=postgres PGUSER="postgres" PGPASSWORD="$(cat $PSQL_ROOT_PWD_FILE)" psql -tAc "SELECT datname FROM pg_database WHERE datname='$database';" | grep --quiet "$database"; then
return 1
else
return 0
fi
local database=$1
sudo --login --user=postgres psql -tAc "SELECT datname FROM pg_database WHERE datname='$database';" | grep --quiet "$database"
}
# Drop a user
@ -215,72 +115,6 @@ ynh_psql_database_exists() {
# usage: ynh_psql_drop_user user
# | arg: user - the user name to drop
#
# Requires YunoHost version 3.5.0 or higher.
ynh_psql_drop_user() {
ynh_psql_execute_as_root --sql="DROP USER ${1};"
}
# Create a database, an user and its password. Then store the password in the app's config
#
# [packagingv1]
#
# usage: ynh_psql_setup_db --db_user=user --db_name=name [--db_pwd=pwd]
# | arg: -u, --db_user= - Owner of the database
# | arg: -n, --db_name= - Name of the database
# | arg: -p, --db_pwd= - Password of the database. If not provided, a password will be generated
#
# After executing this helper, the password of the created database will be available in $db_pwd
#
# Requires YunoHost version 2.7.13 or higher.
ynh_psql_setup_db() {
# ============ Argument parsing =============
local -A args_array=([u]=db_user= [n]=db_name= [p]=db_pwd=)
local db_user
local db_name
db_pwd=""
ynh_handle_getopts_args "$@"
# ===========================================
if ! ynh_psql_user_exists --user=$db_user; then
local new_db_pwd=$(ynh_string_random) # Generate a random password
# If $db_pwd is not provided, use new_db_pwd instead for db_pwd
db_pwd="${db_pwd:-$new_db_pwd}"
ynh_psql_create_user "$db_user" "$db_pwd"
elif [ -z $db_pwd ]; then
ynh_die --message="The user $db_user exists, please provide his password"
fi
ynh_psql_create_db "$db_name" "$db_user" # Create the database
}
# Remove a database if it exists, and the associated user
#
# [packagingv1]
#
# usage: ynh_psql_remove_db --db_user=user --db_name=name
# | arg: -u, --db_user= - Owner of the database
# | arg: -n, --db_name= - Name of the database
#
# Requires YunoHost version 2.7.13 or higher.
ynh_psql_remove_db() {
# ============ Argument parsing =============
local -A args_array=([u]=db_user= [n]=db_name=)
local db_user
local db_name
ynh_handle_getopts_args "$@"
# ===========================================
if ynh_psql_database_exists --database=$db_name; then # Check if the database exists
ynh_psql_drop_db $db_name # Remove the database
else
ynh_print_warn --message="Database $db_name not found"
fi
# Remove psql user if it exists
if ynh_psql_user_exists --user=$db_user; then
ynh_psql_drop_user $db_user
else
ynh_print_warn --message="User $db_user not found"
fi
sudo --login --user=postgres psql <<< "DROP USER ${1};"
}

View file

@ -1455,12 +1455,15 @@ class DatabaseAppResource(AppResource):
db_name = self.get_setting("db_name") or db_user
if self.dbtype == "mysql":
self._run_script(
"deprovision", f"ynh_mysql_remove_db '{db_name}' '{db_user}'"
)
db_helper_name = "mysql"
elif self.dbtype == "postgresql":
db_helper_name = "psql"
self._run_script(
"deprovision", f"ynh_psql_remove_db '{db_name}' '{db_user}'"
"deprovision", f"""
ynh_{db_helper_name}_database_exists "{db_name}" && ynh_{db_helper_name}_drop_db "{db_name}" || true
ynh_{db_helper_name}_user_exists "{db_user}" && ynh_{db_helper_name}_drop_user "{db_user}" || true
"""
)
self.delete_setting("db_name")