]> granicus.if.org Git - postgis/commitdiff
Added uninstall_script to facilitate generation of uninstall_postgis.sql and uninstal...
authorBborie Park <bkpark at ucdavis.edu>
Fri, 23 Dec 2011 16:04:53 +0000 (16:04 +0000)
committerBborie Park <bkpark at ucdavis.edu>
Fri, 23 Dec 2011 16:04:53 +0000 (16:04 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@8521 b70326c6-7e19-0410-871a-916f4a2858ee

utils/uninstall_script [new file with mode: 0755]

diff --git a/utils/uninstall_script b/utils/uninstall_script
new file mode 100755 (executable)
index 0000000..8b44f81
--- /dev/null
@@ -0,0 +1,100 @@
+#!/bin/bash
+
+# this script assumes that it is located in utils
+SOURCE="${BASH_SOURCE[0]}"
+while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
+DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
+
+# use utils as our working directory
+cd $DIR
+
+DB=postgis_uninstall
+
+PGPATH=../postgis/
+RTPATH=../raster/rt_pg/
+
+PGFILE=uninstall_postgis.sql
+RTFILE=uninstall_rtpostgis.sql
+
+if [ "raster" == "$1" ]; then
+       OUTFILE=$RTFILE
+else
+       OUTFILE=$PGFILE
+fi
+INFILE=$OUTFILE.in.$RANDOM
+RAND=$RANDOM
+
+RTN=0
+
+cleanup () {
+       rm -f $INFILE $OUTFILE.$RAND
+}
+
+# get reference uninstall of postgis.sql only
+if [ "$1" == "raster" ]; then
+       ${DIR}$0
+fi
+
+# create database
+createdb $DB
+
+# load postgis.sql
+psql -q -d $DB -f ${PGPATH}postgis.sql
+
+# raster requested, load rtpostgis.sql
+if [ "$1" == "raster" ]; then
+       psql -q -d $DB -f ${RTPATH}rtpostgis.sql
+fi
+
+# dump database loaded with postgis.sql and rtpostgis.sql and strip for only one-line DROP and SET statements
+pg_dump --format=p --clean --schema-only --no-owner --no-acl --no-tablespaces $DB | grep -E "^(DROP|SET).*;$" | grep -v -E "^DROP (SCHEMA|PROCEDURAL) .*;$" > $INFILE
+
+# drop database
+dropdb $DB
+
+# first search_path is parsed
+schema=`grep -m 1 "SET search_path = " < $INFILE | sed -e 's/SET search_path = //' -e 's/\(,\|;\)//g' -e 's/ /\n/'`
+
+# sed arguments
+sedarg="\-e 's/^DROP \(TABLE\|VIEW\|CAST\|OPERATOR CLASS\|OPERATOR\|AGGREGATE\|FUNCTION\|TYPE\)/& IF EXISTS/' "
+for x in $schema; do
+       sedarg="\-e 's/${x}.//g' "${sedarg}
+done
+
+# remove SET statements, remove schema names from DROP statements and add IF EXISTS for DROP statements
+grep -v -E "^SET" < $INFILE | eval "sed ${sedarg}" > $OUTFILE.$RAND
+RTN=$?
+
+if [ "$RTN" != "0" ]; then
+       cleanup
+       exit $RTN
+fi
+
+# if raster, separate raster from postgis items
+if [ "raster" == "$1" ]; then
+       OIFS=$IFS
+       IFS=$'\n'
+
+       echo -n '' > $OUTFILE
+
+       # see if line found in uninstall_postgis.sql
+       for x in `cat $OUTFILE.$RAND`; do
+               y=`echo $x | sed -e 's/\(\[\|\]\)/\\\&/g'`
+               y=`grep "^${y}$" < ${PGPATH}$PGFILE`
+
+               # not postgis item
+               if [ "x$y" == "x" ]; then
+                       echo $x >> ${RTPATH}${OUTFILE}
+               fi
+       done
+
+       IFS=$OIFS
+else
+       mv $OUTFILE.$RAND ${PGPATH}${OUTFILE}
+fi
+
+# cleanup
+cleanup
+
+# return error
+exit $RTN