#!/bin/sh

DB=plpgsql_test
export DB

FRONTEND="psql -e -q -X"
export FRONTEND

echo "*** destroy old $DB database ***"
dropdb $DB

echo "*** create new $DB database ***"
createdb $DB

echo "*** install PL/pgSQL ***"
createlang plpgsql $DB

echo "*** create tables ***"
$FRONTEND -f tables.sql -d $DB >output/tables.out 2>&1
if cmp -s output/tables.out expected/tables.out ; then
    echo "OK"
else
    echo "FAILED"
fi

echo "*** create triggers ***"
$FRONTEND -f triggers.sql -d $DB >output/triggers.out 2>&1
if cmp -s output/triggers.out expected/triggers.out ; then
    echo "OK"
else
    echo "FAILED"
fi

echo "*** create views and support functions ***"
$FRONTEND -f views.sql -d $DB >output/views.out 2>&1
if cmp -s output/views.out expected/views.out ; then
    echo "OK"
else
    echo "FAILED"
fi

echo "*** running tests ***"
$FRONTEND -f test.sql -d $DB >output/test.out 2>&1
if cmp -s output/test.out expected/test.out ; then
    echo "OK"
else
    echo "FAILED"
fi

