Project

General

Profile

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