1 |
8161
|
aaronmk
|
#!/bin/bash
|
2 |
8273
|
aaronmk
|
set -o errexit
|
3 |
|
|
|
4 |
|
|
if false; then #### run script template:
|
5 |
8161
|
aaronmk
|
#!/bin/bash
|
6 |
8273
|
aaronmk
|
set -o errexit
|
7 |
|
|
. "$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run
|
8 |
8161
|
aaronmk
|
|
9 |
8273
|
aaronmk
|
import ()
|
10 |
|
|
{
|
11 |
8282
|
aaronmk
|
echo_func "$FUNCNAME" "$@"
|
12 |
8273
|
aaronmk
|
before_import_cmds
|
13 |
8280
|
aaronmk
|
"$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run import
|
14 |
8273
|
aaronmk
|
after_import_cmds
|
15 |
|
|
}
|
16 |
8161
|
aaronmk
|
|
17 |
8273
|
aaronmk
|
run_cmd "$@"
|
18 |
|
|
fi ####
|
19 |
8213
|
aaronmk
|
|
20 |
8273
|
aaronmk
|
. "$(dirname "${BASH_SOURCE[0]}")"/util.run
|
21 |
|
|
|
22 |
|
|
root_dir="$(dirname "${BASH_SOURCE[0]}")"/..
|
23 |
8193
|
aaronmk
|
bin_dir="$root_dir"/bin
|
24 |
8161
|
aaronmk
|
|
25 |
8273
|
aaronmk
|
: "${schema="$(cd "$top_dir"/..; basename "$PWD")"}"
|
26 |
|
|
: "${table="$( cd "$top_dir" ; basename "$PWD")"}"
|
27 |
8195
|
aaronmk
|
export schema table
|
28 |
8161
|
aaronmk
|
|
29 |
8273
|
aaronmk
|
psql () # usage: ([file=...] [dir=...]; self)
|
30 |
8161
|
aaronmk
|
{
|
31 |
8282
|
aaronmk
|
echo_func "$FUNCNAME" "$@"
|
32 |
8273
|
aaronmk
|
local dir="$dir"
|
33 |
|
|
if test -n "$file"; then
|
34 |
|
|
set -- --file "$file" "$@"
|
35 |
|
|
: "${dir:=$(dirname "$file")}"
|
36 |
|
|
fi
|
37 |
|
|
: "${dir:=$top_dir}"
|
38 |
|
|
|
39 |
8196
|
aaronmk
|
(cat <<EOF
|
40 |
8273
|
aaronmk
|
\cd $dir
|
41 |
8196
|
aaronmk
|
\set schema "$schema"
|
42 |
|
|
\set table "$table"
|
43 |
|
|
\set table_str '''"$table"'''
|
44 |
|
|
SET search_path TO "$schema", util;
|
45 |
|
|
EOF
|
46 |
|
|
cat)|
|
47 |
8277
|
aaronmk
|
echo_run env no_search_path=1 "$bin_dir"/psql_verbose_vegbien "$@"
|
48 |
8161
|
aaronmk
|
}
|
49 |
|
|
|
50 |
8273
|
aaronmk
|
input_make ()
|
51 |
8242
|
aaronmk
|
{
|
52 |
8282
|
aaronmk
|
echo_func "$FUNCNAME" "$@"
|
53 |
8277
|
aaronmk
|
echo_run env make --directory="$top_dir"/.. "${@/#/$table/}"
|
54 |
8242
|
aaronmk
|
}
|
55 |
|
|
|
56 |
8161
|
aaronmk
|
map_table ()
|
57 |
|
|
{
|
58 |
8282
|
aaronmk
|
echo_func "$FUNCNAME" "$@"
|
59 |
8273
|
aaronmk
|
input_make map.csv
|
60 |
8214
|
aaronmk
|
psql <<EOF
|
61 |
8191
|
aaronmk
|
SELECT util.reset_map_table('pg_temp.map');
|
62 |
8161
|
aaronmk
|
\copy pg_temp.map FROM 'map.csv' CSV HEADER;
|
63 |
8191
|
aaronmk
|
SELECT util.set_col_names('"$table"', 'pg_temp.map'::regclass);
|
64 |
8161
|
aaronmk
|
EOF
|
65 |
|
|
}
|
66 |
8198
|
aaronmk
|
|
67 |
8282
|
aaronmk
|
mk_derived ()
|
68 |
|
|
{
|
69 |
|
|
echo_func "$FUNCNAME" "$@"
|
70 |
|
|
"$root_dir"/schemas/VegCore/mk_derived
|
71 |
|
|
}
|
72 |
8245
|
aaronmk
|
|
73 |
|
|
remake_VegBIEN_mappings ()
|
74 |
|
|
{
|
75 |
8282
|
aaronmk
|
echo_func "$FUNCNAME" "$@"
|
76 |
8277
|
aaronmk
|
echo_run rm header.csv map.csv # remake them
|
77 |
8273
|
aaronmk
|
yes|input_make test
|
78 |
8245
|
aaronmk
|
}
|
79 |
8273
|
aaronmk
|
|
80 |
|
|
postprocess () # overridable
|
81 |
|
|
{
|
82 |
8282
|
aaronmk
|
echo_func "$FUNCNAME" "$@"
|
83 |
8273
|
aaronmk
|
local file="$top_dir"/postprocess.sql
|
84 |
|
|
if test -e "$file"; then psql "$@"; fi
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
import ()
|
88 |
|
|
{
|
89 |
8282
|
aaronmk
|
echo_func "$FUNCNAME" "$@"
|
90 |
|
|
map_table
|
91 |
|
|
postprocess
|
92 |
|
|
mk_derived
|
93 |
8273
|
aaronmk
|
}
|
94 |
|
|
|
95 |
|
|
run_cmd "$@"
|