view schema/update-db.sh @ 4606:dfe9cde6a20c geoserver_sql_views

Reflect database model changes for SQL views in backend In principle, we could use many datasources with different database schemas, but this would imply changing GeoServer initialization, service filtering, endpoints and eventually more. Since we do not need it, just hard-code the schema name as a constant.
author Tom Gottfried <tom@intevation.de>
date Thu, 05 Sep 2019 12:23:31 +0200
parents 5c0a99b56b2a
children c1fa4cf8a3f4
line wrap: on
line source

#!/bin/bash -e
# This is Free Software under GNU Affero General Public License v >= 3.0
# without warranty, see README.md and license for details.
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# License-Filename: LICENSES/AGPL-3.0.txt
#
# Copyright (C) 2019 by via donau
#   – Österreichische Wasserstraßen-Gesellschaft mbH
# Software engineering by Intevation GmbH
#
# Author(s):
#  * Sascha Wilde <wilde@intevation.de>

ME=`basename "$0"`
BASEDIR=`dirname "$0"`

usage()
{
  cat <<EOF
$ME [OPTION]...

Options:
  -d, --db=NAME    update the database NAME.  Default: "gemma"
  -p, --port=PORT  connect do the postgresql cluster at PORT.
                   Default is the postgresql standard port 5432
      --help       display this help and exit

EOF
}

fatal()
{
  echo >&2 "$1"
  exit 23
}

genpw()
# $1 - length
{
  PW=''
  until [ "$(grep '[^[:alnum:]]' <<<$PW)" -a "$(grep '[[:digit:]]' <<<$PW)" ]
  do
    PW=$(dd count=1 if=/dev/urandom 2>/dev/null \
           | tr -cd '[:alnum:],._!?-' | tail -c "$1")
  done
  echo "$PW"
}

# Defaults:

db=gemma
port=5432

# Parse options:

OPTS=`getopt \
      -l help,db:,port: \
      -o d:p: -n "$ME" -- "$@"`
[ $? -eq 0 ] || { usage ; exit 1 ; }

eval set -- "$OPTS"

while true ; do
  case "$1" in
    --db|-d)
      db="$2"
      shift 2
      ;;
    --port|-p)
      port="$2"
      shift 2
      ;;
    --help)
      { usage ; exit 0 ; }
      ;;
    --)
      shift
      break
      ;;
  esac
done


get_version()
{
  local ver
  if ver=$( psql -qtA -p "$port" -d "$db" \
                 -c 'SELECT get_schema_version()' 2>/dev/null )
  then
    echo ${ver:--1}
  else
    echo '-1'
  fi
}

# Main ------------------------------------------------------------

current_ver=$( get_version )

for d in "$BASEDIR"/updates/* ; do
  new_ver=$( basename $d )
  if [ -d "$d" ] && [ "$new_ver" -gt $current_ver ] ; then
    echo "Running updates for $new_ver ..."

    file_args=""
    for f in "$d"/* ; do
      file_args+=" -f $f"
    done

    psql -1qv ON_ERROR_STOP= -p "$port" -d "$db" $file_args \
      -c "INSERT INTO gemma_schema_version(version) VALUES ($new_ver)"

  fi
done