Project

General

Profile

1 8699 aaronmk
#!/bin/bash -e
2
# links to locally-available resources
3 8893 aaronmk
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh
4 9014 aaronmk
. "$(dirname "${BASH_SOURCE[0]}")"/db.sh
5 8699 aaronmk
6 8705 aaronmk
if self_not_included; then
7
8 9331 aaronmk
root_dir="$(canon_rel_path "$(dirname "${BASH_SOURCE[0]}")"/../..)" || return
9 8699 aaronmk
bin_dir="$root_dir"/bin
10 9331 aaronmk
bin_dir_abs="$(realpath "$bin_dir")" || return
11 8705 aaronmk
12 8925 aaronmk
export PATH="$bin_dir_abs:$PATH"
13 8708 aaronmk
14 8939 aaronmk
#### make
15
16 9074 aaronmk
root_make() { echo_func; make --directory="$root_dir" "$@"; }
17 8939 aaronmk
18 8890 aaronmk
#### connection vars
19
20 9630 aaronmk
: "${remote_ssh_server=vegbiendev.nceas.ucsb.edu}"
21
: "${remote_server=}"
22 8890 aaronmk
: "${local_server=localhost}"
23
: "${local_user=bien}"
24
: "${local_password="$(cat "$root_dir"/config/bien_password)"}"
25 9575 aaronmk
: "${root_user=root}"
26
: "${root_password=$local_password}"
27 8890 aaronmk
28
#### databases
29
30
### MySQL
31
32 8891 aaronmk
: "${postgres_compat=1}"
33
34 9577 aaronmk
mysql_local() { echo_func; use_local_remote; mysql "$@"; }
35
36 9153 aaronmk
mysql_export_local() { echo_func; use_local_remote; mysql_export "$@"; }
37
38 9074 aaronmk
mysqldump_local() { echo_func; use_local_remote; mysqldump_diffable "$@"; }
39 8892 aaronmk
40 9576 aaronmk
mysql_root() { echo_func; use_local_remote; use_root; mysql "$@"; }
41
42 8890 aaronmk
### PostgreSQL
43
44 9074 aaronmk
psql() # usage: [file=...] [dir=...] self
45 8777 aaronmk
{
46 9235 aaronmk
	echo_func; kw_params file dir
47 9050 aaronmk
	if test "$file"; then
48 8777 aaronmk
		set -- --file "$file" "$@"
49 8834 aaronmk
		local dir="${dir:-$(dirname "$file")}"
50 8777 aaronmk
	fi
51 8834 aaronmk
	local dir="${dir:-$top_dir}"
52 9427 aaronmk
	mk_schema_esc
53
	mk_table_esc
54 8777 aaronmk
55 9421 aaronmk
	local psql_cmd="psql_$(if can_log; then echo verbose; else echo script; fi)_vegbien"
56 9478 aaronmk
	local redirs=("${redirs[@]}" '13>&1') # not 11 because it gives a
57
		# "/dev/fd/11: Bad file descriptor" error when 11 is set with exec
58
		# right before the command instead of on the subshell it's executed in
59 8777 aaronmk
	(cat <<EOF
60
\cd $dir
61 9427 aaronmk
\set schema $schema_esc
62
\set table $table_esc
63
\set table_str '''$table_esc'''
64
SET search_path TO $schema_esc, util;
65 8777 aaronmk
EOF
66 9214 aaronmk
	cat)|cmd_log_fd=1 \
67 9477 aaronmk
	env no_search_path=1 "$psql_cmd" --output /dev/fd/13 "$@"
68 9479 aaronmk
		# --output is for query *results*, not echoed statements
69 8777 aaronmk
}
70
71 9074 aaronmk
public_schema_exists() { psql_script_vegbien </dev/null 2>/dev/null; }
72 9046 aaronmk
73 8705 aaronmk
fi