{% macro apply_snapshot_backfill(snapshot_ref, backfill_ref, primary_key) %} {% if execute %} {% if relation_exists(snapshot_ref) and relation_exists(backfill_ref)%} INSERT INTO {{ snapshot_ref }} ( {{ column_intersect(snapshot_ref, backfill_ref,exclude=["dbt_scd_id", "dbt_updated_at", "dbt_valid_from", "dbt_valid_to"], quote_identifiers=false)| list | join(",") }} , dbt_scd_id, dbt_updated_at, dbt_valid_from, dbt_valid_to) SELECT {{ column_intersect(snapshot_ref, backfill_ref,exclude=["dbt_scd_id", "dbt_updated_at", "dbt_valid_from", "dbt_valid_to"])| list | join(",") }} , b.dbt_scd_id, b.dbt_updated_at, b.dbt_valid_from, --doesnt handle deleted rows... least(b.dbt_valid_to, min_from) as dbt_valid_to FROM {{ backfill_ref }} b LEFT JOIN ( SELECT MIN(dbt_valid_from) as min_from, {{ primary_key }} FROM {{ snapshot_ref }} GROUP BY {{ primary_key }} ) c USING ({{ primary_key }}) WHERE b.dbt_valid_from < coalesce(min_from, current_timestamp); -- only insert earlier backfill rows OR any backfill entities that no longer exist. {% endif %} {% endif %} {% endmacro %}