Skip to content

Commit

Permalink
new script to run generic SQL on a remote PG server
Browse files Browse the repository at this point in the history
  • Loading branch information
andylytical committed Feb 19, 2024
1 parent 86de4c1 commit 906f5fa
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cleanup/pg_run_sql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/bash

### VARIABLES
BASE=$( dirname "$0" )


### FUNCTIONS

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

prep() {
rsync "$BASE"/pgpass.cnf ${HOST}:.pgpass
ssh ${HOST} chmod 600 .pgpass
}


test_conn() {
ssh $HOST 'pg_isready -d jsmdb -h localhost -U jsmdb_user'
}


run_sql() {
fn="$1"
cat "$fn" | ssh $HOST 'psql -d jsmdb -h localhost -U jsmdb_user'
}


### MAIN
HOST="$1"
[[ -z "$HOST" ]] && die "Missing hostname"

FN="$2"
[[ -z "$FN" ]] && die "Missing filename"
case "$FN" in
(/*) SQL="$FN" ;;
(*) SQL="${BASE}/${FN}" ;;
esac
[[ -r "$SQL" ]] || die "Cannot read file '$SQL'"

prep

test_conn || die "connection not ready"

run_sql "$SQL"
58 changes: 58 additions & 0 deletions cleanup/user_import_errors.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
select 'missing display name'
as Comment;
SELECT
lower_user_name
, active
, lower_first_name
, lower_last_name
, display_name
, lower_display_name
, email_address
, lower_email_address
FROM cwd_user
WHERE ID IN (53892, 53893);

select 'anyone with missing display name'
as Comment;
SELECT
lower_user_name
, active
, lower_first_name
, lower_last_name
, display_name
, lower_display_name
, email_address
, lower_email_address
FROM cwd_user
WHERE
COALESCE(TRIM(display_name),'') = '';

select 'missing email address'
as Comment;
SELECT
lower_user_name
, active
, lower_first_name
, lower_last_name
, display_name
, lower_display_name
, email_address
, lower_email_address
FROM cwd_user
WHERE
id IN (10671, 11070, 11095,18659, 53892, 53893, 55269, 55273, 55857, 56299, 58329, 62244);

select 'anyone with missing email address'
as Comment;
SELECT
lower_user_name
, active
, lower_first_name
, lower_last_name
, display_name
, lower_display_name
, email_address
, lower_email_address
FROM cwd_user
WHERE
COALESCE(TRIM(email_address),'') = '';

0 comments on commit 906f5fa

Please sign in to comment.