matabas/build/devShellHook.sh
2023-08-09 22:41:11 +02:00

41 lines
1.0 KiB
Bash

set -e
export PGPORT=2137
export PGDATABASE=matabas
export PGUSER=matabasare
log() {
printf "\e[1m\e[38;5;87m==>\e[0m\e[1m %s%s\e[0m\n" "$1" "${2:-.}"
}
exitHook() {
log "Stoppar brevekorren"
pg_ctl stop --pgdata=./matabas-db
}
log "Letar efter existerande brevekorresmapp"
[ -d ./matabas-db ] || {
log "Finns ej, skapar brevekorresmapp (./matabas-db)"
pg_ctl initdb --pgdata=./matabas-db -o "--username=$PGUSER"
}
[ -f matabas-db/matabas.log ] && {
log "Flyttar journalen från senaste sittningen till journalarkivet (./matabas-db/matabas.log.old)"
cat matabas-db/matabas.log >> matabas-db/matabas.log.old
rm -f matabas-db/matabas.log
}
log "Startar brevekorren"
pg_ctl start --pgdata=./matabas-db --log=./matabas-db/matabas.log --options="-p $PGPORT -k /tmp"
trap exitHook EXIT
log "Skapar matabas"
createdb -p "$PGPORT" matabas 2>>./matabas-db/matabas.log || log "Matabasen finns, skriver ej över"
log "Ställer in schema.sql"
psql -p "$PGPORT" "$PBDATABASE" < schema.sql
log "Redo" "!"
set +e