diff --git a/templates/nextcloud-config.yaml b/templates/nextcloud-config.yaml
index ce4cc124f4e0866d48385eb65c604adcbecedb78..518a3ca2243dc46159bc5fe1b637e177e94b3edd 100644
--- a/templates/nextcloud-config.yaml
+++ b/templates/nextcloud-config.yaml
@@ -28,12 +28,36 @@ data:
     }
   setup-apps.sh: |
     #!/bin/bash
+    #
+    # This is a post-install,post-upgrade helm hook, which
+    #
+    #   * Persists and loads the onlyoffice-config.json config file
+    #   * Run upgrade routines after installation of a new release.
+    #   * Updates all applications
+    #   * Installs all apps declared in the `apps` helm values array
+    #   * Configures single-sign-on
+    #   * Update database indices, columns, keys, etc needed after NC upgrade
+
     set -ev
+
     # Debug: place the json file in a persistent location for reuse
     cp /var/local/onlyoffice-config.json /var/www/html/
+
     occ="/var/www/html/occ"
+
+    # Run upgrade routines after installation of a new release.
+    php /var/www/nextcloud/occ upgrade --no-interaction
+
     # Enable app store so we can run `install` and `enable` commands
     php $occ config:system:set appstoreenabled --type boolean --value true
+
+    # Update all apps to their latest version, so they are compatible
+    # with the new NC version.
+    # Unfortunatly the occ app:install cmd doesn't allow to pin apps to
+    # a certain version.
+    php /var/www/nextcloud/occ app:update --all --no-interaction
+
+    # Install all apps declared in the `apps` helm values array
     {{- range .Values.apps }}
     # -- Begin {{ .name }}
     # Only install {{ .name }} if it's not installed already
@@ -51,8 +75,16 @@ data:
     php $occ config:app:set sociallogin custom_providers --value='{"custom_oidc": [{{ .Values.sociallogin.custom_oidc | toJson }}]}'
     php $occ config:app:set sociallogin auto_create_groups --value='{{ .Values.sociallogin.auto_create_groups }}'
     php $occ config:app:set sociallogin update_profile_on_login --value='{{ .Values.sociallogin.update_profile_on_login }}'
+
     # Disable app store again
     php $occ config:system:set appstoreenabled --type boolean --value false
+
+    # Update database indices, columns, keys, etc needed after NC upgrade
+    php /var/www/nextcloud/occ db:add-missing-indices --no-interaction
+    php /var/www/nextcloud/occ db:add-missing-columns --no-interaction
+    php /var/www/nextcloud/occ db:add-missing-primary-keys --no-interaction
+    php /var/www/nextcloud/occ db:convert-filecache-bigint --no-interaction
+
   #
   # All values in config.json are applied by the nextcloud occ command
   #   config:import.