From 0000d64060650ec1295927863ac19a6f4fe36e5f Mon Sep 17 00:00:00 2001 From: Sylvain Date: Sun, 8 Dec 2024 13:52:10 +0100 Subject: [PATCH] Improve psql --- manifest.toml | 11 +++++------ scripts/_common.sh | 25 +++++++++++++++++++++---- scripts/install | 2 ++ scripts/restore | 2 ++ scripts/upgrade | 12 +++++++++++- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/manifest.toml b/manifest.toml index a78119c..f1ed827 100644 --- a/manifest.toml +++ b/manifest.toml @@ -7,7 +7,7 @@ name = "Immich" description.en = "Photo and video backup solution directly from your mobile phone" description.fr = "Sauvegarde de photos et de vidéos directement depuis votre mobile" -version = "1.122.1~ynh1" +version = "1.122.1~ynh2" maintainers = ["ewilly"] @@ -126,10 +126,9 @@ ram.runtime = "500M" "libpq-dev", "postgresql-16", "postgresql-16-pgvector", - "postgresql-client-16", - "postgresql-common", - "postgresql-client-common" + "postgresql-client-16" ] - [resources.database] - type = "postgresql" + # Commented to avoid provisionning a db on wrong cluster + #[resources.database] + #type = "postgresql" diff --git a/scripts/_common.sh b/scripts/_common.sh index d851e52..903cb40 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -15,6 +15,9 @@ postgresql_version() { ynh_read_manifest "resources.apt.extras.postgresql.packages" \ | grep -o 'postgresql-[0-9][0-9]-pgvector' | head -n1 | cut -d'-' -f2 } +postgresql_cluster_port() { + pg_lsclusters --no-header | grep "^$postgresql_version" | cut -d' ' -f3 +} # Retrieve full latest python version from major version # usage: py_latest_from_major --python="3.8" @@ -295,9 +298,7 @@ myynh_drop_psql_db() { # Dump the database myynh_dump_psql_db() { - local db_port=$(ynh_app_setting_get --key=psql_port) - - sudo --login --user=postgres pg_dump --port="$db_port" --dbname="$app" > db.sql + sudo --login --user=postgres pg_dump --cluster="$(postgresql_version)/main" --dbname="$app" > db.sql } # Restore the database @@ -312,7 +313,7 @@ myynh_restore_psql_db() { # Set permissions -myynh_set_permissions () { +myynh_set_permissions() { chown -R $app: "$install_dir" chmod u=rwX,g=rX,o= "$install_dir" chmod -R o-rwx "$install_dir" @@ -327,3 +328,19 @@ myynh_set_permissions () { chown -R $app: "/var/log/$app" chmod u=rw,g=r,o= "/var/log/$app" } + +myynh_set_default_psql_cluster_to_debian_default() { + local default_port=5432 + local config_file="/etc/postgresql-common/user_clusters" + + #retrieve informations about default psql cluster + default_psql_version=$(pg_lsclusters --no-header | grep "$default_port" | cut -d' ' -f1) + default_psql_cluster=$(pg_lsclusters --no-header | grep "$default_port" | cut -d' ' -f2) + default_psql_database=$(pg_lsclusters --no-header | grep "$default_port" | cut -d' ' -f5) + + # Remove non commented lines + sed -i'.bak' -e '/^#/!d' "$config_file" + + # Add new line USER GROUP VERSION CLUSTER DATABASE + echo -e "* * $default_psql_version $default_psql_cluster $default_psql_database" >> "$config_file" +} diff --git a/scripts/install b/scripts/install index 5786445..4552dfb 100755 --- a/scripts/install +++ b/scripts/install @@ -43,6 +43,8 @@ ynh_app_setting_set --key=psql_pwd --value="$db_pwd" ynh_app_setting_set --key=psql_version --value="$(postgresql_version)" ynh_app_setting_set --key=psql_port --value="$db_port" +myynh_set_default_psql_cluster_to_debian_default + #================================================= # MAKE INSTALL #================================================= diff --git a/scripts/restore b/scripts/restore index 1c465b5..0d0059c 100755 --- a/scripts/restore +++ b/scripts/restore @@ -42,6 +42,8 @@ myynh_update_psql_db myynh_create_psql_db myynh_restore_psql_db +myynh_set_default_psql_cluster_to_debian_default + #================================================= # RESTORE SYSTEM CONFIGURATIONS #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 1e0e882..4027873 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -10,7 +10,8 @@ ynh_script_progression "Ensuring downward compatibility..." if ynh_app_upgrading_from_version_before 1.118.2~ynh1 then - if ynh_hide_warnings yunohost service status "$app-microservices" >/dev/null; then + if ynh_hide_warnings yunohost service status "$app-microservices" >/dev/null + then yunohost service remove "$app-microservices" fi ynh_config_remove_systemd "$app-microservices" @@ -21,6 +22,13 @@ then ynh_app_setting_delete --key="checksum__etc_systemd_system_immich-microservices.service" fi +if ynh_app_upgrading_from_version_before 1.122.1~ynh2 +then + ynh_psql_drop_db "$app" + ynh_psql_drop_user "$app" +fi + + #================================================= # STOP SYSTEMD SERVICE #================================================= @@ -64,6 +72,8 @@ ynh_script_progression "Udpating a PostgreSQL database..." myynh_update_psql_db +myynh_set_default_psql_cluster_to_debian_default + #================================================= # MAKE INSTALL #=================================================