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
	before_import_cmds
12
	echo_cmd "$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run import
13
	after_import_cmds
14
}
15 8161 aaronmk
16 8273 aaronmk
run_cmd "$@"
17
fi ####
18 8213 aaronmk
19 8273 aaronmk
. "$(dirname "${BASH_SOURCE[0]}")"/util.run
20
21
root_dir="$(dirname "${BASH_SOURCE[0]}")"/..
22 8193 aaronmk
bin_dir="$root_dir"/bin
23 8161 aaronmk
24 8273 aaronmk
: "${schema="$(cd "$top_dir"/..; basename "$PWD")"}"
25
: "${table="$( cd "$top_dir"   ; basename "$PWD")"}"
26 8195 aaronmk
export schema table
27 8161 aaronmk
28 8273 aaronmk
psql () # usage: ([file=...] [dir=...]; self)
29 8161 aaronmk
{
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 8273 aaronmk
	echo_cmd env no_search_path=1 "$bin_dir"/psql_verbose_vegbien "$@"
46 8161 aaronmk
}
47
48 8273 aaronmk
input_make ()
49 8242 aaronmk
{
50 8273 aaronmk
	echo_cmd env make --directory="$top_dir"/.. "${@/#/$table/}"
51 8242 aaronmk
}
52
53 8161 aaronmk
map_table ()
54
{
55 8273 aaronmk
	input_make map.csv
56 8214 aaronmk
	psql <<EOF
57 8191 aaronmk
SELECT util.reset_map_table('pg_temp.map');
58 8161 aaronmk
\copy pg_temp.map FROM 'map.csv' CSV HEADER;
59 8191 aaronmk
SELECT util.set_col_names('"$table"', 'pg_temp.map'::regclass);
60 8161 aaronmk
EOF
61
}
62 8198 aaronmk
63
mk_derived () { "$root_dir"/schemas/VegCore/mk_derived; }
64 8245 aaronmk
65
remake_VegBIEN_mappings ()
66
{
67 8273 aaronmk
	echo_cmd rm header.csv map.csv # remake them
68
	yes|input_make test
69 8245 aaronmk
}
70 8273 aaronmk
71
postprocess () # overridable
72
{
73
	local file="$top_dir"/postprocess.sql
74
	if test -e "$file"; then psql "$@"; fi
75
}
76
77
import ()
78
{
79
	echo_cmd map_table
80
	echo_cmd postprocess
81
	echo_cmd mk_derived
82
}
83
84
run_cmd "$@"