From 9c42dbdc350271714bc11ccf0777d4a4f0edc787 Mon Sep 17 00:00:00 2001
From: oversu <95a7002b-481b-4ef3-b578-f9c421550aa4@localhost>
Date: Fri, 4 Sep 2026 12:56:15 +0200
Subject: [PATCH] Protect persistent data during upgrades
---
README.md | 2 +-
manifest.toml | 2 +-
scripts/_common.sh | 39 +++++++++++++++++++++++++++++++++++++++
scripts/restore | 8 ++++++++
scripts/upgrade | 17 +++++++++++++++--
5 files changed, 64 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index 980f5c7..0f9f516 100644
--- a/README.md
+++ b/README.md
@@ -12,7 +12,7 @@ Lightweight animal shelter management
[](https://globinours.fr)
[](https://demo.globinours.fr)
-[?style=for-the-badge)](https://ci-apps.yunohost.org/ci/apps/globinours/)
+[?style=for-the-badge)](https://ci-apps.yunohost.org/ci/apps/globinours/)

diff --git a/manifest.toml b/manifest.toml
index d979607..25c80a8 100644
--- a/manifest.toml
+++ b/manifest.toml
@@ -5,7 +5,7 @@ id = "globinours"
name = "Globinours"
description.en = "Lightweight animal shelter management"
description.fr = "Gestion légère et complète pour refuges animaliers"
-version = "1.0.0-rc.3~ynh14"
+version = "1.0.0-rc.3~ynh15"
maintainers = ["OverSu"]
[upstream]
diff --git a/scripts/_common.sh b/scripts/_common.sh
index b485aca..676a018 100755
--- a/scripts/_common.sh
+++ b/scripts/_common.sh
@@ -15,8 +15,47 @@ install_yunohost_logo() {
yunohost user permission update "$app.main" --logo "$logo"
}
+migrate_legacy_data() {
+ local legacy_data="$install_dir/data"
+ local legacy_storage="$install_dir/storage"
+ local legacy_media="$install_dir/public/media"
+
+ if [[ -d "$legacy_data" && ! -L "$legacy_data" ]]; then
+ ynh_print_info "Migrating the legacy data directory to $data_dir..."
+ cp -a "$legacy_data/." "$data_dir/"
+
+ if [[ -f "$legacy_data/refuge.sqlite" ]]; then
+ local migrated_database="$data_dir/refuge.sqlite.migrating"
+ sqlite3 "$legacy_data/refuge.sqlite" ".backup '$migrated_database'"
+
+ local integrity_check
+ integrity_check="$(sqlite3 "$migrated_database" "PRAGMA integrity_check;")"
+ if [[ "$integrity_check" != "ok" ]]; then
+ rm -f "$migrated_database"
+ ynh_die --message="The migrated Globinours database failed its integrity check. The legacy data was left untouched."
+ fi
+
+ mv -f "$migrated_database" "$data_dir/refuge.sqlite"
+ rm -f "$data_dir/refuge.sqlite-wal" "$data_dir/refuge.sqlite-shm"
+ fi
+ fi
+
+ if [[ -d "$legacy_storage" && ! -L "$legacy_storage" ]]; then
+ ynh_print_info "Migrating the legacy storage directory to $data_dir/storage..."
+ cp -a "$legacy_storage/." "$data_dir/storage/"
+ fi
+
+ if [[ -d "$legacy_media" && ! -L "$legacy_media" ]]; then
+ ynh_print_info "Migrating the legacy public media directory to $data_dir/media..."
+ cp -a "$legacy_media/." "$data_dir/media/"
+ fi
+}
+
prepare_persistent_paths() {
for path_to_replace in "$install_dir/data" "$install_dir/storage" "$install_dir/public/media"; do
+ if [[ -d "$path_to_replace" && ! -L "$path_to_replace" ]]; then
+ ynh_die --message="Refusing to replace the physical data directory $path_to_replace before it is migrated."
+ fi
if [[ -e "$path_to_replace" || -L "$path_to_replace" ]]; then
ynh_safe_rm "$path_to_replace"
fi
diff --git a/scripts/restore b/scripts/restore
index 3298419..cbf410f 100755
--- a/scripts/restore
+++ b/scripts/restore
@@ -25,6 +25,14 @@ ynh_script_progression "Restoring system configurations related to $app..."
ynh_restore "/etc/nginx/conf.d/$domain.d/$app.conf"
ynh_restore "/etc/php/$php_version/fpm/pool.d/$app.conf"
+migrate_legacy_data
+
+for migrated_path in "$install_dir/data" "$install_dir/storage" "$install_dir/public/media"; do
+ if [[ -d "$migrated_path" && ! -L "$migrated_path" ]]; then
+ ynh_safe_rm "$migrated_path"
+ fi
+done
+
prepare_persistent_paths
install_yunohost_logo
ynh_exec_as_app php "$install_dir/scripts/migrate.php"
diff --git a/scripts/upgrade b/scripts/upgrade
index 582e61d..d0a1d61 100755
--- a/scripts/upgrade
+++ b/scripts/upgrade
@@ -11,13 +11,19 @@ source /usr/share/yunohost/helpers
# CREATE AN APPLICATION BACKUP
#=================================================
-if [[ -f "$install_dir/scripts/backup.php" && -f "$data_dir/refuge.sqlite" ]] && \
+if [[ -f "$install_dir/scripts/backup.php" && -f "$install_dir/data/refuge.sqlite" ]] && \
php -l "$install_dir/app/Services/BackupService.php" >/dev/null 2>&1; then
ynh_exec_as_app php "$install_dir/scripts/backup.php" pre-upgrade-yunohost
-elif [[ -f "$data_dir/refuge.sqlite" ]]; then
+elif [[ -f "$install_dir/data/refuge.sqlite" ]]; then
ynh_print_warn --message="La sauvegarde applicative préalable est ignorée car l'ancienne version n'est pas compatible avec PHP $php_version. La sauvegarde de sécurité YunoHost reste active."
fi
+#=================================================
+# MIGRATE LEGACY DATA
+#=================================================
+
+migrate_legacy_data
+
#=================================================
# ENABLE MAINTENANCE MODE
#=================================================
@@ -33,6 +39,13 @@ ynh_script_progression "Upgrading source files..."
ynh_setup_source --dest_dir="$install_dir" --full_replace --keep="data storage public/media"
+# Physical legacy directories were kept until their migration was complete.
+for migrated_path in "$install_dir/data" "$install_dir/storage" "$install_dir/public/media"; do
+ if [[ -d "$migrated_path" && ! -L "$migrated_path" ]]; then
+ ynh_safe_rm "$migrated_path"
+ fi
+done
+
#=================================================
# UPDATE APPLICATION AND SYSTEM CONFIGURATION
#=================================================