pg-env/pg-env
Yogesh Sharma a6c857bcb4
- Fix errors for zsh commands
- Check for DIR presence before running a command
- add support for pgconfdigs folder
- Update README
2024-01-10 12:20:10 -05:00

232 lines
5.1 KiB
Bash
Executable file

#!/bin/bash
#
##
# Author: Yogesh Sharma
# Copyright 2018-2024 Yogesh Sharma
##
#
# GIT_DIR = Git Folder
# PG_DEV_NAME
# PG_INST_NAME
#
if [ "$0" = "$BASH_SOURCE" ]; then
echo "source $0"
exit 1
fi
if [[ -f ~/.pg-env ]]; then
[ ! -z "$PG_ENV_DEBUG" ] && echo "Loading ~/.pg-env"
source ~/.pg-env
fi
if [[ -f .pg-env ]]; then
[ ! -z "$PG_ENV_DEBUG" ] && echo "Loading .pg-env"
source .pg-env
fi
if [ -z "$GIT_DIR" ]; then
GIT_DIR=${HOME}/gitwork
fi
if [ -z "$PG_DEV_NAME" ]; then
PG_DEV_NAME="postgresql-${PGV}"
fi
if [ -z "$PG_INST_NAME" ]; then
PG_INST_NAME="${PG_DEV_NAME}-INST"
fi
[ ! -z "$PG_ENV_DEBUG" ] && echo "GIT_DIR $GIT_DIR"
[ ! -z "$PG_ENV_DEBUG" ] && echo "PG_DEV_NAME $PG_DEV_NAME"
[ ! -z "$PG_ENV_DEBUG" ] && echo "PG_INST_NAME $PG_INST_NAME"
if [ -z "$SAVED_PATH" ]; then
SAVED_PATH=${PATH}
fi
if [ -z "$SAVED_PS1" ]; then
SAVED_PS1=${PS1}
fi
if [ -z "$SAVED_VIRTUAL_ENV" ]; then
SAVED_VIRTUAL_ENV=${VIRTUAL_ENV}
fi
if [ "$0" = "$BASH_SOURCE" ] && [ ! "$1" = "" ]; then
PGV=${1}
fi
if [ -z "$PGV" ]; then
echo "Missing PGV, export PGV=HEAD or export PGV=15"
else
PG_DEV_SRC=${GIT_DIR}/${PG_DEV_NAME}
PG_DEV_INST=${GIT_DIR}/${PG_INST_NAME}
if [ ! -d ${PG_DEV_SRC} ] || [ ! -d ${PG_DEV_INST_DIR} ]; then
echo "Missing source worktree/clone in ${PG_DEV_SRC} or ${PG_DEV_INST}"
echo "git worktree add --track -b <local branch> ../<local dir> origin/<Branch/TAG>\ngit clone ...\n\n"
echo "git worktree add --track -b REL_${PGV}_STABLE ../${PG_DEV_NAME} origin/REL_${PGV}_STABLE"
echo "mkdir -p ${PG_DEV_INST}"
else
export SAVED_LD_LIBRARY_PATH=${LD_LIBRARY_PATH}
export SAVED_PGDATA=${PGDATA}
export SAVED_PGPORT=${PGPORT}
export SAVED_PGDATABASE=${PGDATABASE}
export PS1="(PG:${PGV}) ${SAVED_PS1}"
export VIRTUAL_ENV="PG:${PGV}"
export PG_DEV_SRC
export PG_DEV_INST
export PG_BIN=${PG_DEV_INST}/bin
export PGDATA=${PG_DEV_INST}/data
export PGDATABASE=postgres
export LD_LIBRARY_PATH=${PG_DEV_INST}/lib
export PATH=${PG_BIN}:${SAVED_PATH}
if [[ "$PGV" =~ '^[0-9]+$' ]]; then
export PGPORT=54${PGV}
else
export PGPORT=5432
fi
fi
distclean() {
pushd ${PG_DEV_SRC}
make distclean
popd
}
compile() {
pushd ${PG_DEV_SRC}
./configure --prefix=${PG_DEV_INST} --enable-debug --with-pgport=${PGPORT} --with-lz4 --with-ssl=openssl --with-python --with-perl --enable-debug --enable-cassert --enable-tap-tests CFLAGS="-ggdb -O0 -g3 -ggdb3 -fno-omit-frame-pointer ${PG_ENV_CFLAGS}"
if [ $? -gt 0 ]; then
echo "Configure failed"
else
make install
if [ $? -gt 0 ]; then
echo "make install failed"
fi
fi
popd
}
compile_contrib() {
pushd ${PG_DEV_SRC}/contrib
make install
if [ $? -gt 0 ]; then
echo "make install failed"
fi
popd
}
startdb(){
if [ -z "$1" ]; then
[ -d "${PGDATA}" ] && pg_ctl start
else
for NodeN in "$@"
do
[ -d "${PGDATA}-${NodeN}" ] && pg_ctl start -D ${PGDATA}-${NodeN}
done
fi
}
stopdb(){
if [ -z "$1" ]; then
[ -d "${PGDATA}" ] && pg_ctl stop
else
for NodeN in "$@"
do
[ -d "${PGDATA}-${NodeN}" ] && pg_ctl stop -D ${PGDATA}-${NodeN}
done
fi
}
restartdb(){
stopdb "$@"
startdb "$@"
}
cleandb(){
if [ -z "$1" ]; then
[ -n "$ZSH_VERSION" ] && setopt localoptions rmstarsilent
rm -rf $PGDATA/*
else
for NodeN in "$@"
do
[ -n "$ZSH_VERSION" ] && setopt localoptions rmstarsilent
rm -rf ${PGDATA}-${NodeN}/*
done
fi
}
setupdb(){
if [ -z "$1" ]; then
initdb --data-checksums --username=sharyogi
echo "include = '${GIT_DIR}/pgconfigs/pg${PGV}.conf'" >> ${PGDATA}-${NodeN}/postgresql.conf
else
for NodeN in "$@"
do
initdb --data-checksums --username=sharyogi -D ${PGDATA}-${NodeN}
echo "include = '${GIT_DIR}/pgconfigs/pg${PGV}-${NodeN}.conf'" >> ${PGDATA}-${NodeN}/postgresql.conf
done
fi
}
resetdb() {
stopdb "$@"
cleandb "$@"
setupdb "$@"
startdb "$@"
}
resetall() {
stopdb "$@"
[ -n "$ZSH_VERSION" ] && setopt localoptions rmstarsilent
rm -rf $PG_DEV_INST/*
distclean
compile
compile_contrib
resetdb "$@"
}
deactivate() {
export PATH=${SAVED_PATH}
export PS1=${SAVED_PS1}
if [ ! -z "$VIRTUAL_ENV" ]; then
export VIRTUAL_ENV=${SAVED_VIRTUAL_ENV}
else
unset VIRTUAL_ENV
fi
if [ ! -z "$SAVED_LD_LIBRARY_PATH" ]; then
export LD_LIBRARY_PATH=${SAVED_LD_LIBRARY_PATH}
else
unset LD_LIBRARY_PATH
fi
if [ ! -z "$SAVED_PGDATA" ]; then
export PGDATA=${SAVED_PGDATA}
else
unset PGDATA
fi
if [ ! -z "$SAVED_PGPORT" ]; then
export PGPORT=${SAVED_PGPORT}
else
unset PGPORT
fi
if [ ! -z "$SAVED_PGDATABASE" ]; then
export PGDATABASE=${SAVED_PGDATABASE}
else
unset PGDATABASE
fi
unset PG_DEV_SRC
unset PG_DEV_INST
unset PG_BIN
unset SAVED_LD_LIBRARY_PATH
unset SAVED_PGDATA
unset SAVED_PGPORT
unset SAVED_PGDATABASE
unset SAVED_PATH
unset SAVED_PS1
unset SAVED_VIRTUAL_ENV
unset -f distclean
unset -f compile
unset -f compile_contrib
unset -f startdb
unset -f stopdb
unset -f restartdb
unset -f cleandb
unset -f setupdb
unset -f resetdb
unset -f resetall
unset -f deactivate
}
fi