pg-env/pg-env

201 lines
4 KiB
Bash
Executable file

#!/bin/bash
#
##
# Author: Yogesh Sharma
# Copyright 2018-2024 Yogesh Sharma
##
#
if [ "$0" = "$BASH_SOURCE" ]; then
echo "source $0"
exit 1
fi
if [[ -f ~/.pg-env ]]; then
echo "Loading ~/.pg-env"
source ~/.pg-env
fi
if [[ -f .pg-env ]]; then
echo "Loading .pg-env"
source .pg-env
fi
if [ -z "$PG_REPO_DIR" ]; then
PG_REPO_DIR=${HOME}/gitwork
fi
if [ -z "$PG_REPO_NAME" ]; then
PG_REPO_NAME=postgresql
fi
if [ -z "$DEV_REPO_DIR" ]; then
DEV_REPO_DIR=${PG_REPO_DIR}
fi
if [ -z "$SRC_DIR" ]; then
SRC_DIR="pg-${PGV}-src"
fi
if [ -z "$INST_DIR" ]; then
INST_DIR="pg-${PGV}-inst"
fi
#echo "PG_REPO_DIR $PG_REPO_DIR"
#echo "PG_REPO_NAME $PG_REPO_NAME"
#echo "DEV_REPO_DIR $DEV_REPO_DIR"
#echo "SRC_DIR $SRC_DIR"
#echo "INST_DIR $INST_DIR"
if [ -z "$SAVED_PATH" ]; then
SAVED_PATH=${PATH}
fi
if [ -z "$SAVED_PS1" ]; then
SAVED_PS1=${PS1}
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
if [ -z "$PG_SRC" ]; then
PG_SRC=${PG_REPO_DIR}/${PG_PREFIX}${SRC_DIR}
fi
PG_BASE=${PG_REPO_DIR}/${PG_PREFIX}${INST_DIR}
if [ ! -d ${PG_SRC} ] || [ ! -d ${PG_REPO_DIR}/${PG_PREFIX}${INST_DIR} ]; then
echo "Missing source worktree/clone in ${PG_SRC} or ${PG_BASE}"
echo "git worktree add --track -b <local branch> ../<local dir> origin/<Branch/TAG>\ngit clone ...\n\n"
echo "git worktree add --track -b ${PG_PREFIX}REL_${PGV}_STABLE ../${PG_PREFIX}${SRC_DIR} origin/${PG_PREFIX}REL_${PGV}_STABLE"
echo "mkdir -p ${PG_BASE}"
else
export PS1=$'\n'"ENV: ${PG_SRC}"$'\n'"[ %n@%m %C ] % "
export PG_SRC
export PG_BASE
export PG_BIN=${PG_BASE}/bin
export PGDATA=${PG_BASE}/data
export PGDATABASE=postgres
export LD_LIBRARY_PATH=${PG_BASE}/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_SRC}
make distclean
popd
}
compile() {
pushd ${PG_SRC}
./configure --prefix=${PG_BASE} --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"
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_SRC}/contrib
make install
if [ $? -gt 0 ]; then
echo "make install failed"
fi
popd
}
startdb(){
if [ -z "$1" ]; then
pg_ctl start
else
for NodeN in "$@"
do
pg_ctl start -D ${PGDATA}-${NodeN}
done
fi
}
stopdb(){
if [ -z "$1" ]; then
pg_ctl stop
else
for NodeN in "$@"
do
pg_ctl stop -D ${PGDATA}-${NodeN}
done
fi
}
restartdb(){
stopdb "$@"
startdb "$@"
}
cleandb(){
if [ -z "$1" ]; then
setopt localoptions rmstarsilent
rm -rf $PGDATA/*
else
for NodeN in "$@"
do
setopt localoptions rmstarsilent
rm -rf ${PGDATA}-${NodeN}/*
done
fi
}
setupdb(){
if [ -z "$1" ]; then
initdb --data-checksums --username=sharyogi
else
for NodeN in "$@"
do
initdb --data-checksums --username=sharyogi -D ${PGDATA}-${NodeN}
echo "include = '../../pg${PGV}-${NodeN}.conf'" >> ${PGDATA}-${NodeN}/postgresql.conf
done
fi
}
resetdb() {
stopdb "$@"
cleandb "$@"
setupdb "$@"
startdb "$@"
}
resetall() {
stopdb "$@"
setopt localoptions rmstarsilent
rm -rf $PG_BASE/*
distclean
compile
compile_contrib
resetdb "$@"
}
deactivate() {
export PATH=${SAVED_PATH}
export PS1=${SAVED_PS1}
unset PG_SRC
unset PG_BASE
unset PG_BIN
unset LD_LIBRARY_PATH
unset PGDATA
unset PGDATABASE
unset SAVED_PATH
unset SAVED_PS1
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