/*
This macro is to used as a pre-hook only for WC_ORG_D
The purpose is to update the children string.
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_children_string() %}
{%- set source_relation = adapter.get_relation(
database=source('edw_transient', 'WC_ORG_POSTLOAD_CHILD_STR').database,
schema=source('edw_transient', 'WC_ORG_POSTLOAD_CHILD_STR').schema,
identifier=source('edw_transient', 'WC_ORG_POSTLOAD_CHILD_STR').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.CHILDREN_STRING = WC_ORG_CHILD_STR.CHILD_STRING
from {{ source('edw_transient','WC_ORG_POSTLOAD_CHILD_STR') }} as WC_ORG_CHILD_STR
where WC_ORG_D.INTEGRATION_ID = WC_ORG_CHILD_STR.TRUE_PARENT_ID
{% endset %}
{% do run_query(query) %}
{% endif %}
{% endmacro %}