From 1a045a69931493196ef33586772c0a3a6c1391c8 Mon Sep 17 00:00:00 2001 From: andrewp-CWF Date: Wed, 30 Oct 2024 15:15:15 -0400 Subject: [PATCH 1/6] Add WCRP columns to wcrp_watersheds.csv Column shows which wcrp the watershed belongs to --- data/wcrp_watersheds.csv | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/data/wcrp_watersheds.csv b/data/wcrp_watersheds.csv index abbf5e1f..1077c452 100644 --- a/data/wcrp_watersheds.csv +++ b/data/wcrp_watersheds.csv @@ -1,20 +1,20 @@ -watershed_group_code,ch,cm,co,pk,sk,st,wct,notes -ATNA,t,,t,,t,,, -BELA,t,,t,,t,,, -BONP,t,,t,,t,,, -BOWR,t,,t,,t,,, -BULK,t,,t,,t,t,, -CARR,t,,t,,t,,, -ELKR,,,,,,,t, -HORS,t,,t,,t,,, -LNIC,t,,t,,,t,, -NECL,t,,t,,t,,, -QUES,t,,t,,t,,, -SHUL,t,,t,,t,,, -USHU,t,,t,,t,,, -TAKL,t,,,,t,,, -MIDR,t,,,,t,,, -UTRE,t,,,,t,,, -LTRE,t,,,,t,,, -STUL,t,,,,t,,, -STUR,t,,,,t,,, \ No newline at end of file +watershed_group_code,ch,cm,co,pk,sk,st,wct,notes,wcrp +ATNA,t,,t,,t,,,,bela_atna_necl +BELA,t,,t,,t,,,,bela_atna_necl +BONP,t,,t,,t,,,,tho_shu +BOWR,t,,t,,t,,,,bowr_ques_carr +BULK,t,,t,,t,t,,,bulk +CARR,t,,t,,t,,,,bowr_ques_carr +ELKR,,,,,,,t,,elkr +HORS,t,,t,,t,,,,hors +LNIC,t,,t,,,t,,,lnic +NECL,t,,t,,t,,,,bela_atna_necl +QUES,t,,t,,t,,,,bowr_ques_carr +SHUL,t,,t,,t,,,,tho_shu +USHU,t,,t,,t,,,,tho_shu +TAKL,t,,,,t,,,,tuzistol_tah +MIDR,t,,,,t,,,,tuzistol_tah +UTRE,t,,,,t,,,,tuzistol_tah +LTRE,t,,,,t,,,,tuzistol_tah +STUL,t,,,,t,,,,tuzistol_tah +STUR,t,,,,t,,,,tuzistol_tah From 46ad123107ba714b0bfbd7ecfe46b6094fa23393 Mon Sep 17 00:00:00 2001 From: andrewp-CWF Date: Fri, 1 Nov 2024 11:03:39 -0400 Subject: [PATCH 2/6] Added migration files db.v0.5.3 includes join_tracking_table_crossings_vw.sql and migrate.sh --- data/wcrp_watersheds.csv | 2 +- db/v0.5.3/migrate.sh | 8 ++++++ .../sql/join_tracking_table_crossings_vw.sql | 25 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 db/v0.5.3/migrate.sh create mode 100644 db/v0.5.3/sql/join_tracking_table_crossings_vw.sql diff --git a/data/wcrp_watersheds.csv b/data/wcrp_watersheds.csv index 1077c452..93439a0f 100644 --- a/data/wcrp_watersheds.csv +++ b/data/wcrp_watersheds.csv @@ -17,4 +17,4 @@ MIDR,t,,,,t,,,,tuzistol_tah UTRE,t,,,,t,,,,tuzistol_tah LTRE,t,,,,t,,,,tuzistol_tah STUL,t,,,,t,,,,tuzistol_tah -STUR,t,,,,t,,,,tuzistol_tah +STUR,t,,,,t,,,,tuzistol_tah \ No newline at end of file diff --git a/db/v0.5.3/migrate.sh b/db/v0.5.3/migrate.sh new file mode 100644 index 00000000..a1c92b8f --- /dev/null +++ b/db/v0.5.3/migrate.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -euxo pipefail + +echo "On systems supporting CWF WCRP reporting, add join_tracking_table_crossings_view.sql" +echo "psql $DATABASE_URL -f sql/join_tracking_table_crossings_view.sql" + +# note version +psql $DATABASE_URL -c "update bcfishpass.db_version set tag = '${PWD##*/}'" \ No newline at end of file diff --git a/db/v0.5.3/sql/join_tracking_table_crossings_vw.sql b/db/v0.5.3/sql/join_tracking_table_crossings_vw.sql new file mode 100644 index 00000000..074f2539 --- /dev/null +++ b/db/v0.5.3/sql/join_tracking_table_crossings_vw.sql @@ -0,0 +1,25 @@ + +-- This function joins the tracking table to bcfishpass.crossings_wcrp_vw on the barrier ID +create or replace function wcrp_hors.join_tracking_table_crossings_wcrp_vw(p_wcrp text) + returns void +as +$$ +begin + execute format('create or replace view wcrp_%I.combined_tracking_table_crossings_wcrp_vw_%I as + select + cv.*, + tt.* + from bcfishpass.crossings_wcrp_vw cv + join wcrp_%I.combined_tracking_table_%I tt + on tt.barrier_id = cv.aggregated_crossings_id', p_wcrp, p_wcrp, p_wcrp, p_wcrp); +end +$$ +language plpgsql; + +-- loop through each WCRP and create a view for each one +with data (wcrp) as ( + select wcrp + from bcfishpass.wcrp_watersheds +) +select wcrp_hors.join_tracking_table_crossings_wcrp_vw(wcrp) +from data; \ No newline at end of file From 24bbc640f5e40f7a783e709b0215c6837b07bb1b Mon Sep 17 00:00:00 2001 From: andrewp-CWF Date: Fri, 1 Nov 2024 11:12:20 -0400 Subject: [PATCH 3/6] Add wcrp column to wcrp_watersheds Added column in table creation --- db/v0.5.0/sql/tables/wcrp.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/db/v0.5.0/sql/tables/wcrp.sql b/db/v0.5.0/sql/tables/wcrp.sql index 7c50e1c6..1b56913c 100644 --- a/db/v0.5.0/sql/tables/wcrp.sql +++ b/db/v0.5.0/sql/tables/wcrp.sql @@ -13,7 +13,8 @@ create table bcfishpass.wcrp_watersheds sk boolean, st boolean, wct boolean, - notes text + notes text, + wcrp varchar(32) ); From 97cc8b5598bcdddbf4fdb94f2f4b5a130ff9b9d6 Mon Sep 17 00:00:00 2001 From: andrewp-CWF Date: Mon, 4 Nov 2024 13:31:19 -0500 Subject: [PATCH 4/6] Moved wcrp column creation - Added statement to add the wcrp column to bcfishpass.wcrp_watersheds - removed the change from db/v0.5.0/sql/tables/wcrp.sql --- db/v0.5.0/sql/tables/wcrp.sql | 3 +-- db/v0.5.3/sql/join_tracking_table_crossings_vw.sql | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/db/v0.5.0/sql/tables/wcrp.sql b/db/v0.5.0/sql/tables/wcrp.sql index 1b56913c..7c50e1c6 100644 --- a/db/v0.5.0/sql/tables/wcrp.sql +++ b/db/v0.5.0/sql/tables/wcrp.sql @@ -13,8 +13,7 @@ create table bcfishpass.wcrp_watersheds sk boolean, st boolean, wct boolean, - notes text, - wcrp varchar(32) + notes text ); diff --git a/db/v0.5.3/sql/join_tracking_table_crossings_vw.sql b/db/v0.5.3/sql/join_tracking_table_crossings_vw.sql index 074f2539..c753e923 100644 --- a/db/v0.5.3/sql/join_tracking_table_crossings_vw.sql +++ b/db/v0.5.3/sql/join_tracking_table_crossings_vw.sql @@ -1,3 +1,6 @@ +-- Add the wcrp column to the wcrp_watersheds table +alter table bcfishpass.wcrp_watersheds +add column wcrp varchar(32); -- This function joins the tracking table to bcfishpass.crossings_wcrp_vw on the barrier ID create or replace function wcrp_hors.join_tracking_table_crossings_wcrp_vw(p_wcrp text) From 94aae1ebc61a1db856f1bb52a52a1cc83b234191 Mon Sep 17 00:00:00 2001 From: Simon Norris Date: Mon, 4 Nov 2024 11:02:26 -0800 Subject: [PATCH 5/6] run add column wcrp to wcrp_watersheds on all systems --- db/v0.5.3/migrate.sh | 8 ++++++-- db/v0.5.3/sql/join_tracking_table_crossings_vw.sql | 4 ---- 2 files changed, 6 insertions(+), 6 deletions(-) mode change 100644 => 100755 db/v0.5.3/migrate.sh diff --git a/db/v0.5.3/migrate.sh b/db/v0.5.3/migrate.sh old mode 100644 new mode 100755 index a1c92b8f..25d93de0 --- a/db/v0.5.3/migrate.sh +++ b/db/v0.5.3/migrate.sh @@ -1,8 +1,12 @@ #!/bin/bash set -euxo pipefail +# add wcrp name column to wcrp table +psql $DATABASE_URL -v ON_ERROR_STOP=1 -c "alter table bcfishpass.wcrp_watersheds add column wcrp varchar(32);" + +# add wcrp tracking views echo "On systems supporting CWF WCRP reporting, add join_tracking_table_crossings_view.sql" -echo "psql $DATABASE_URL -f sql/join_tracking_table_crossings_view.sql" +echo "psql $DATABASE_URL -v ON_ERROR_STOP=1 -f sql/join_tracking_table_crossings_view.sql" # note version -psql $DATABASE_URL -c "update bcfishpass.db_version set tag = '${PWD##*/}'" \ No newline at end of file +psql $DATABASE_URL -v ON_ERROR_STOP=1 -c "update bcfishpass.db_version set tag = '${PWD##*/}'" \ No newline at end of file diff --git a/db/v0.5.3/sql/join_tracking_table_crossings_vw.sql b/db/v0.5.3/sql/join_tracking_table_crossings_vw.sql index c753e923..f5ed09ef 100644 --- a/db/v0.5.3/sql/join_tracking_table_crossings_vw.sql +++ b/db/v0.5.3/sql/join_tracking_table_crossings_vw.sql @@ -1,7 +1,3 @@ --- Add the wcrp column to the wcrp_watersheds table -alter table bcfishpass.wcrp_watersheds -add column wcrp varchar(32); - -- This function joins the tracking table to bcfishpass.crossings_wcrp_vw on the barrier ID create or replace function wcrp_hors.join_tracking_table_crossings_wcrp_vw(p_wcrp text) returns void From 269a31a2ebaa22409db43375db4a1b6aa2f058f1 Mon Sep 17 00:00:00 2001 From: andrewp-CWF Date: Fri, 8 Nov 2024 10:27:40 -0500 Subject: [PATCH 6/6] Update join_tracking_table_crossings_vw.sql - Added right join so excluded structures with no feature in crossings_wcrp_vw show up in the joined table. --- .../sql/join_tracking_table_crossings_vw.sql | 111 +++++++++++++++++- 1 file changed, 108 insertions(+), 3 deletions(-) diff --git a/db/v0.5.3/sql/join_tracking_table_crossings_vw.sql b/db/v0.5.3/sql/join_tracking_table_crossings_vw.sql index f5ed09ef..02b454c1 100644 --- a/db/v0.5.3/sql/join_tracking_table_crossings_vw.sql +++ b/db/v0.5.3/sql/join_tracking_table_crossings_vw.sql @@ -6,10 +6,115 @@ $$ begin execute format('create or replace view wcrp_%I.combined_tracking_table_crossings_wcrp_vw_%I as select - cv.*, - tt.* + tt.barrier_id, + cv.crossing_source, + cv.crossing_feature_type, + cv.pscis_status, + cv.crossing_type_code, + cv.crossing_subtype_code, + cv.barrier_status, + cv.pscis_road_name, + cv.pscis_stream_name, + cv.pscis_assessment_comment, + cv.pscis_assessment_date, + cv.transport_line_structured_name_1, + cv.rail_track_name, + cv.dam_name, + cv.dam_height, + cv.dam_owner, + cv.dam_use, + cv.dam_operating_status, + cv.utm_zone, + cv.utm_easting, + cv.utm_northing, + cv.blue_line_key, + cv.downstream_route_measure, + cv.wscode, + cv.localcode, + cv.watershed_group_code, + cv.gnis_stream_name, + cv.barriers_anthropogenic_dnstr, + cv.barriers_anthropogenic_dnstr_count, + cv.barriers_anthropogenic_habitat_wcrp_upstr + cv.barriers_antrhopogenic_habitat_wcrp_upstr_count, + cv.barriers_ch_cm_co_pk_sk_dnstr, + cv.barriers_st_dnstr, + cv.barriers_wct_dnstr, + cv.ch_spawning_km, + cv.ch_rearing_km, + cv.ch_spawning_belowupstrbarriers_km, + cv.ch_rearing_belowupstrbarriers_km, + cv.cm_spawning_km, + cv.cm_spawning_belowupstrbarriers_km, + cv.co_spawning_km, + cv.co_rearing_km, + cv.co_rearing_ha, + cv.co_spawning_belowupstrbarriers_km, + cv.co_rearing_belowupstrbarriers_km, + cv.co_rearing_belowupstrbarriers_ha, + cv.pk_spawning_km, + cv.pk_spawning_belowupstrbarriers_km, + cv.sk_spawning_km, + cv.sk_rearing_km, + cv.sk_rearing_ha, + cv.sk_spawning_belowupstrbarriers_km, + cv.sk_rearing_belowupstrbarriers_km, + cv.sk_rearing_belowupstrbarriers_ha, + cv.st_spawning_km, + cv.st_rearing_km, + cv.st_spawning_belowupstrbarriers_km, + cv.st_rearing_belowupstrbarriers_km, + cv.wct_spawning_km, + cv.wct_rearing_km, + cv.wct_spawning_belowupstrbarriers_km, + cv.wct_rearing_belowupstrbarriers_km, + cv.all_spawning_km, + cv.all_spawning_belowupstrbarriers_km, + cv.all_rearing_km, + cv.all_rearing_belowupstrbarriers_km, + cv.all_spawningrearing_km, + cv.all_spawningrearing_belowupstrbarriers_km, + cv.set_id, + cv.total_hab_gain_set, + cv.num_barriers_set, + cv.avg_gain_per_barrier, + cv.dnstr_set_ids, + cv.rank_avg_gain_per_barrier, + cv.rank_avg_gain_tiered, + cv.rank_total_upstr_hab, + cv.rank_combined, + cv.tier_combined, + cv.geom, + tt.internal_name, + tt.watercourse_name, + tt.road_name, + tt.structure_type, + tt.structure_owner, + tt.private_owner_details, + tt.structure_list_status, + tt.assessment_type_completed, + tt.reason_for_exclusion, + tt.method_of_exclusion, + tt.partial_passability, + tt.partial_passability_notes, + tt.upstream_habitat_quality, + tt.constructability, + tt.estimated_cost_$, + tt.priority, + tt.type_of_rehabilitation, + tt.rehabilitated_by, + tt.rehabilitated_date, + tt.estimated_rehabilitation_cost_$, + tt.actual_project_cost_$, + tt.next_steps, + tt.timeline_for_next_steps, + tt.lead_for_next_steps, + tt.others_involved_in_next_steps, + tt.reason, + tt.notes, + tt.supporting_links from bcfishpass.crossings_wcrp_vw cv - join wcrp_%I.combined_tracking_table_%I tt + right join wcrp_%I.combined_tracking_table_%I tt on tt.barrier_id = cv.aggregated_crossings_id', p_wcrp, p_wcrp, p_wcrp, p_wcrp); end $$