"Fossies" - the Fresh Open Source Software Archive

Member "absence-v2.1/db/create_db.sh" (20 Oct 2013, 3034 Bytes) of package /linux/www/web-absence-2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/local/bin/bash
    2 
    3 #======================================================================
    4 #    This file is part of Absence.
    5 #
    6 #    Absence is free software: you can redistribute it and/or modify
    7 #    it under the terms of the GNU General Public License as published by
    8 #    the Free Software Foundation, either version 3 of the License, or
    9 #    (at your option) any later version.
   10 #
   11 #    Absence is distributed in the hope that it will be useful,
   12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 #    GNU General Public License for more details.
   15 #
   16 #    You should have received a copy of the GNU General Public License
   17 #    along with Absence.  If not, see <http://www.gnu.org/licenses/>.
   18 #======================================================================
   19 
   20 #-----------------------------------------------------------------------
   21 # $id$
   22 # copyright Robert Urban
   23 #-----------------------------------------------------------------------
   24 
   25 run_cmd() {
   26     typeset cmd=$1
   27     [[ $VERBOSE -eq 1 ]] && echo "cmd: [$cmd]"
   28     eval $cmd
   29     if [ $? -ne 0 ]; then
   30         echo "COMMAND FAILED: [$cmd]  exiting."
   31         exit 1
   32     fi
   33 }
   34 
   35 HERE=$(dirname $0)
   36 echo "HERE=[$HERE]"
   37 
   38 VERBOSE=1
   39 DB_ADMIN_USER="postgres"
   40 DB_NAME="absence-ldw"
   41 DB_DESC="absence data"
   42 DB_USER="absence"
   43 DB_PASS="absence"
   44 CREATE_TABLES_SCRIPT="$HERE/create_tables.sql"
   45 CREATE_TABLES_LOG="/tmp/create_tables.log"
   46 PSQL_CMD="psql -U $DB_ADMIN_USER"
   47 
   48 PATH=/bin:/usr/bin:/usr/local/bin
   49 
   50 #---------------------------------------------------------------------
   51 # create DB Owner
   52 #---------------------------------------------------------------------
   53 if $PSQL_CMD -c "SELECT rolname FROM pg_roles where rolname = '$DB_USER';" template1 | grep '1 row'; then
   54     echo "user $DB_USER exists"
   55 else
   56     #$PSQL_CMD -c "create user $DB_USER with password '$DB_PASS';"
   57     $PSQL_CMD -c "CREATE ROLE $DB_USER NOSUPERUSER CREATEDB NOCREATEROLE INHERIT LOGIN;"
   58     $PSQL_CMD -c "ALTER USER $DB_USER ENCRYPTED PASSWORD '$DB_PASS';" template1
   59 fi
   60 
   61 #cmd="create user $DB_USER with password '$DB_PASS' createdb;"
   62 #cmd="create user $DB_USER createdb;"
   63 #psql -c "$cmd" template1
   64 
   65 #psql -c "ALTER USER $DB_USER WITH PASSWORD '$DB_PASS';" template1
   66 
   67 #---------------------------------------------------------------------
   68 # create DB
   69 #---------------------------------------------------------------------
   70 echo "dropping database..."
   71 out=$(dropdb -U $DB_ADMIN_USER $DB_NAME 2>&1)
   72 if [ $? -ne 0 ]; then
   73     if [[ "$out" != *not\ exist* ]]; then
   74         echo "dropdb command failed. exiting."
   75         exit 1
   76     fi
   77 fi
   78 run_cmd "createdb -U $DB_USER -E UTF8 -O $DB_USER $DB_NAME '$DB_DESC'"
   79 
   80 #---------------------------------------------------------------------
   81 # create tables
   82 #---------------------------------------------------------------------
   83 psql -U $DB_USER -d "$DB_NAME" -f $CREATE_TABLES_SCRIPT -o $CREATE_TABLES_LOG
   84 echo "psql return-code = $?"
   85 #cat $CREATE_TABLES_SCRIPT | psql -d "$DB_NAME" -f - -o $CREATE_TABLES_LOG
   86 
   87 echo "finished creating database."