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
	begin_target
11
	before_import_cmds
12
	with_rm 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
	make --directory="$top_dir/.." schema # works w/ old-style srcs w/o ../run
55
	#"$top_dir/../run" schema/make # uncomment once everything is new-style
56
	is_view="$is_view" table_make_install ${_remake:+re}install # just the table
57
}
58

    
59
trim_table()
60
{
61
	begin_target
62
	if test "$is_view"; then return 0; fi # do not modify view columns
63
	psql <<EOF
64
SELECT util.trim('"$table"', '$map_table');
65
EOF
66
}
67

    
68
reset_col_names()
69
{
70
	begin_target
71
	if test "$is_view"; then return 0; fi # do not rename view columns
72
	psql <<EOF
73
SELECT util.reset_col_names('"$table"', '$map_table');
74
EOF
75
}
76

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

    
97
test_() { begin_target; piped_cmd yes|table_make test; }
98

    
99
mappings()
100
{
101
	begin_target
102
	public_schema_exists || return 0
103
	if remaking; then in_top_dir rm -f map.csv; fi # remake it
104
	test_
105
}
106

    
107
custom_postprocess() # overridable
108
{
109
	begin_target
110
	local file="$postprocess_sql"
111
	if test -e "$file"; then
112
		table_make "$(basename "$file")" # apply renames to SQL statements
113
		psql "$@"
114
	fi
115
}
116

    
117
mk_derived()
118
{ begin_target; "$root_dir"/schemas/VegCore/mk_derived; }
119

    
120
postprocess()
121
{
122
	begin_target
123
	kw_params can_test; local can_test="${can_test-1}"
124
	if remaking; then trim_table; fi
125
	map_table
126
	custom_postprocess
127
	mk_derived
128
	if test "$can_test"; then mappings; fi
129
}
130

    
131
import()
132
{
133
	begin_target
134
	with_rm load_data
135
	postprocess
136
}
137

    
138
fi
(14-14/17)