Revision 8273
Added by Aaron Marcuse-Kubitza almost 12 years ago
lib/table.run | ||
---|---|---|
1 |
#!/bin/bash |
|
2 |
set -o errexit |
|
3 |
|
|
4 |
if false; then #### run script template: |
|
5 |
#!/bin/bash |
|
6 |
set -o errexit |
|
7 |
. "$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run |
|
8 |
|
|
9 |
import () |
|
10 |
{ |
|
11 |
before_import_cmds |
|
12 |
echo_cmd "$(dirname "${BASH_SOURCE[0]}")"/path/to/table.run import |
|
13 |
after_import_cmds |
|
14 |
} |
|
15 |
|
|
16 |
run_cmd "$@" |
|
17 |
fi #### |
|
18 |
|
|
19 |
. "$(dirname "${BASH_SOURCE[0]}")"/util.run |
|
20 |
|
|
21 |
root_dir="$(dirname "${BASH_SOURCE[0]}")"/.. |
|
22 |
bin_dir="$root_dir"/bin |
|
23 |
|
|
24 |
: "${schema="$(cd "$top_dir"/..; basename "$PWD")"}" |
|
25 |
: "${table="$( cd "$top_dir" ; basename "$PWD")"}" |
|
26 |
export schema table |
|
27 |
|
|
28 |
psql () # usage: ([file=...] [dir=...]; self) |
|
29 |
{ |
|
30 |
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 |
(cat <<EOF |
|
38 |
\cd $dir |
|
39 |
\set schema "$schema" |
|
40 |
\set table "$table" |
|
41 |
\set table_str '''"$table"''' |
|
42 |
SET search_path TO "$schema", util; |
|
43 |
EOF |
|
44 |
cat)| |
|
45 |
echo_cmd env no_search_path=1 "$bin_dir"/psql_verbose_vegbien "$@" |
|
46 |
} |
|
47 |
|
|
48 |
input_make () |
|
49 |
{ |
|
50 |
echo_cmd env make --directory="$top_dir"/.. "${@/#/$table/}" |
|
51 |
} |
|
52 |
|
|
53 |
map_table () |
|
54 |
{ |
|
55 |
input_make map.csv |
|
56 |
psql <<EOF |
|
57 |
SELECT util.reset_map_table('pg_temp.map'); |
|
58 |
\copy pg_temp.map FROM 'map.csv' CSV HEADER; |
|
59 |
SELECT util.set_col_names('"$table"', 'pg_temp.map'::regclass); |
|
60 |
EOF |
|
61 |
} |
|
62 |
|
|
63 |
mk_derived () { "$root_dir"/schemas/VegCore/mk_derived; } |
|
64 |
|
|
65 |
remake_VegBIEN_mappings () |
|
66 |
{ |
|
67 |
echo_cmd rm header.csv map.csv # remake them |
|
68 |
yes|input_make test |
|
69 |
} |
|
70 |
|
|
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 "$@" |
|
0 | 85 |
Also available in: Unified diff
Added lib/table.run, which includes the commands in import.sh but uses run scripts to allow running commands other than just import. (For example, map_table or postprocess can be run separately. Uninstall-related commands which would not belong in an import script can also be added, because import is only one of many commands a run script can offer.)