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