Project

General

Profile

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
	prep_try; if ! remaking && pg_table_exists; then rethrow; return 0; fi
35
		rethrow
36
	local install_log="${install_log-$top_dir/$install_log_rel}"
37
	local verbosity_min= # install logs require default verbosity
38
	if ! remaking && test -e "$install_log"; then local_export noclobber=1; fi
39
	table_make "$@"
40
}
41

    
42
datasrc_make_install()
43
{
44
	echo_func
45
	subdir=. install_log="$top_dir/../$install_log_rel" table_make_install "$@"
46
}
47

    
48
postprocess_sql="$top_dir"/postprocess.sql
49

    
50
is_view="$(test -e "$postprocess_sql" \
51
&& grep -F force_update_view "$postprocess_sql" >/dev/null; exit2bool)"
52

    
53
map_table="\"~$table.map\"" # sort after other tables to avoid clutter
54

    
55
##### targets
56

    
57
load_data()
58
{
59
	begin_target
60
	if remaking; then in_top_dir rm -f header.csv; fi # remake it
61
	datasrc_make_install schema
62
	is_view="$is_view" table_make_install ${_remake:+re}install # just the table
63
}
64

    
65
trim_table()
66
{
67
	begin_target
68
	if test "$is_view"; then return 0; fi # do not modify view columns
69
	psql <<EOF
70
SELECT util.trim('"$table"', '$map_table')
71
EOF
72
}
73

    
74
reset_col_names()
75
{
76
	begin_target
77
	if test "$is_view"; then return 0; fi # do not rename view columns
78
	psql <<EOF
79
SELECT util.reset_col_names('"$table"', '$map_table')
80
EOF
81
}
82

    
83
map_table()
84
# note that collisions may prevent all renames from being made at once.
85
# if this is the case, run map_table repeatedly until no more renames are made:
86
# $ while true; do .../run map_table; done
87
# collisions may result if the staging table gets messed up (e.g. due to missing
88
# input columns in map.csv).
89
{
90
	begin_target
91
	if test "$is_view"; then return 0; fi # do not rename view columns
92
	
93
	if remaking; then reset_col_names; fi
94
	table_make map.csv
95
	psql <<EOF
96
SELECT util.reset_map_table('$map_table');
97
ALTER TABLE $map_table DISABLE TRIGGER map_filter_insert;
98
\copy $map_table FROM 'map.csv' CSV HEADER;
99
SELECT util.set_col_names_with_metadata('"$table"', '$map_table');
100
EOF
101
}
102

    
103
test_() { begin_target; piped_cmd yes|table_make test; }
104

    
105
mappings()
106
{
107
	begin_target
108
	prep_try; public_schema_exists || { rethrow; return 0; }; rethrow
109
	if remaking; then in_top_dir rm -f map.csv; fi # remake it
110
	test_
111
}
112

    
113
custom_postprocess() # overridable
114
{
115
	begin_target
116
	local file="$postprocess_sql"
117
	if test -e "$file"; then
118
		table_make "$(basename "$file")" # apply renames to SQL statements
119
		psql "$@"
120
	fi
121
}
122

    
123
mk_derived()
124
{ begin_target; "$root_dir"/schemas/VegCore/mk_derived; }
125

    
126
postprocess()
127
{
128
	begin_target
129
	kw_params can_test; local can_test="${can_test-1}"
130
	if remaking; then trim_table; fi
131
	map_table
132
	custom_postprocess
133
	mk_derived
134
	if test "$can_test"; then mappings; fi
135
}
136

    
137
import()
138
{
139
	begin_target
140
	self_make load_data
141
	postprocess
142
}
143

    
144
fi
(13-13/16)