update_org_segments

/*
This macro is to used as a pre-hook only for WC_ORG_D
The purpose is to update the segment information.

To check whether a source table exist, see below
https://discourse.getdbt.com/t/writing-packages-when-a-source-table-may-or-may-not-exist/1487

For update statement in DBT, see below
https://stackoverflow.com/questions/67870208/how-to-use-update-statement-in-dbt
*/


{% macro update_org_segments() %}

{%- set source_relation = adapter.get_relation(
      database=source('edw_transient', 'WC_ORG_SEGMENTS').database,
      schema=source('edw_transient', 'WC_ORG_SEGMENTS').schema,
      identifier=source('edw_transient', 'WC_ORG_SEGMENTS').name) -%}
{% set table_exists = source_relation is not none %}

      {% if table_exists %}

            {% set query %}
                update {{ source('edw_modeled','WC_ORG_D') }} as WC_ORG_D
                set WC_ORG_D.PARENT_BROKER_COUNT = WC_ORG_SEGMENTS.PARENT_BROKER_COUNT,
                 WC_ORG_D.PARENT_SETTLE_VOL_PAST_YR = WC_ORG_SEGMENTS.PARENT_SETTLE_VOL_PAST_YR,
                 WC_ORG_D.PARENT_SEGMENT = WC_ORG_SEGMENTS.PARENT_SEGMENT,
                 WC_ORG_D.PARENT_MICRO_SEGMENT = WC_ORG_SEGMENTS.PARENT_MICRO_SEGMENT,
                 WC_ORG_D.PARENT_GROWTH_3FY_AVG = WC_ORG_SEGMENTS.PARENT_GROWTH_3FY_AVG,
                 WC_ORG_D.CATEGORY_PM = WC_ORG_SEGMENTS.CATEGORY_PM,
                 WC_ORG_D.CATEGORY_BRM = WC_ORG_SEGMENTS.CATEGORY_BRM,
                 WC_ORG_D.CATEGORY_CBDM = WC_ORG_SEGMENTS.CATEGORY_CBDM
                from {{ source('edw_transient','WC_ORG_SEGMENTS') }} as WC_ORG_SEGMENTS
                where WC_ORG_D.ROW_WID = WC_ORG_SEGMENTS.ROW_WID
            {% endset %}

            {% do run_query(query) %}

      {% endif %}

{% endmacro %}