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