Files
shz-backend/.codex/skills/query-shz-highgo/scripts/write.sh
2026-07-07 19:48:06 +08:00

39 lines
1.2 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
skill_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
password_file="${HIGHGO_PASSWORD_FILE:-$skill_dir/.password}"
ssh_host="root@124.243.245.42"
psql_path="/opt/HighGo4.5.7-see/bin/psql"
db_host="39.98.44.136"
db_port="6023"
db_name="highgo"
db_user="sysdba"
if [[ ! -r "$password_file" ]]; then
echo "HighGo password file is missing or unreadable: $password_file" >&2
exit 2
fi
if [[ $# -eq 0 ]]; then
echo "Usage: $0 'SQL_STATEMENT'" >&2
exit 2
fi
sql="$*"
sql_without_final_semicolon="${sql%;}"
if [[ "$sql_without_final_semicolon" == *';'* ]]; then
echo "Rejected: multiple SQL statements are not allowed. Run them one at a time." >&2
exit 3
fi
password="$(<"$password_file")"
sql_base64="$(printf '%s' "$sql" | base64 | tr -d '\n')"
{
printf '%s\n' "$password"
printf '%s\n' "$sql_base64"
} | ssh -o BatchMode=yes -o ConnectTimeout=10 "$ssh_host" \
"read -r PGPASSWORD; read -r SQL_BASE64; export PGPASSWORD; export PGOPTIONS='-c search_path=shz,public -c statement_timeout=30000'; SQL=\$(printf '%s' \"\$SQL_BASE64\" | base64 -d); exec '$psql_path' -X --no-psqlrc -h '$db_host' -p '$db_port' -U '$db_user' -d '$db_name' -v ON_ERROR_STOP=1 -P pager=off -c \"\$SQL\""