Project

General

Profile

1
#!/bin/bash
2
# Creates analytical DB
3
# Usage: self [log=]
4

    
5
selfDir="$(dirname -- "$0")"
6

    
7
. "$selfDir/vegbien_dest"
8

    
9
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

    
13
(set -x; date)
14

    
15
sleep 5 # wait for tnrs.make to acquire the lock if being run right after import
16
"$selfDir/waitself" "$selfDir/../inputs/.TNRS/tnrs/tnrs.make"
17

    
18
mk_analytical_table ()
19
{
20
    local table="$1"
21
    local public_table="\"$public\".$table" analyt_table="analytical_db.$table"
22
    cat <<EOF
23
-- Create $table
24
TRUNCATE $public_table;
25
INSERT INTO $public_table SELECT * FROM ${public_table}_view;
26
ANALYZE $public_table;
27

    
28
-- 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
EOF
34
}
35

    
36
((set -x; "$selfDir/psql_verbose_vegbien") <<EOF
37
GRANT USAGE ON SCHEMA analytical_db TO bien_read;
38
GRANT USAGE ON SCHEMA analytical_db TO public_;
39

    
40
TRUNCATE geoscrub_input;
41
INSERT INTO geoscrub_input SELECT * FROM geoscrub_input_view;
42

    
43
SELECT make_family_higher_plant_group();
44
TRUNCATE threatened_taxonlabel;
45
INSERT INTO threatened_taxonlabel SELECT * FROM threatened_taxonlabel_view;
46

    
47
$(mk_analytical_table taxon_trait)
48
$(mk_analytical_table analytical_stem)
49
$(mk_analytical_table provider_count)
50
GRANT SELECT ON TABLE analytical_db.provider_count TO public_;
51
EOF
52
) || exit
53

    
54
#"$selfDir/export_analytical_db"
(39-39/81)