1
|
#!/bin/bash -e
|
2
|
|
3
|
if false; then #### run script template:
|
4
|
#!/bin/bash -e
|
5
|
. "$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run
|
6
|
|
7
|
func_override import__table_run
|
8
|
import()
|
9
|
{
|
10
|
echo_func
|
11
|
before_import_cmds
|
12
|
self_make import__table_run "$@"
|
13
|
after_import_cmds
|
14
|
}
|
15
|
fi ####
|
16
|
|
17
|
. "$(dirname "${BASH_SOURCE[0]}")"/import.run
|
18
|
.rel subdir.run
|
19
|
.rel ../sh/db_make.sh
|
20
|
|
21
|
if self_not_included; then
|
22
|
|
23
|
: "${table=$subdir}"; export table
|
24
|
|
25
|
table_make() { subdir_make "$@"; }
|
26
|
|
27
|
install_log_rel=logs/install.log.sql
|
28
|
|
29
|
table_make_install()
|
30
|
{
|
31
|
echo_func; local install_log="${install_log-$top_dir/$install_log_rel}"
|
32
|
local verbosity_min= # install logs require default verbosity
|
33
|
set -- table_make "$@"
|
34
|
if remaking || test ! -e "$install_log"; then "$@" # OK to clobber log
|
35
|
else benign_error=1 noclobber=1 "$@" || true
|
36
|
fi
|
37
|
}
|
38
|
|
39
|
datasrc_make_install()
|
40
|
{
|
41
|
echo_func
|
42
|
subdir=. install_log="$top_dir/../$install_log_rel" table_make_install "$@"
|
43
|
}
|
44
|
|
45
|
postprocess_sql="$top_dir"/postprocess.sql
|
46
|
|
47
|
is_view="$(test -e "$postprocess_sql" \
|
48
|
&& grep -F force_update_view "$postprocess_sql" >/dev/null; exit2bool)"
|
49
|
|
50
|
map_table="\"~$table.map\"" # sort after other tables to avoid clutter
|
51
|
|
52
|
reset_col_names()
|
53
|
{
|
54
|
echo_func; set_make_vars
|
55
|
if test "$is_view"; then return 0; fi # do not rename view columns
|
56
|
psql <<EOF
|
57
|
SELECT util.reset_col_names('"$table"', '$map_table')
|
58
|
EOF
|
59
|
}
|
60
|
|
61
|
map_table()
|
62
|
# note that collisions may prevent all renames from being made at once.
|
63
|
# if this is the case, run map_table repeatedly until no more renames are made:
|
64
|
# $ while true; do .../run map_table; done
|
65
|
# collisions may result if the staging table gets messed up (e.g. due to missing
|
66
|
# input columns in map.csv).
|
67
|
{
|
68
|
echo_func; set_make_vars
|
69
|
if test "$is_view"; then return 0; fi # do not rename view columns
|
70
|
|
71
|
if remaking; then reset_col_names; fi
|
72
|
table_make map.csv
|
73
|
psql <<EOF
|
74
|
SELECT util.reset_map_table('$map_table');
|
75
|
ALTER TABLE $map_table DISABLE TRIGGER map_filter_insert;
|
76
|
\copy $map_table FROM 'map.csv' CSV HEADER;
|
77
|
SELECT util.set_col_names_with_metadata('"$table"', '$map_table');
|
78
|
EOF
|
79
|
}
|
80
|
|
81
|
mk_derived()
|
82
|
{ echo_func; set_make_vars; "$root_dir"/schemas/VegCore/mk_derived; }
|
83
|
|
84
|
remake_VegBIEN_mappings()
|
85
|
{
|
86
|
echo_func; set_make_vars
|
87
|
public_schema_exists || return 0
|
88
|
if remaking; then in_top_dir rm -f map.csv; fi # remake it
|
89
|
piped_cmd yes|table_make test
|
90
|
}
|
91
|
|
92
|
custom_postprocess() # overridable
|
93
|
{
|
94
|
echo_func; set_make_vars
|
95
|
local file="$postprocess_sql"
|
96
|
if test -e "$file"; then psql "$@"; fi
|
97
|
}
|
98
|
|
99
|
load_data()
|
100
|
{
|
101
|
echo_func; set_make_vars
|
102
|
if remaking; then in_top_dir rm -f header.csv; fi # remake it
|
103
|
datasrc_make_install schema
|
104
|
is_view="$is_view" table_make_install ${_remake:+re}install # just the table
|
105
|
}
|
106
|
|
107
|
trim_table()
|
108
|
{
|
109
|
echo_func; set_make_vars
|
110
|
if test "$is_view"; then return 0; fi # do not modify view columns
|
111
|
psql <<EOF
|
112
|
SELECT util.trim('"$table"', '$map_table')
|
113
|
EOF
|
114
|
}
|
115
|
|
116
|
postprocess()
|
117
|
{
|
118
|
echo_func; set_make_vars
|
119
|
kw_params can_test; local can_test="${can_test-1}"
|
120
|
if remaking; then trim_table; fi
|
121
|
map_table
|
122
|
custom_postprocess
|
123
|
mk_derived
|
124
|
if test "$can_test"; then remake_VegBIEN_mappings; fi
|
125
|
}
|
126
|
|
127
|
import()
|
128
|
{
|
129
|
echo_func; set_make_vars
|
130
|
self_make load_data
|
131
|
postprocess
|
132
|
}
|
133
|
|
134
|
fi
|