Skip to content

Commit

Permalink
update pg_run_sql
Browse files Browse the repository at this point in the history
  • Loading branch information
andylytical committed Mar 2, 2024
1 parent 9ab90aa commit 3766a3d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 51 deletions.
8 changes: 4 additions & 4 deletions bin/my_run_sql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
### VARIABLES
BASE=$( dirname "$0" )
HOST=''
MY_CNF=''
CONF=''
FILES=()


Expand Down Expand Up @@ -32,9 +32,9 @@ get_files() {


prep() {
MY_CNF=~/.my.cnf.${HOST}
[[ -f "${MY_CNF}" ]] || die "File not found '${MY_CNF}'"
rsync "${MY_CNF}" ${HOST}:.my.cnf
CONF=~/.my.cnf.${HOST}
[[ -f "${CONF}" ]] || die "config file not found '${CONF}'"
rsync "${CONF}" ${HOST}:.my.cnf
}


Expand Down
83 changes: 83 additions & 0 deletions bin/pg_run_sql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/bash

### VARIABLES
BASE=$( dirname "$0" )
HOST=''
CONF=''
FILES=()
PGHOST=''
PGUSER=''
PGPORT=''
PGDB=''


### FUNCTIONS

die() {
echo "ERROR: $*" 1>&2
kill 0
exit 99
}


get_host() {
HOST="$1"
[[ -z "$HOST" ]] && die 'Missing host'
}


get_files() {
local _fn
for _fn in "${@}"; do
[[ -f "${_fn}" ]] || die "File not found '${_fn}'"
FILES+=( "${_fn}" )
done
[[ ${#FILES[@]} -lt 1 ]] && die "Missing sql files for host: '$HOST'"
}


prep() {
CONF=~/.pgpass.cnf.${HOST}
[[ -f "${CONF}" ]] || die "config file not found '${CONF}'"
rsync "${CONF}" ${HOST}:.pgpass
ssh ${HOST} chmod 600 .pgpass
}


get_connection_parameters() {
PGHOST=$( head -1 "${CONF}" | cut -d: -f1 )
PGPORT=$( head -1 "${CONF}" | cut -d: -f2 )
PGDB=$( head -1 "${CONF}" | cut -d: -f3 )
PGUSER=$( head -1 "${CONF}" | cut -d: -f4 )
}


test_connection() {
ssh $HOST "pg_isready -q -h ${PGHOST} -p ${PGPORT} -d ${PGDB} -U ${PGUSER}"
}


run_sql() {
fn="$1"
# cat "$fn" | ssh $HOST 'psql -d jsmdb -h localhost -U jsmdb_user'
cat "$fn" | ssh $HOST "psql -h ${PGHOST} -p ${PGPORT} -d ${PGDB} -U ${PGUSER}"
}


### MAIN
get_host "$1"
shift
get_files "${@}"

prep

get_connection_parameters

test_connection || die "connection not ready"

for fn in "${FILES[@]}"; do
set -x
run_sql "$fn"
set +x
echo
done
47 changes: 0 additions & 47 deletions cleanup/pg_run_sql.sh

This file was deleted.

3 changes: 3 additions & 0 deletions wiki_cleanup/ls_views.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* https://stackoverflow.com/questions/51246974/error-is-not-base-table-on-repairing-corrupted-table */

SHOW FULL TABLES in ncsa_wiki WHERE TABLE_TYPE LIKE 'VIEW';
1 change: 1 addition & 0 deletions wiki_cleanup/pg_run_sql.sh

0 comments on commit 3766a3d

Please sign in to comment.