From b706f0c65a195082ecef4950cdcf5a48f6b7f71f Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Sun, 12 Jan 2025 15:24:18 +0100 Subject: [PATCH] Automate manual update steps (closes #2384) --- README.md | 4 +--- app.php | 23 +++++++++++++++++++++-- changelog/77_UNRELEASED_xxxx-xx-xx.md | 3 ++- helpers/extensions.php | 16 ++++++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a74b42fa..231a8d95 100644 --- a/README.md +++ b/README.md @@ -63,8 +63,6 @@ See [grocy/grocy-docker](https://github.com/grocy/grocy-docker) or [linuxserver/ - Overwrite everything with the [latest release](https://releases.grocy.info/latest) while keeping the `data` directory - Check `config-dist.php` for new configuration options and add them to your `data/config.php` where appropriate (the default values from `config-dist.php` will be used for not in `data/config.php` defined settings) -- Empty the `data/viewcache` directory -- Visit the main route once to apply database migrations ([see below](https://github.com/grocy/grocy#database-migrations)) If you run Grocy on Linux, there is also `update.sh` (remember to make the script executable (`chmod +x update.sh`) and ensure that you have `unzip` installed) which does exactly this and additionally creates a backup (`.tgz` archive) of the current installation in `data/backups` (backups older than 60 days will be deleted during the update). @@ -137,7 +135,7 @@ See that plugin or `plugins/DemoBarcodeLookupPlugin.php` for a commented example ### Database migrations -Database schema migration is automatically done when visiting the root (`/`) route (click on the logo in the left upper edge). +Database schema migration is done when visiting the root (`/`) route (click on the logo in the left upper edge) as needed and is also triggered automatically if the version has changed (so when an update has been made). _Please note: Database migrations are supposed to work between releases, not between every commit. If you want to run the current `master` branch (which is the development version), you need to handle that (and more) yourself._ diff --git a/app.php b/app.php index bc5b1b39..7a0a511d 100644 --- a/app.php +++ b/app.php @@ -52,9 +52,28 @@ catch (EInvalidConfig $ex) } // Create data/viewcache folder if it doesn't exist -if (!file_exists(GROCY_DATAPATH . '/viewcache')) +$viewcachePath = GROCY_DATAPATH . '/viewcache'; +if (!file_exists($viewcachePath)) { - mkdir(GROCY_DATAPATH . '/viewcache'); + mkdir($viewcachePath); +} + +// Empty data/viewcache when the version changed (so when an update was done) and trigger database migrations +$releaseHash = hash_file('sha256', __DIR__ . '/version.json'); +$releaseHashCacheFile = $viewcachePath . "/$releaseHash.txt"; +if (!file_exists($releaseHashCacheFile)) +{ + EmptyFolder($viewcachePath); + touch($releaseHashCacheFile); + + if (function_exists('opcache_reset')) + { + opcache_reset(); + } + + // Schema migration happens on the root route, so redirect to there + header('Location: ' . (new UrlManager(GROCY_BASE_URL))->ConstructUrl('/')); + exit(); } // Setup base application diff --git a/changelog/77_UNRELEASED_xxxx-xx-xx.md b/changelog/77_UNRELEASED_xxxx-xx-xx.md index 2db0c657..5152aa25 100644 --- a/changelog/77_UNRELEASED_xxxx-xx-xx.md +++ b/changelog/77_UNRELEASED_xxxx-xx-xx.md @@ -62,7 +62,8 @@ - Dialogs can now be closed with the `ESC` key on the keyboard - There is no longer a close button at the bottom (outside of the displayed `iframe`) and instead one at the top right corner of the dialog - Wide dialogs (e.g. all showing a table, like showing stock entries of a product from the stock overview more/context menu per line) now use the full screen width - - Improved handling of the initial field focus +- Improved the handling of the initial field focus on form pages +- The previously manually necessary update steps (e.g. emptying the `data/viewcache` directory) are now fully automated, so no need to do this manually after this and future updates ### API diff --git a/helpers/extensions.php b/helpers/extensions.php index c83f4e4c..5d4c7bc4 100644 --- a/helpers/extensions.php +++ b/helpers/extensions.php @@ -262,3 +262,19 @@ function require_frontend_packages(array $packages) $GROCY_REQUIRED_FRONTEND_PACKAGES = array_unique(array_merge($GROCY_REQUIRED_FRONTEND_PACKAGES, $packages)); } + +function EmptyFolder($folderPath) +{ + foreach(glob("{$folderPath}/*") as $item) + { + if(is_dir($item)) + { + EmptyFolder($item); + rmdir($item); + } + else + { + unlink($item); + } + } +}