1 |
9014
|
aaronmk
|
#!/bin/bash -e
|
2 |
|
|
. "$(dirname "${BASH_SOURCE[0]}")"/util.sh
|
3 |
|
|
|
4 |
|
|
if self_not_included; then
|
5 |
|
|
|
6 |
|
|
# using prefixed connection vars
|
7 |
9641
|
aaronmk
|
alias ssh2local='declare prefix=ssh_; import_vars; unset prefix
|
8 |
|
|
declare $(prefix=ssh_ get_prefix_vars); unset $(prefix=ssh_ get_prefix_vars)'
|
9 |
9464
|
aaronmk
|
alias use_local='declare prefix=local_; import_vars; unset prefix'
|
10 |
|
|
alias use_remote='declare prefix=remote_; import_vars; unset prefix'
|
11 |
9167
|
aaronmk
|
alias use_local_remote='{ use_local; use_remote; }'
|
12 |
|
|
# *must* be run inside a function
|
13 |
9574
|
aaronmk
|
alias use_root='declare prefix=root_; import_vars; unset prefix'
|
14 |
9014
|
aaronmk
|
|
15 |
|
|
quote='"'
|
16 |
|
|
|
17 |
9074
|
aaronmk
|
esc_name() { echo "$quote${1//$quote/$quote$quote}$quote"; }
|
18 |
9014
|
aaronmk
|
|
19 |
9423
|
aaronmk
|
mk_esc_name_alias() # usage: mk_esc_name_alias schema_esc
|
20 |
9426
|
aaronmk
|
{ alias mk_"$1"='declare '"$1"'="${'"$1"':-$(esc_name "$'"${1%_esc}"'")}"; '\
|
21 |
|
|
'echo_vars '"$1"; }
|
22 |
9014
|
aaronmk
|
|
23 |
9423
|
aaronmk
|
mk_esc_name_alias schema_esc
|
24 |
|
|
mk_esc_name_alias table_esc
|
25 |
|
|
|
26 |
9470
|
aaronmk
|
fi # load new aliases
|
27 |
|
|
if self_being_included; then
|
28 |
|
|
|
29 |
9623
|
aaronmk
|
skip_table() # usage: create_table || table=... skip_table || return 0
|
30 |
|
|
{ what="table $table" already_exists_msg; }
|
31 |
|
|
|
32 |
9817
|
aaronmk
|
: "${test=$(isset limit; exit2bool)}" # test mode when using limited # rows
|
33 |
|
|
|
34 |
9698
|
aaronmk
|
# usage: (set_large_table; to_file db_cmd...) || return
|
35 |
|
|
alias set_large_table='declare del="${del-$(test "$limit"; exit2bool)}"
|
36 |
|
|
echo_vars del'
|
37 |
|
|
|
38 |
9700
|
aaronmk
|
limit() # usage: "query... $([prefix=$'| |\n'] limit)"
|
39 |
9467
|
aaronmk
|
{
|
40 |
|
|
echo_func; kw_params prefix; local prefix="${prefix-
|
41 |
|
|
}"
|
42 |
|
|
echo -n "${limit:+${prefix}LIMIT $limit}"
|
43 |
|
|
}
|
44 |
|
|
|
45 |
10401
|
aaronmk
|
cols2list() # usage: cols2list col...
|
46 |
|
|
{ echo_func; cmd=esc_name foreach_arg; delim=', ' join "$@"; }
|
47 |
|
|
|
48 |
9468
|
aaronmk
|
mk_select() # usage: {query=... | table=... [cols=...] [filter=...]} mk_select
|
49 |
|
|
{
|
50 |
|
|
echo_func; kw_params query table cols filter; mk_table_esc
|
51 |
10402
|
aaronmk
|
if is_array cols; then cols="$(cols2list "${cols[@]}")"; fi
|
52 |
9468
|
aaronmk
|
echo "$(rtrim "${query:-SELECT ${cols:-*} ${cols:+
|
53 |
9335
|
aaronmk
|
}FROM $table_esc
|
54 |
|
|
${filter:+WHERE $filter
|
55 |
9467
|
aaronmk
|
}}")\
|
56 |
|
|
$(limit)"
|
57 |
9468
|
aaronmk
|
}
|
58 |
9088
|
aaronmk
|
|
59 |
9468
|
aaronmk
|
# export func usage: export_func() { ...; mk_select_var; ... }
|
60 |
|
|
# caller usage: {query=... | table=... [cols=...] [filter=...]} export_func
|
61 |
|
|
# cmd line usage: [limit=...] caller
|
62 |
|
|
alias mk_select_var='declare query="$(mk_select)"'
|
63 |
|
|
|
64 |
9014
|
aaronmk
|
fi # load new aliases
|
65 |
|
|
if self_being_included; then
|
66 |
|
|
|
67 |
9482
|
aaronmk
|
mk_drop() # usage: table=... mk_drop|*sql_ANSI
|
68 |
|
|
{
|
69 |
|
|
log++; echo_func; kw_params table; : "${table?}"; mk_table_esc
|
70 |
|
|
echo "DROP TABLE IF EXISTS $table_esc"
|
71 |
|
|
}
|
72 |
|
|
|
73 |
9465
|
aaronmk
|
mk_truncate() # usage: table=... mk_truncate|*sql_ANSI
|
74 |
9432
|
aaronmk
|
{
|
75 |
|
|
log++; echo_func; kw_params table; : "${table?}"; mk_table_esc
|
76 |
|
|
echo "TRUNCATE $table_esc"
|
77 |
|
|
}
|
78 |
9014
|
aaronmk
|
|
79 |
9432
|
aaronmk
|
|
80 |
9014
|
aaronmk
|
### MySQL
|
81 |
|
|
|
82 |
9094
|
aaronmk
|
alias set_database=\
|
83 |
|
|
'if test "$schema"; then declare database="${database-$schema}"; fi'
|
84 |
|
|
|
85 |
|
|
fi # load new aliases
|
86 |
|
|
if self_being_included; then
|
87 |
|
|
|
88 |
9099
|
aaronmk
|
# usage: mysql*() { ...; mysql_cmd "$@"; } (with alias)
|
89 |
|
|
function mysql_cmd() # usage: mysql*() { ...; mysql_cmd cmd "$@"; }
|
90 |
9271
|
aaronmk
|
# auto-adds connection/login opts when specified
|
91 |
9014
|
aaronmk
|
{
|
92 |
|
|
echo_func
|
93 |
9099
|
aaronmk
|
local cmd="$1"; shift
|
94 |
9014
|
aaronmk
|
local ssh_server="$(localize_url "$ssh_server")"
|
95 |
|
|
local server="$(localize_url "$server")"
|
96 |
9050
|
aaronmk
|
if test "$ssh_server"; then
|
97 |
9014
|
aaronmk
|
local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
|
98 |
|
|
fi
|
99 |
|
|
|
100 |
|
|
local var=ssh_dest; local_inv
|
101 |
9701
|
aaronmk
|
command "time" ${ssh_dest:+ssh "$ssh_dest" }"$cmd" \
|
102 |
9014
|
aaronmk
|
${server:+ --host="$server" }${user:+--user="$user" } --password\
|
103 |
9694
|
aaronmk
|
${password+="$password"} --quick "$@" # --quick: don't buffer entire result
|
104 |
9014
|
aaronmk
|
}
|
105 |
9099
|
aaronmk
|
alias mysql_cmd='mysql_cmd "$FUNCNAME"'
|
106 |
9014
|
aaronmk
|
|
107 |
9099
|
aaronmk
|
fi # load new aliases
|
108 |
|
|
if self_being_included; then
|
109 |
|
|
|
110 |
9706
|
aaronmk
|
function mysql() # usage: [output_data=1] [data_only=1] [log_queries=] mysql ...
|
111 |
9095
|
aaronmk
|
{
|
112 |
9578
|
aaronmk
|
echo_func; kw_params output_data data_only query_verbosity
|
113 |
9557
|
aaronmk
|
if test "$data_only"; then local output_data="${output_data-1}"; fi
|
114 |
9578
|
aaronmk
|
local log_queries="${log_queries-1}"
|
115 |
9095
|
aaronmk
|
set_database
|
116 |
9101
|
aaronmk
|
|
117 |
9557
|
aaronmk
|
set -- ${database:+--database="$database" }--local-infile=1 \
|
118 |
|
|
--${data_only:+skip-}column-names "$@"
|
119 |
9150
|
aaronmk
|
if test "$output_data"; then echo_stdin|mysql_cmd --batch "$@"
|
120 |
9591
|
aaronmk
|
else cmd_log_fd=1 mysql_cmd ${log_queries:+--verbose --verbose --verbose }\
|
121 |
|
|
"$@" # --verbose*3: echo runtimes
|
122 |
9101
|
aaronmk
|
fi
|
123 |
9095
|
aaronmk
|
}
|
124 |
9014
|
aaronmk
|
|
125 |
9074
|
aaronmk
|
mysql_ANSI()
|
126 |
9014
|
aaronmk
|
{
|
127 |
|
|
echo_func
|
128 |
|
|
(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
|
129 |
|
|
}
|
130 |
|
|
|
131 |
9706
|
aaronmk
|
# always use ANSI mode, to support "" identifiers. `` are *still* supported in
|
132 |
|
|
# this mode, so it also works with SHOW CREATE TABLE output and dumpfiles.
|
133 |
|
|
alias mysql='mysql_ANSI'
|
134 |
|
|
|
135 |
9707
|
aaronmk
|
fi # load new aliases
|
136 |
|
|
if self_being_included; then
|
137 |
|
|
|
138 |
|
|
mysql_root() { echo_func; use_root; mysql "$@"; }
|
139 |
|
|
|
140 |
9777
|
aaronmk
|
mysql_truncate() { echo_func; mk_truncate|mysql; }
|
141 |
9466
|
aaronmk
|
|
142 |
9456
|
aaronmk
|
mysql_import() # usage: table=... [cols=...] [append=1] mysql_import <file
|
143 |
|
|
# without append=1, first ensures the table is empty
|
144 |
9430
|
aaronmk
|
{
|
145 |
|
|
echo_func
|
146 |
|
|
mk_table_esc
|
147 |
|
|
local mysql_load_data_format="${mysql_load_data_format-\
|
148 |
|
|
FIELDS TERMINATED BY ','
|
149 |
|
|
OPTIONALLY ENCLOSED BY '\"'
|
150 |
|
|
}"
|
151 |
|
|
|
152 |
9466
|
aaronmk
|
if test ! "$append"; then mysql_truncate; fi
|
153 |
9430
|
aaronmk
|
mysql_load_data_format="${mysql_load_data_format%
|
154 |
|
|
}"
|
155 |
9642
|
aaronmk
|
ssh2local # ssh does not tunnel nonstandard fds
|
156 |
9777
|
aaronmk
|
mysql "$@" 40<&0 <<EOF
|
157 |
9651
|
aaronmk
|
LOAD DATA LOCAL INFILE '/dev/fd/40'
|
158 |
9660
|
aaronmk
|
${append:+IGNORE }INTO TABLE $table_esc
|
159 |
9430
|
aaronmk
|
$mysql_load_data_format
|
160 |
|
|
IGNORE 1 LINES
|
161 |
|
|
EOF
|
162 |
|
|
}
|
163 |
|
|
|
164 |
9152
|
aaronmk
|
mysql_export() # does not support CSV
|
165 |
9348
|
aaronmk
|
# caller usage: {query=... | table=... [cols=...] [filter=...]} mysql_export
|
166 |
|
|
# cmd line usage: [limit=...] caller
|
167 |
9102
|
aaronmk
|
{
|
168 |
|
|
echo_func
|
169 |
9463
|
aaronmk
|
mk_select_var
|
170 |
9152
|
aaronmk
|
|
171 |
9777
|
aaronmk
|
output_data=1 mysql "$@" <<<"$query"
|
172 |
9152
|
aaronmk
|
}
|
173 |
|
|
|
174 |
|
|
mysql_export_outfile() # supports CSV, but requires the FILE privilege
|
175 |
|
|
{
|
176 |
|
|
echo_func
|
177 |
9102
|
aaronmk
|
: "${file:?}"
|
178 |
9463
|
aaronmk
|
mk_select_var
|
179 |
9102
|
aaronmk
|
local mysql_load_data_format="${mysql_load_data_format-\
|
180 |
|
|
FIELDS TERMINATED BY ','
|
181 |
|
|
OPTIONALLY ENCLOSED BY '\"'
|
182 |
|
|
}"
|
183 |
|
|
|
184 |
|
|
local head="${query%%FROM*}" # includes trailing newline
|
185 |
9105
|
aaronmk
|
head="${head%
|
186 |
|
|
}"
|
187 |
9102
|
aaronmk
|
local tail="${query#$head}"
|
188 |
9106
|
aaronmk
|
mysql_load_data_format="${mysql_load_data_format%
|
189 |
|
|
}"
|
190 |
9777
|
aaronmk
|
mysql "$@" <<EOF
|
191 |
9105
|
aaronmk
|
$head
|
192 |
|
|
INTO OUTFILE '$file'
|
193 |
9106
|
aaronmk
|
$mysql_load_data_format
|
194 |
|
|
$tail
|
195 |
9102
|
aaronmk
|
EOF
|
196 |
|
|
}
|
197 |
|
|
|
198 |
10649
|
aaronmk
|
mysqldump() # usage: [schema=1 | data=1] [create_db=1] mysqldump db [table...]
|
199 |
9014
|
aaronmk
|
{
|
200 |
10649
|
aaronmk
|
echo_func
|
201 |
|
|
echo_vars schema data create_db
|
202 |
9231
|
aaronmk
|
|
203 |
10649
|
aaronmk
|
local var=create_db; local_inv
|
204 |
|
|
mysql_cmd ${database:+--databases "$database" ${no_create_db:+--tables }} \
|
205 |
9694
|
aaronmk
|
--lock-tables=false --set-charset \
|
206 |
10446
|
aaronmk
|
${postgres_compat:+${data:+--compatible=postgresql }--add-locks=false }\
|
207 |
9014
|
aaronmk
|
${schema:+--no-data }${data:+--no-create-info }"$@"
|
208 |
|
|
}
|
209 |
|
|
|
210 |
9074
|
aaronmk
|
mysqldump_diffable()
|
211 |
9014
|
aaronmk
|
{
|
212 |
|
|
echo_func
|
213 |
9550
|
aaronmk
|
mysqldump "$@"|{ pipe_delay; echo_run sed 's/^(-- Dump completed).*$/\1/'; }
|
214 |
9014
|
aaronmk
|
}
|
215 |
|
|
|
216 |
9588
|
aaronmk
|
mysql_rm_privileged_statements()
|
217 |
|
|
{
|
218 |
|
|
echo_func
|
219 |
|
|
grep -vE -e '^/\*!40000 ALTER TABLE `.*` (DIS|EN)ABLE KEYS \*/;$' \
|
220 |
|
|
-e '^(UN)?LOCK TABLES( `.*` WRITE)?;$'
|
221 |
|
|
}
|
222 |
9017
|
aaronmk
|
|
223 |
9632
|
aaronmk
|
## permissions
|
224 |
9588
|
aaronmk
|
|
225 |
9632
|
aaronmk
|
mysql_seal_table() # usage: table=... user=... mysql_seal_table
|
226 |
|
|
# prevents further modifications to a table by a user
|
227 |
|
|
{
|
228 |
9634
|
aaronmk
|
echo_func; kw_params table user; : "${table:?}" "${user:?}"; mk_table_esc
|
229 |
9632
|
aaronmk
|
|
230 |
9635
|
aaronmk
|
benign_error=1 mysql_root <<<"REVOKE ALL PRIVILEGES ON $table FROM '$user'@'%'" || true
|
231 |
9636
|
aaronmk
|
benign_error=1 mysql_root <<<"REVOKE GRANT OPTION ON $table FROM '$user'@'%'" || true
|
232 |
9632
|
aaronmk
|
}
|
233 |
|
|
|
234 |
|
|
|
235 |
9014
|
aaronmk
|
### PostgreSQL
|
236 |
|
|
|
237 |
9648
|
aaronmk
|
alias use_pg='declare prefix=pg_; import_vars; unset prefix'
|
238 |
|
|
|
239 |
9649
|
aaronmk
|
fi # load new aliases
|
240 |
|
|
if self_being_included; then
|
241 |
|
|
|
242 |
9646
|
aaronmk
|
psql() # usage: [stdin=copy_pstdin_file] psql <<<"cmds, e.g. \copy from pstdin"
|
243 |
|
|
{
|
244 |
|
|
echo_func; kw_params stdin
|
245 |
|
|
|
246 |
9649
|
aaronmk
|
use_pg
|
247 |
10154
|
aaronmk
|
log- 2
|
248 |
9649
|
aaronmk
|
local PGHOST="$server"; export PGHOST
|
249 |
|
|
local PGUSER="$user"; export PGUSER
|
250 |
|
|
local PGPASSWORD="$password"; export PGPASSWORD
|
251 |
|
|
local PGDATABASE="$database"; export PGDATABASE
|
252 |
|
|
log+ 2
|
253 |
|
|
|
254 |
9646
|
aaronmk
|
if can_log; then set -- --echo-all --echo-hidden "$@"; fi
|
255 |
9651
|
aaronmk
|
local redirs=("${redirs[@]}" '40<&0' "0<${stdin:-&20}" '41>&1')
|
256 |
9646
|
aaronmk
|
(
|
257 |
10295
|
aaronmk
|
# hide stack traces/DETAIL sections of error messages at verbosity <2
|
258 |
|
|
if ! clog++ can_log; then echo '\set VERBOSITY terse'; fi
|
259 |
9646
|
aaronmk
|
if can_log; then cat <<'EOF'
|
260 |
|
|
\timing on
|
261 |
|
|
SET client_min_messages = NOTICE;
|
262 |
|
|
EOF
|
263 |
|
|
fi
|
264 |
|
|
cat
|
265 |
9651
|
aaronmk
|
)|cmd_log_fd=1 command psql --file /dev/fd/40 --output /dev/fd/41 \
|
266 |
9647
|
aaronmk
|
--set ON_ERROR_STOP=1 --quiet "$@"
|
267 |
9646
|
aaronmk
|
# --output is for query *results*, not echoed statements
|
268 |
|
|
}
|
269 |
|
|
|
270 |
9083
|
aaronmk
|
pg_export()
|
271 |
9014
|
aaronmk
|
{
|
272 |
|
|
echo_func
|
273 |
9463
|
aaronmk
|
mk_select_var
|
274 |
9014
|
aaronmk
|
local pg_copy_format="${pg_copy_format-CSV HEADER}"
|
275 |
|
|
|
276 |
9084
|
aaronmk
|
psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
|
277 |
9014
|
aaronmk
|
}
|
278 |
|
|
|
279 |
9074
|
aaronmk
|
pg_header()
|
280 |
9014
|
aaronmk
|
{
|
281 |
|
|
echo_func
|
282 |
|
|
local pg_copy_format="CSV HEADER" limit=0
|
283 |
9083
|
aaronmk
|
pg_export "$@"|echo_stdout
|
284 |
9014
|
aaronmk
|
}
|
285 |
|
|
|
286 |
9074
|
aaronmk
|
pg_export_table_no_header()
|
287 |
9014
|
aaronmk
|
{
|
288 |
|
|
echo_func
|
289 |
|
|
local pg_copy_format="CSV"
|
290 |
9083
|
aaronmk
|
pg_export "$@"
|
291 |
9014
|
aaronmk
|
}
|
292 |
|
|
|
293 |
9074
|
aaronmk
|
pg_export_table_to_dir_no_header()
|
294 |
9014
|
aaronmk
|
{
|
295 |
|
|
echo_func
|
296 |
|
|
local table="$1"; shift; mk_table_esc
|
297 |
9476
|
aaronmk
|
stdout="$exports_dir/$table.no_header.cols=$(pg_header).csv" to_file \
|
298 |
9042
|
aaronmk
|
pg_export_table_no_header "$@"
|
299 |
9014
|
aaronmk
|
}
|
300 |
|
|
|
301 |
|
|
fi
|