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