compare_new_and_old

{% macro compare_new_and_old(new_table, old_table, join_keys, exceptions) %}


{% set all_columns = adapter.get_columns_in_relation(
    new_table
) %}

select
    --new.row_wid new_id,
    --new.integration_id new_int_Id,
    --old.row_wid old_id,
    --old.integration_Id old_int_id,
    {%- for key in join_keys %}  
        new.{{key}}
    {%- endfor %},
    {%- for col in all_columns if col.name not in exceptions %}  
        {%- if loop.first %}{{ "case " }}{% endif %}
            when new.{{col.name}} != old.{{col.name}} then '{{col.name}}'
        {%- if loop.last %}{{ " end as mismatch_col" }}{% endif %}
    {%- endfor %},

    {%- for col in all_columns if col.name not in exceptions %}  
        {%- if loop.first %}{{ "case " }}{% endif %}
            when new.{{col.name}} != old.{{col.name}} then to_char(new.{{col.name}})
        {%- if loop.last %}{{ " end as new_mismatch_val" }}{% endif %}
    {%- endfor %},
   
    {%- for col in all_columns if col.name not in exceptions %}  
        {%- if loop.first %}{{ "case " }}{% endif %}
            when new.{{col.name}} != old.{{col.name}} then to_char(old.{{col.name}})
        {%- if loop.last %}{{ " end as old_mismatch_val" }}{% endif %}
    {%- endfor %}
from  {{ new_table }} new
full outer join {{ old_table }} old  on
{%- for key in join_keys %}  
    new.{{key}} = old.{{key}} {%- if not loop.last %}{{ "\n and" }} {% endif %}
{%- endfor %}
where
{%- for col in all_columns if col.name not in exceptions %}  
    {%- if not loop.first %}{{ "\n or" }}{% endif %} new.{{col.name}} != old.{{col.name}}
{%- endfor %}
{% endmacro %}