Project

General

Profile

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 9039 aaronmk
func_override import__table_run
8 9074 aaronmk
import()
9 8273 aaronmk
{
10 8881 aaronmk
	echo_func
11 8273 aaronmk
	before_import_cmds
12 9681 aaronmk
	self_make import__table_run "$@"
13 8273 aaronmk
	after_import_cmds
14
}
15
fi ####
16 8213 aaronmk
17 8296 aaronmk
. "$(dirname "${BASH_SOURCE[0]}")"/import.run
18 9854 aaronmk
.rel subdir.run
19
.rel ../sh/db_make.sh
20 8273 aaronmk
21 8705 aaronmk
if self_not_included; then
22
23 9585 aaronmk
: "${table=$subdir}"; export table
24
25
table_make() { subdir_make "$@"; }
26
27 9935 aaronmk
install_log_rel=logs/install.log.sql
28
29 9934 aaronmk
table_make_install()
30
{
31 9935 aaronmk
	echo_func; local install_log="${install_log-$top_dir/$install_log_rel}"
32 9934 aaronmk
	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 9936 aaronmk
datasrc_make_install()
40
{
41
	echo_func
42
	subdir=. install_log="$top_dir/../$install_log_rel" table_make_install "$@"
43
}
44
45 10248 aaronmk
postprocess_sql="$top_dir"/postprocess.sql
46
47 10249 aaronmk
is_view="$(test -e "$postprocess_sql" \
48
&& grep -F force_update_view "$postprocess_sql" >/dev/null; exit2bool)"
49
50 10368 aaronmk
map_table="\"~$table.map\"" # sort after other tables to avoid clutter
51
52 9074 aaronmk
map_table()
53 10210 aaronmk
# note that collisions may prevent all renames from being made at once.
54 10211 aaronmk
# if this is the case, run map_table repeatedly until no more renames are made:
55
# $ while true; do .../run map_table; done
56 10210 aaronmk
# collisions may result if the staging table gets messed up (e.g. due to missing
57
# input columns in map.csv).
58 8161 aaronmk
{
59 9838 aaronmk
	echo_func; set_make_vars
60 10250 aaronmk
	if test "$is_view"; then return 0; fi # do not rename view columns
61
62 9386 aaronmk
	table_make map.csv
63 8214 aaronmk
	psql <<EOF
64 10156 aaronmk
SELECT util.reset_map_table('$map_table');
65
ALTER TABLE $map_table DISABLE TRIGGER map_filter_insert;
66
\copy $map_table FROM 'map.csv' CSV HEADER;
67 10366 aaronmk
SELECT util.set_col_names_with_metadata('"$table"', '$map_table');
68 8161 aaronmk
EOF
69
}
70 8198 aaronmk
71 9838 aaronmk
mk_derived()
72
{ echo_func; set_make_vars; "$root_dir"/schemas/VegCore/mk_derived; }
73 8245 aaronmk
74 9074 aaronmk
remake_VegBIEN_mappings()
75 8245 aaronmk
{
76 9838 aaronmk
	echo_func; set_make_vars
77 9047 aaronmk
	public_schema_exists || return 0
78 10252 aaronmk
	if remaking; then in_top_dir rm map.csv; fi # remake it
79 9967 aaronmk
	piped_cmd yes|table_make test
80 8245 aaronmk
}
81 8273 aaronmk
82 10273 aaronmk
custom_postprocess() # overridable
83 8273 aaronmk
{
84 9838 aaronmk
	echo_func; set_make_vars
85 10248 aaronmk
	local file="$postprocess_sql"
86 8273 aaronmk
	if test -e "$file"; then psql "$@"; fi
87
}
88
89 9836 aaronmk
load_data()
90 9392 aaronmk
{
91 9838 aaronmk
	echo_func; set_make_vars
92 9937 aaronmk
	datasrc_make_install schema
93 9934 aaronmk
	table_make_install ${_remake:+re}install # just the table
94 9392 aaronmk
}
95
96 10274 aaronmk
postprocess()
97 8273 aaronmk
{
98 9838 aaronmk
	echo_func; set_make_vars
99 10275 aaronmk
	kw_params can_test; local can_test="${can_test-1}"
100 8282 aaronmk
	map_table
101 10273 aaronmk
	custom_postprocess
102 10171 aaronmk
	mk_derived
103 10367 aaronmk
	if test "$can_test"; then remake_VegBIEN_mappings; fi
104 8273 aaronmk
}
105 8705 aaronmk
106 10274 aaronmk
import()
107
{
108
	echo_func; set_make_vars
109
	self_make load_data
110
	postprocess
111
}
112
113 8705 aaronmk
fi