Project

General

Profile

1 6461 aaronmk
#!/bin/bash
2 4395 aaronmk
# Creates analytical DB
3 7864 aaronmk
# Usage: self [log=]
4 4395 aaronmk
5
selfDir="$(dirname -- "$0")"
6
7 6459 aaronmk
. "$selfDir/vegbien_dest"
8
9 7864 aaronmk
if test -n "${log=1}"; then # $log defaults to 1
10
    exec >>"$selfDir/../inputs/analytical_db/logs/make_analytical_db.log.sql" 2>&1
11
fi
12 6047 aaronmk
13 6054 aaronmk
(set -x; date)
14 6048 aaronmk
15 7620 aaronmk
sleep 5 # wait for tnrs.make to acquire the lock if being run right after import
16 7618 aaronmk
"$selfDir/waitself" "$selfDir/../inputs/.TNRS/tnrs/tnrs.make"
17
18 6460 aaronmk
mk_analytical_table ()
19
{
20
    local table="$1"
21 6465 aaronmk
    local public_table="\"$public\".$table" analyt_table="analytical_db.$table"
22 6460 aaronmk
    cat <<EOF
23
-- Create $table
24 6466 aaronmk
TRUNCATE $public_table;
25
INSERT INTO $public_table SELECT * FROM ${public_table}_view;
26 7061 aaronmk
ANALYZE $public_table;
27 6460 aaronmk
28 6465 aaronmk
-- Create $analyt_table view pointing to table in $public
29
DROP VIEW IF EXISTS $analyt_table;
30
CREATE VIEW $analyt_table AS
31
SELECT * FROM $public_table;
32
GRANT SELECT ON TABLE $analyt_table TO bien_read;
33 6460 aaronmk
EOF
34
}
35
36 7345 aaronmk
((set -x; "$selfDir/psql_verbose_vegbien") <<EOF
37 7031 aaronmk
GRANT USAGE ON SCHEMA analytical_db TO bien_read;
38
GRANT USAGE ON SCHEMA analytical_db TO public_;
39
40 6053 aaronmk
TRUNCATE geoscrub_input;
41
INSERT INTO geoscrub_input SELECT * FROM geoscrub_input_view;
42
43 6042 aaronmk
SELECT make_family_higher_plant_group();
44 6085 aaronmk
TRUNCATE threatened_taxonlabel;
45
INSERT INTO threatened_taxonlabel SELECT * FROM threatened_taxonlabel_view;
46 6042 aaronmk
47 6895 aaronmk
$(mk_analytical_table taxon_trait)
48 6460 aaronmk
$(mk_analytical_table analytical_stem)
49 6868 aaronmk
$(mk_analytical_table provider_count)
50 7043 aaronmk
GRANT SELECT ON TABLE analytical_db.provider_count TO public_;
51 6042 aaronmk
EOF
52 7345 aaronmk
) || exit
53 6981 aaronmk
54 8320 aaronmk
#"$selfDir/export_analytical_db"