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 |
10827
|
aaronmk
|
mk_select() # usage: {query=... | table=... [cols=...] [filter=...] \
|
49 |
|
|
# [order_by=...]} mk_select
|
50 |
9468
|
aaronmk
|
{
|
51 |
|
|
echo_func; kw_params query table cols filter; mk_table_esc
|
52 |
10827
|
aaronmk
|
if is_array cols ; then cols="$( cols2list "${cols[@]}" )"; fi
|
53 |
|
|
if is_array order_by; then order_by="$(cols2list "${order_by[@]}")"; fi
|
54 |
9468
|
aaronmk
|
echo "$(rtrim "${query:-SELECT ${cols:-*} ${cols:+
|
55 |
9335
|
aaronmk
|
}FROM $table_esc
|
56 |
|
|
${filter:+WHERE $filter
|
57 |
10827
|
aaronmk
|
}${order_by:+ORDER BY $order_by
|
58 |
9467
|
aaronmk
|
}}")\
|
59 |
|
|
$(limit)"
|
60 |
9468
|
aaronmk
|
}
|
61 |
9088
|
aaronmk
|
|
62 |
9468
|
aaronmk
|
# export func usage: export_func() { ...; mk_select_var; ... }
|
63 |
|
|
# caller usage: {query=... | table=... [cols=...] [filter=...]} export_func
|
64 |
|
|
# cmd line usage: [limit=...] caller
|
65 |
|
|
alias mk_select_var='declare query="$(mk_select)"'
|
66 |
|
|
|
67 |
9014
|
aaronmk
|
fi # load new aliases
|
68 |
|
|
if self_being_included; then
|
69 |
|
|
|
70 |
9482
|
aaronmk
|
mk_drop() # usage: table=... mk_drop|*sql_ANSI
|
71 |
|
|
{
|
72 |
|
|
log++; echo_func; kw_params table; : "${table?}"; mk_table_esc
|
73 |
|
|
echo "DROP TABLE IF EXISTS $table_esc"
|
74 |
|
|
}
|
75 |
|
|
|
76 |
9465
|
aaronmk
|
mk_truncate() # usage: table=... mk_truncate|*sql_ANSI
|
77 |
9432
|
aaronmk
|
{
|
78 |
|
|
log++; echo_func; kw_params table; : "${table?}"; mk_table_esc
|
79 |
|
|
echo "TRUNCATE $table_esc"
|
80 |
|
|
}
|
81 |
9014
|
aaronmk
|
|
82 |
9432
|
aaronmk
|
|
83 |
9014
|
aaronmk
|
### MySQL
|
84 |
|
|
|
85 |
9094
|
aaronmk
|
alias set_database=\
|
86 |
|
|
'if test "$schema"; then declare database="${database-$schema}"; fi'
|
87 |
|
|
|
88 |
|
|
fi # load new aliases
|
89 |
|
|
if self_being_included; then
|
90 |
|
|
|
91 |
9099
|
aaronmk
|
# usage: mysql*() { ...; mysql_cmd "$@"; } (with alias)
|
92 |
|
|
function mysql_cmd() # usage: mysql*() { ...; mysql_cmd cmd "$@"; }
|
93 |
9271
|
aaronmk
|
# auto-adds connection/login opts when specified
|
94 |
9014
|
aaronmk
|
{
|
95 |
|
|
echo_func
|
96 |
9099
|
aaronmk
|
local cmd="$1"; shift
|
97 |
9014
|
aaronmk
|
local ssh_server="$(localize_url "$ssh_server")"
|
98 |
|
|
local server="$(localize_url "$server")"
|
99 |
9050
|
aaronmk
|
if test "$ssh_server"; then
|
100 |
9014
|
aaronmk
|
local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
|
101 |
|
|
fi
|
102 |
|
|
|
103 |
|
|
local var=ssh_dest; local_inv
|
104 |
9701
|
aaronmk
|
command "time" ${ssh_dest:+ssh "$ssh_dest" }"$cmd" \
|
105 |
9014
|
aaronmk
|
${server:+ --host="$server" }${user:+--user="$user" } --password\
|
106 |
9694
|
aaronmk
|
${password+="$password"} --quick "$@" # --quick: don't buffer entire result
|
107 |
9014
|
aaronmk
|
}
|
108 |
9099
|
aaronmk
|
alias mysql_cmd='mysql_cmd "$FUNCNAME"'
|
109 |
9014
|
aaronmk
|
|
110 |
9099
|
aaronmk
|
fi # load new aliases
|
111 |
|
|
if self_being_included; then
|
112 |
|
|
|
113 |
9706
|
aaronmk
|
function mysql() # usage: [output_data=1] [data_only=1] [log_queries=] mysql ...
|
114 |
9095
|
aaronmk
|
{
|
115 |
9578
|
aaronmk
|
echo_func; kw_params output_data data_only query_verbosity
|
116 |
9557
|
aaronmk
|
if test "$data_only"; then local output_data="${output_data-1}"; fi
|
117 |
9578
|
aaronmk
|
local log_queries="${log_queries-1}"
|
118 |
9095
|
aaronmk
|
set_database
|
119 |
9101
|
aaronmk
|
|
120 |
9557
|
aaronmk
|
set -- ${database:+--database="$database" }--local-infile=1 \
|
121 |
|
|
--${data_only:+skip-}column-names "$@"
|
122 |
9150
|
aaronmk
|
if test "$output_data"; then echo_stdin|mysql_cmd --batch "$@"
|
123 |
9591
|
aaronmk
|
else cmd_log_fd=1 mysql_cmd ${log_queries:+--verbose --verbose --verbose }\
|
124 |
|
|
"$@" # --verbose*3: echo runtimes
|
125 |
9101
|
aaronmk
|
fi
|
126 |
9095
|
aaronmk
|
}
|
127 |
9014
|
aaronmk
|
|
128 |
9074
|
aaronmk
|
mysql_ANSI()
|
129 |
9014
|
aaronmk
|
{
|
130 |
|
|
echo_func
|
131 |
|
|
(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
|
132 |
|
|
}
|
133 |
|
|
|
134 |
9706
|
aaronmk
|
# always use ANSI mode, to support "" identifiers. `` are *still* supported in
|
135 |
|
|
# this mode, so it also works with SHOW CREATE TABLE output and dumpfiles.
|
136 |
|
|
alias mysql='mysql_ANSI'
|
137 |
|
|
|
138 |
9707
|
aaronmk
|
fi # load new aliases
|
139 |
|
|
if self_being_included; then
|
140 |
|
|
|
141 |
|
|
mysql_root() { echo_func; use_root; mysql "$@"; }
|
142 |
|
|
|
143 |
9777
|
aaronmk
|
mysql_truncate() { echo_func; mk_truncate|mysql; }
|
144 |
9466
|
aaronmk
|
|
145 |
9456
|
aaronmk
|
mysql_import() # usage: table=... [cols=...] [append=1] mysql_import <file
|
146 |
|
|
# without append=1, first ensures the table is empty
|
147 |
9430
|
aaronmk
|
{
|
148 |
|
|
echo_func
|
149 |
|
|
mk_table_esc
|
150 |
|
|
local mysql_load_data_format="${mysql_load_data_format-\
|
151 |
|
|
FIELDS TERMINATED BY ','
|
152 |
|
|
OPTIONALLY ENCLOSED BY '\"'
|
153 |
|
|
}"
|
154 |
|
|
|
155 |
9466
|
aaronmk
|
if test ! "$append"; then mysql_truncate; fi
|
156 |
9430
|
aaronmk
|
mysql_load_data_format="${mysql_load_data_format%
|
157 |
|
|
}"
|
158 |
9642
|
aaronmk
|
ssh2local # ssh does not tunnel nonstandard fds
|
159 |
9777
|
aaronmk
|
mysql "$@" 40<&0 <<EOF
|
160 |
9651
|
aaronmk
|
LOAD DATA LOCAL INFILE '/dev/fd/40'
|
161 |
9660
|
aaronmk
|
${append:+IGNORE }INTO TABLE $table_esc
|
162 |
9430
|
aaronmk
|
$mysql_load_data_format
|
163 |
|
|
IGNORE 1 LINES
|
164 |
|
|
EOF
|
165 |
|
|
}
|
166 |
|
|
|
167 |
9152
|
aaronmk
|
mysql_export() # does not support CSV
|
168 |
9348
|
aaronmk
|
# caller usage: {query=... | table=... [cols=...] [filter=...]} mysql_export
|
169 |
|
|
# cmd line usage: [limit=...] caller
|
170 |
9102
|
aaronmk
|
{
|
171 |
|
|
echo_func
|
172 |
9463
|
aaronmk
|
mk_select_var
|
173 |
9152
|
aaronmk
|
|
174 |
9777
|
aaronmk
|
output_data=1 mysql "$@" <<<"$query"
|
175 |
9152
|
aaronmk
|
}
|
176 |
|
|
|
177 |
|
|
mysql_export_outfile() # supports CSV, but requires the FILE privilege
|
178 |
|
|
{
|
179 |
|
|
echo_func
|
180 |
9102
|
aaronmk
|
: "${file:?}"
|
181 |
9463
|
aaronmk
|
mk_select_var
|
182 |
9102
|
aaronmk
|
local mysql_load_data_format="${mysql_load_data_format-\
|
183 |
|
|
FIELDS TERMINATED BY ','
|
184 |
|
|
OPTIONALLY ENCLOSED BY '\"'
|
185 |
|
|
}"
|
186 |
|
|
|
187 |
|
|
local head="${query%%FROM*}" # includes trailing newline
|
188 |
9105
|
aaronmk
|
head="${head%
|
189 |
|
|
}"
|
190 |
9102
|
aaronmk
|
local tail="${query#$head}"
|
191 |
9106
|
aaronmk
|
mysql_load_data_format="${mysql_load_data_format%
|
192 |
|
|
}"
|
193 |
9777
|
aaronmk
|
mysql "$@" <<EOF
|
194 |
9105
|
aaronmk
|
$head
|
195 |
|
|
INTO OUTFILE '$file'
|
196 |
9106
|
aaronmk
|
$mysql_load_data_format
|
197 |
|
|
$tail
|
198 |
9102
|
aaronmk
|
EOF
|
199 |
|
|
}
|
200 |
|
|
|
201 |
10649
|
aaronmk
|
mysqldump() # usage: [schema=1 | data=1] [create_db=1] mysqldump db [table...]
|
202 |
9014
|
aaronmk
|
{
|
203 |
10649
|
aaronmk
|
echo_func
|
204 |
|
|
echo_vars schema data create_db
|
205 |
9231
|
aaronmk
|
|
206 |
10649
|
aaronmk
|
local var=create_db; local_inv
|
207 |
|
|
mysql_cmd ${database:+--databases "$database" ${no_create_db:+--tables }} \
|
208 |
9694
|
aaronmk
|
--lock-tables=false --set-charset \
|
209 |
10446
|
aaronmk
|
${postgres_compat:+${data:+--compatible=postgresql }--add-locks=false }\
|
210 |
9014
|
aaronmk
|
${schema:+--no-data }${data:+--no-create-info }"$@"
|
211 |
|
|
}
|
212 |
|
|
|
213 |
9074
|
aaronmk
|
mysqldump_diffable()
|
214 |
9014
|
aaronmk
|
{
|
215 |
|
|
echo_func
|
216 |
9550
|
aaronmk
|
mysqldump "$@"|{ pipe_delay; echo_run sed 's/^(-- Dump completed).*$/\1/'; }
|
217 |
9014
|
aaronmk
|
}
|
218 |
|
|
|
219 |
9588
|
aaronmk
|
mysql_rm_privileged_statements()
|
220 |
|
|
{
|
221 |
|
|
echo_func
|
222 |
|
|
grep -vE -e '^/\*!40000 ALTER TABLE `.*` (DIS|EN)ABLE KEYS \*/;$' \
|
223 |
|
|
-e '^(UN)?LOCK TABLES( `.*` WRITE)?;$'
|
224 |
|
|
}
|
225 |
9017
|
aaronmk
|
|
226 |
9632
|
aaronmk
|
## permissions
|
227 |
9588
|
aaronmk
|
|
228 |
9632
|
aaronmk
|
mysql_seal_table() # usage: table=... user=... mysql_seal_table
|
229 |
|
|
# prevents further modifications to a table by a user
|
230 |
|
|
{
|
231 |
9634
|
aaronmk
|
echo_func; kw_params table user; : "${table:?}" "${user:?}"; mk_table_esc
|
232 |
9632
|
aaronmk
|
|
233 |
9635
|
aaronmk
|
benign_error=1 mysql_root <<<"REVOKE ALL PRIVILEGES ON $table FROM '$user'@'%'" || true
|
234 |
9636
|
aaronmk
|
benign_error=1 mysql_root <<<"REVOKE GRANT OPTION ON $table FROM '$user'@'%'" || true
|
235 |
9632
|
aaronmk
|
}
|
236 |
|
|
|
237 |
|
|
|
238 |
9014
|
aaronmk
|
### PostgreSQL
|
239 |
|
|
|
240 |
9648
|
aaronmk
|
alias use_pg='declare prefix=pg_; import_vars; unset prefix'
|
241 |
|
|
|
242 |
9649
|
aaronmk
|
fi # load new aliases
|
243 |
|
|
if self_being_included; then
|
244 |
|
|
|
245 |
10763
|
aaronmk
|
# usage: pg_*() { ...; pg_cmd "$@"; } (uses alias)
|
246 |
|
|
function pg_cmd() # usage for fn: pg_*() { ...; pg_cmd cmd "$@"; }
|
247 |
|
|
# auto-adds connection/login opts when specified
|
248 |
9646
|
aaronmk
|
{
|
249 |
10763
|
aaronmk
|
echo_func
|
250 |
9646
|
aaronmk
|
|
251 |
9649
|
aaronmk
|
use_pg
|
252 |
10154
|
aaronmk
|
log- 2
|
253 |
9649
|
aaronmk
|
local PGHOST="$server"; export PGHOST
|
254 |
|
|
local PGUSER="$user"; export PGUSER
|
255 |
|
|
local PGPASSWORD="$password"; export PGPASSWORD
|
256 |
|
|
local PGDATABASE="$database"; export PGDATABASE
|
257 |
|
|
log+ 2
|
258 |
|
|
|
259 |
10763
|
aaronmk
|
command "time" "$@"
|
260 |
|
|
}
|
261 |
|
|
alias pg_cmd='"pg_cmd" "${FUNCNAME%%__*}"'
|
262 |
|
|
|
263 |
|
|
fi # load new aliases
|
264 |
|
|
if self_being_included; then
|
265 |
|
|
|
266 |
|
|
psql() # usage: [stdin=copy_pstdin_file] psql <<<"cmds, e.g. \copy from pstdin"
|
267 |
|
|
{
|
268 |
|
|
echo_func; kw_params stdin
|
269 |
|
|
|
270 |
9646
|
aaronmk
|
if can_log; then set -- --echo-all --echo-hidden "$@"; fi
|
271 |
9651
|
aaronmk
|
local redirs=("${redirs[@]}" '40<&0' "0<${stdin:-&20}" '41>&1')
|
272 |
9646
|
aaronmk
|
(
|
273 |
10295
|
aaronmk
|
# hide stack traces/DETAIL sections of error messages at verbosity <2
|
274 |
|
|
if ! clog++ can_log; then echo '\set VERBOSITY terse'; fi
|
275 |
9646
|
aaronmk
|
if can_log; then cat <<'EOF'
|
276 |
|
|
\timing on
|
277 |
|
|
SET client_min_messages = NOTICE;
|
278 |
|
|
EOF
|
279 |
|
|
fi
|
280 |
|
|
cat
|
281 |
10763
|
aaronmk
|
)|cmd_log_fd=1 pg_cmd --file /dev/fd/40 --output /dev/fd/41 \
|
282 |
9647
|
aaronmk
|
--set ON_ERROR_STOP=1 --quiet "$@"
|
283 |
9646
|
aaronmk
|
# --output is for query *results*, not echoed statements
|
284 |
|
|
}
|
285 |
|
|
|
286 |
10755
|
aaronmk
|
pg_export() # usage: mk_select_opts... [pg_copy_format=CSV...] pg_export >out
|
287 |
9014
|
aaronmk
|
{
|
288 |
|
|
echo_func
|
289 |
9463
|
aaronmk
|
mk_select_var
|
290 |
9014
|
aaronmk
|
local pg_copy_format="${pg_copy_format-CSV HEADER}"
|
291 |
|
|
|
292 |
9084
|
aaronmk
|
psql "$@" <<<"COPY ($query) TO STDOUT $pg_copy_format;"
|
293 |
9014
|
aaronmk
|
}
|
294 |
|
|
|
295 |
9074
|
aaronmk
|
pg_header()
|
296 |
9014
|
aaronmk
|
{
|
297 |
|
|
echo_func
|
298 |
|
|
local pg_copy_format="CSV HEADER" limit=0
|
299 |
9083
|
aaronmk
|
pg_export "$@"|echo_stdout
|
300 |
9014
|
aaronmk
|
}
|
301 |
|
|
|
302 |
9074
|
aaronmk
|
pg_export_table_no_header()
|
303 |
9014
|
aaronmk
|
{
|
304 |
|
|
echo_func
|
305 |
|
|
local pg_copy_format="CSV"
|
306 |
9083
|
aaronmk
|
pg_export "$@"
|
307 |
9014
|
aaronmk
|
}
|
308 |
|
|
|
309 |
10825
|
aaronmk
|
pg_export_table_to_dir()
|
310 |
|
|
{
|
311 |
|
|
echo_func
|
312 |
|
|
local table="$1"; shift; mk_table_esc
|
313 |
|
|
stdout="$exports_dir/$table.csv" to_file pg_export "$@"
|
314 |
|
|
}
|
315 |
|
|
|
316 |
9074
|
aaronmk
|
pg_export_table_to_dir_no_header()
|
317 |
9014
|
aaronmk
|
{
|
318 |
|
|
echo_func
|
319 |
|
|
local table="$1"; shift; mk_table_esc
|
320 |
9476
|
aaronmk
|
stdout="$exports_dir/$table.no_header.cols=$(pg_header).csv" to_file \
|
321 |
9042
|
aaronmk
|
pg_export_table_no_header "$@"
|
322 |
9014
|
aaronmk
|
}
|
323 |
|
|
|
324 |
10764
|
aaronmk
|
pg_dump() # usage: [schema=...] [struct=1 | data=1] [compress=1] [owners=1] \
|
325 |
10774
|
aaronmk
|
# [create_schema=] pg_dump [opts...] >out
|
326 |
10764
|
aaronmk
|
{
|
327 |
10774
|
aaronmk
|
echo_func; kw_params struct data plain owners create_schema
|
328 |
10775
|
aaronmk
|
local create_schema="${create_schema-1}"
|
329 |
10764
|
aaronmk
|
|
330 |
|
|
# subset
|
331 |
|
|
if test "$schema"; then set -- --schema="\"$schema\"" "$@"; fi
|
332 |
|
|
|
333 |
|
|
# format
|
334 |
|
|
if test "$compress"; then set -- "$@" --format=c --compress=9
|
335 |
|
|
else set -- "$@" --format=p --inserts # plain
|
336 |
|
|
fi
|
337 |
|
|
if test "$struct" ; then set -- "$@" --schema-only; fi
|
338 |
|
|
if test "$data" ; then set -- "$@" --data-only ; fi
|
339 |
|
|
if test ! "$owners"; then set -- "$@" --no-owner ; fi
|
340 |
|
|
|
341 |
10774
|
aaronmk
|
(
|
342 |
|
|
# filter output
|
343 |
|
|
if test ! "$create_schema"; then
|
344 |
|
|
fd=1 redirs= filter_fd sed 's/^CREATE SCHEMA/--&/'
|
345 |
|
|
fi
|
346 |
|
|
|
347 |
|
|
pg_cmd "$@"
|
348 |
|
|
)
|
349 |
10764
|
aaronmk
|
}
|
350 |
|
|
|
351 |
9014
|
aaronmk
|
fi
|