view scripts/.functions.sh @ 394:326978b4b0be

correct db name backup-workflow.sh script
author Matt Calmes <calmes@ssdt-ohio.org>
date Wed, 27 Oct 2021 15:21:57 -0400
parents d6d8468441b1
children
line wrap: on
line source
#!/bin/bash
getScriptDir() {
     SOURCE="${BASH_SOURCE[0]}"
     while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
     echo $( cd $( dirname $SOURCE) && pwd )
}

if [ "$SSDT_HOME" == "" ]
then
   export SSDT_SCRIPTS=$(getScriptDir)
   export SSDT_HOME=$(dirname $SSDT_SCRIPTS)
fi

if [ "$(which docker-compose)" ==  "" ]
then
   PATH="${PATH}:/usr/local/bin"
fi

export SSDT_DOCKER_VERSION=$(docker version --format '{{.Server.Version}}')

# deletes psql data from specified service
resetDbVolume() {
#   Works for named and unnamed volumes and custom $PGDATA.
#   Container must be stopped
   service=${1?Must supply db service to reset}
   container=$(composeGetContainer $service)
   if [ "$container" != "" ]
   then
      echo "deleting db data from $container"
      docker-compose run --no-deps --rm $service bash -c "rm -rf \${PGDATA?Missing PGDATA env}"
   fi
}

# finds docker container for specified compose service 
composeGetContainer() {
  service=${1?"Must supply service from current project"}
  container=$( docker-compose ps -q $service | xargs docker inspect -f '{{.Name}}' | cut -d "/" -f 2 )
  echo $container
}

executeSQL() {
   service=${1?Must supply db service to execute against}
   container=$(composeGetContainer $service)

   echo "$2" | docker exec -i $container  sh -c "gosu postgres psql \$DB_NAME"
}

composeGetProject() {  
  project=$(docker-compose ps -q | tail -n1 |  xargs docker inspect -f "{{ index .Config.Labels \"com.docker.compose.project\"}}" )
  echo $project
}