1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
#! /bin/sh -e
#
# Copyright (C) 2001 Gaetano Paolone (bigpaul) <bigpaul@debian.org>
# Roberto Kaitsas <robk@linuxlinks>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
progname=$(basename $0)
if [ "$USER" = "" ]
then
USER=$(whoami)
fi
DATABASE='odontolinux'
function execute()
{
echo -n "** Running: "
echo "$1"
eval "$1"
echo
}
function create_postgres_tables()
{
echo | $DBCOMMAND $DATABASE <<EOF
CREATE SEQUENCE "anagrafica_id_seq" start 1 increment 1 maxvalue 2147483647 minvalue 1 cache 1 ;
CREATE SEQUENCE "pianocura_idpc_seq" start 1 increment 1 maxvalue 2147483647 minvalue 1 cache 1 ;
CREATE SEQUENCE "storico_idst_seq" start 1 increment 1 maxvalue 2147483647 minvalue 1 cache 1 ;
CREATE SEQUENCE "fattura_idfattura_seq" start 1 increment 1 maxvalue 2147483647 minvalue 1 cache 1 ;
CREATE SEQUENCE "pagamenti_idpagamenti_seq" start 1 increment 1 maxvalue 2147483647 minvalue 1 cache 1 ;
CREATE SEQUENCE "fattura_idvocefattura_seq" start 1 increment 1 maxvalue 2147483647 minvalue 1 cache 1 ;
CREATE SEQUENCE "pagamenti_idvocepagamenti_seq" start 1 increment 1 maxvalue 2147483647 minvalue 1 cache 1 ;
CREATE TABLE "anagrafica" (
"id" integer DEFAULT nextval('"anagrafica_id_seq"'::text) NOT NULL,
"dataarrivo" date,
"cognome" character varying(30),
"nome" character varying(30),
"sesso" character(1),
"luogonascita" character varying(50),
"datanascita" date,
"via" character varying(100),
"cap" character varying(5),
"citta" character varying(50),
"professione" character varying(20),
"recapito" character varying(100),
"telefono" character varying(100),
"codicefiscale" character varying(16),
"presentato" character varying(30)
);
CREATE TABLE "pagamenti" (
"id" smallint,
"cognome" character varying(30),
"nome" character varying(30),
"data" date,
"prestazione" character varying(80),
"quantita" smallint,
"prezzounitario" double precision,
"prezzodovuto" double precision,
"prezzodatooggi" double precision,
"rimanedapagare" double precision,
"tipopagamento" character varying(80),
"note" character varying(80),
"idpagamenti" integer DEFAULT nextval('"pagamenti_idpagamenti_seq"'::text) NOT NULL,
"idvocepagamenti" integer DEFAULT nextval('"pagamenti_idvocepagamenti_seq"'::text)
);
CREATE TABLE "storico" (
"id" smallint,
"cognome" character varying(30),
"nome" character varying(30),
"data" date,
"prestazione" character varying(200),
"operatore" character varying(5),
"idst" integer DEFAULT nextval('"storico_idst_seq"'::text) NOT NULL
);
CREATE TABLE "pianocura" (
"id" smallint,
"data" date,
"cognome" character varying(30),
"nome" character varying(30),
"prestazione" character varying(200),
"posizione" character varying(50),
"quantita" smallint,
"prezzounitario" double precision,
"prezzomoltiplicato" double precision,
"idpc" integer DEFAULT nextval('"pianocura_idpc_seq"'::text) NOT NULL,
"annotazioni" text,
"vocepianocura" character varying(30)
);
CREATE TABLE "fattura" (
"id" smallint,
"data" date,
"cognome" character varying(30),
"nome" character varying(30),
"luogonascita" character varying(50),
"datanascita" date,
"via" character varying(100),
"cap" character varying(5),
"citta" character varying(50),
"codicefiscale" character varying(16),
"prestazione" character varying(200),
"quantita" smallint,
"prezzounitario" double precision,
"prezzomoltiplicato" double precision,
"idfattura" integer DEFAULT nextval('"fattura_idfattura_seq"'::text) NOT NULL,
"idvocefattura" integer DEFAULT nextval('"fattura_idvocefattura_seq"'::text) NOT NULL
);
\q
EOF
}
function exit_if_exists()
{
set +e
result=`$DBCOMMAND template1 -l | grep $DATABASE`
set -e
if [ "$result" != "" ]
then
echo ""
echo "*** Error: Database exists ***"
echo ""
echo "The $DATABASE database already exists"
echo "This script is not needed to setup the database."
echo "You might still want to look at its source to write"
echo "your own script for populating the $DATABASE database."
echo ""
echo "If you want to delete the exiting database, use"
echo " dropdb $DATABASE "
echo ""
exit 1
fi
}
function exit_if_root()
{
if [ "$1" == "root" ]
then
echo ""
echo "*** Error: Root usage ***"
echo ""
echo "We do not recommend that you run this script as root"
echo "Please re-run it as a normal user to setup $DATABASE."
echo "If you insist, then please uncomment this test first."
echo ""
exit 1
# else
# echo "Good: We are not running as root"
fi
}
function exit_if_no_postgres_user()
{
set +e
( $DBCOMMAND template1 -c "select * from pg_user;" | grep $1 ) \
>/dev/null 2>&1
if [ $? -eq 1 ]
then
echo ""
echo "*** Error: No postgresql user '$1'"
echo ""
echo "We were unable to start psql as the user '$1' does not exist"
echo "You need to create a Postgresql user '$1' first:"
echo ""
echo " Change to user postgres: $ su - postgres"
echo " Create the user: # createuser $1 -d -A"
echo " Exit from user postgres: # exit"
echo ""
echo "and then run this script again."
echo ""
exit 1
# else
# echo "Good: Postgresql can be accessed by $1"
fi
set -e
}
DBCOMMAND="psql -q"
# test if we are running this as root
exit_if_root $USER
# test if database can be accessed by user
exit_if_no_postgres_user $USER
# test if $DATABASE exists and exit if true
exit_if_exists
echo "Creating $DATABASE database"
execute "createdb $DATABASE"
echo ""
echo "Creating $DATABASE database tables"
create_postgres_tables
|