Project

General

Profile

1 8161 aaronmk
#!/bin/bash
2 8273 aaronmk
set -o errexit
3
4
if false; then #### run script template:
5 8161 aaronmk
#!/bin/bash
6 8273 aaronmk
set -o errexit
7
. "$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run
8 8161 aaronmk
9 8273 aaronmk
import ()
10
{
11 8282 aaronmk
	echo_func "$FUNCNAME" "$@"
12 8273 aaronmk
	before_import_cmds
13 8280 aaronmk
	"$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run import
14 8273 aaronmk
	after_import_cmds
15
}
16
fi ####
17 8213 aaronmk
18 8273 aaronmk
. "$(dirname "${BASH_SOURCE[0]}")"/util.run
19
20
root_dir="$(dirname "${BASH_SOURCE[0]}")"/..
21 8193 aaronmk
bin_dir="$root_dir"/bin
22 8161 aaronmk
23 8273 aaronmk
: "${schema="$(cd "$top_dir"/..; basename "$PWD")"}"
24
: "${table="$( cd "$top_dir"   ; basename "$PWD")"}"
25 8195 aaronmk
export schema table
26 8161 aaronmk
27 8273 aaronmk
psql () # usage: ([file=...] [dir=...]; self)
28 8161 aaronmk
{
29 8282 aaronmk
	echo_func "$FUNCNAME" "$@"
30 8273 aaronmk
	local dir="$dir"
31
	if test -n "$file"; then
32
		set -- --file "$file" "$@"
33
		: "${dir:=$(dirname "$file")}"
34
	fi
35
	: "${dir:=$top_dir}"
36
37 8196 aaronmk
	(cat <<EOF
38 8273 aaronmk
\cd $dir
39 8196 aaronmk
\set schema "$schema"
40
\set table "$table"
41
\set table_str '''"$table"'''
42
SET search_path TO "$schema", util;
43
EOF
44
	cat)|
45 8277 aaronmk
	echo_run env no_search_path=1 "$bin_dir"/psql_verbose_vegbien "$@"
46 8161 aaronmk
}
47
48 8273 aaronmk
input_make ()
49 8242 aaronmk
{
50 8282 aaronmk
	echo_func "$FUNCNAME" "$@"
51 8277 aaronmk
	echo_run env make --directory="$top_dir"/.. "${@/#/$table/}"
52 8242 aaronmk
}
53
54 8161 aaronmk
map_table ()
55
{
56 8282 aaronmk
	echo_func "$FUNCNAME" "$@"
57 8273 aaronmk
	input_make map.csv
58 8214 aaronmk
	psql <<EOF
59 8191 aaronmk
SELECT util.reset_map_table('pg_temp.map');
60 8161 aaronmk
\copy pg_temp.map FROM 'map.csv' CSV HEADER;
61 8191 aaronmk
SELECT util.set_col_names('"$table"', 'pg_temp.map'::regclass);
62 8161 aaronmk
EOF
63
}
64 8198 aaronmk
65 8282 aaronmk
mk_derived ()
66
{
67
	echo_func "$FUNCNAME" "$@"
68
	"$root_dir"/schemas/VegCore/mk_derived
69
}
70 8245 aaronmk
71
remake_VegBIEN_mappings ()
72
{
73 8282 aaronmk
	echo_func "$FUNCNAME" "$@"
74 8277 aaronmk
	echo_run rm header.csv map.csv # remake them
75 8273 aaronmk
	yes|input_make test
76 8245 aaronmk
}
77 8273 aaronmk
78
postprocess () # overridable
79
{
80 8282 aaronmk
	echo_func "$FUNCNAME" "$@"
81 8273 aaronmk
	local file="$top_dir"/postprocess.sql
82
	if test -e "$file"; then psql "$@"; fi
83
}
84
85
import ()
86
{
87 8282 aaronmk
	echo_func "$FUNCNAME" "$@"
88
	map_table
89
	postprocess
90
	mk_derived
91 8273 aaronmk
}