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