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
: "${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
map_table()
46
# note that collisions may prevent all renames from being made at once.
47
# if this is the case, run map_table repeatedly until no more renames are made:
48
# $ while true; do .../run map_table; done
49
# collisions may result if the staging table gets messed up (e.g. due to missing
50
# input columns in map.csv).
51
{
52
	echo_func; set_make_vars
53
	table_make map.csv
54
	local map_table="\"~$table.map\"" # sort after other tables to avoid clutter
55
	psql <<EOF
56
SELECT util.reset_map_table('$map_table');
57
ALTER TABLE $map_table DISABLE TRIGGER map_filter_insert;
58
\copy $map_table FROM 'map.csv' CSV HEADER;
59
SELECT util.set_col_names_with_metadata('"$table"', '$map_table'::regclass);
60
EOF
61
}
62

    
63
mk_derived()
64
{ echo_func; set_make_vars; "$root_dir"/schemas/VegCore/mk_derived; }
65

    
66
remake_VegBIEN_mappings()
67
{
68
	echo_func; set_make_vars
69
	public_schema_exists || return 0
70
	if remaking; then in_top_dir rm header.csv map.csv; fi # remake them
71
	piped_cmd yes|table_make test
72
}
73

    
74
postprocess() # overridable
75
{
76
	echo_func; set_make_vars
77
	local file="$top_dir"/postprocess.sql
78
	if test -e "$file"; then psql "$@"; fi
79
}
80

    
81
load_data()
82
{
83
	echo_func; set_make_vars
84
	datasrc_make_install schema
85
	table_make_install ${_remake:+re}install # just the table
86
}
87

    
88
import()
89
{
90
	echo_func; set_make_vars
91
	self_make load_data
92
	map_table
93
	postprocess
94
	mk_derived
95
	remake_VegBIEN_mappings
96
}
97

    
98
fi
(5-5/7)