diff --git a/package.json b/package.json index 22bee194a9c..8e93774bad5 100644 --- a/package.json +++ b/package.json @@ -107,7 +107,8 @@ "enable-autologin": "sed -i -e 's/enableDevAutoLogin = false/enableDevAutoLogin = true/g' ./conf/application.conf", "disable-autologin": "sed -i -e 's/enableDevAutoLogin = true/enableDevAutoLogin = false/g' ./conf/application.conf", "coverage": "nyc report --reporter=text-lcov | coveralls", - "postcheckout": "echo 'Deleting auto-generated flow files...' && rm -f app/assets/javascripts/test/snapshots/flow-check/*.js" + "postcheckout": "echo 'Deleting auto-generated flow files...' && rm -f app/assets/javascripts/test/snapshots/flow-check/*.js", + "apply-evolutions": "tools/postgres/apply_evolutions.sh" }, "lint-staged": { "*.js": [ diff --git a/tools/postgres/apply_evolutions.sh b/tools/postgres/apply_evolutions.sh new file mode 100755 index 00000000000..b26a3daca4c --- /dev/null +++ b/tools/postgres/apply_evolutions.sh @@ -0,0 +1,35 @@ +#!/bin/bash +set -Eeuo pipefail + +scriptdir="$(dirname "$0")" + +# find schema version +schema_version=`psql -A -U postgres -h localhost --dbname="webknossos" -t -c "SELECT * FROM webknossos.releaseinformation"` +echo "Schema version: ${schema_version}" + +# assert that the found schema version is a number +re='^[0-9]+$' +if ! [[ $schema_version =~ $re ]] ; then + echo "Error: Schema version is not a number" >&2; exit 1 +fi + +# get list of evolutions to apply +files="" +for entry in "${scriptdir}/../../conf/evolutions"/*.sql +do + tmp_number=`grep -oP "[0-9]{3}" <<< $entry` + evolution_number=$((10#$tmp_number)) # force the number to be decimal + if (( evolution_number > schema_version )); then + files="$files -f $entry" + fi; +done + +# apply evolutions +if [ -n "$files" ]; then + echo "Applying following evolutions: $files" + `PGPASSWORD=postgres psql -U postgres -h localhost --dbname="webknossos" -v ON_ERROR_STOP=ON -q $files` + echo "Successfully applied the evolutions after $schema_version" +else + echo "There are no evolutions that can be applied." +fi; +