Code

pgtest.sh: Added a small helper script to setup/run a PG test setup.
authorSebastian Harl <sh@tokkee.org>
Sat, 28 Apr 2012 15:57:02 +0000 (17:57 +0200)
committerSebastian Harl <sh@tokkee.org>
Sat, 28 Apr 2012 15:57:02 +0000 (17:57 +0200)
pgtest.sh [new file with mode: 0755]

diff --git a/pgtest.sh b/pgtest.sh
new file mode 100755 (executable)
index 0000000..b88bf6c
--- /dev/null
+++ b/pgtest.sh
@@ -0,0 +1,88 @@
+#! /bin/bash
+#
+# PostRR test setup helper script
+#
+# Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+PG_CONFIG=`which pg_config`
+if test -z "$PG_CONFIG"; then
+       echo "pg_config not found!" >&2
+       exit 1
+fi
+
+BIN_DIR=`pg_config --bindir`
+if test -z "$TARGET"; then
+       TARGET=`pwd`/target
+fi
+
+PWD_esc="$( echo "$PWD" | sed -e 's/\//\\\//g' )"
+TARGET_esc="$( echo "$TARGET" | sed -e 's/\//\\\//g' )"
+
+set -e
+
+case "$1" in
+       setup)
+               mkdir -p $TARGET/var/lib/postgresql/main
+               mkdir -p $TARGET/var/run/postgresql
+               mkdir -p $TARGET/var/log/postgresql/main
+               $BIN_DIR/initdb -D $TARGET/var/lib/postgresql/main
+               sed -r -i -e 's/^#port = 5432/port = 5435/' \
+                       $TARGET/var/lib/postgresql/main/postgresql.conf
+               sed -r -i -e "s/^#dynamic_library_path = '\\\$libdir'/dynamic_library_path = '\$libdir:$PWD_esc\/src'/" $TARGET/var/lib/postgresql/main/postgresql.conf
+               sed -r -i -e "s/^#unix_socket_directory = ''/unix_socket_directory = '$TARGET_esc\/var\/run\/postgresql'/" $TARGET/var/lib/postgresql/main/postgresql.conf
+               $0 start -B
+               $BIN_DIR/createdb -e -h $TARGET/var/run/postgresql/ -p 5435 tokkee
+               $0 stop
+               ;;
+       client)
+               libedit=$( ldd $BIN_DIR/psql 2> /dev/null \
+                       | grep -Eo '=> [^ ]+\/libedit\.so\..? ' | cut -d' ' -f2 )
+               if test -n "$libedit"; then
+                       libdir=${libedit/libedit.*/}
+                       libreadline=$( ls $libdir/libreadline.so* 2> /dev/null | head -n1 )
+                       if test -n "$libreadline"; then
+                               export LD_PRELOAD="$LD_PRELOAD:$libreadline"
+                       fi
+               fi
+               $BIN_DIR/psql -h $TARGET/var/run/postgresql/ -p 5435
+               ;;
+       stop)
+               $BIN_DIR/pg_ctl -D $TARGET/var/lib/postgresql/main stop
+               ;;
+       start)
+               if test "$2" = "-B"; then
+                       $BIN_DIR/pg_ctl -D $TARGET/var/lib/postgresql/main -l logfile -w start
+               else
+                       $BIN_DIR/postgres -D $TARGET/var/lib/postgresql/main
+               fi
+               ;;
+       *)
+               echo "Usage: $0 setup|client|stop|start" >&2
+               exit 1
+               ;;
+esac
+
+# vim: set tw=0 :
+