Skip to content

Instantly share code, notes, and snippets.

@jtcohen6
Last active April 23, 2023 20:43
Show Gist options
  • Save jtcohen6/68220cd76b0bde088d3439664ccfb013 to your computer and use it in GitHub Desktop.
Save jtcohen6/68220cd76b0bde088d3439664ccfb013 to your computer and use it in GitHub Desktop.
on-run-end hook (or post-hook) to create "redirects" for latest_version of versioned models
{% macro create_latest_version_views() %}
{% for res in results %}
{% set node = res.node %}
{% if node.is_versioned and node.version == node.latest_version %}
{% set versioned_relation = api.Relation.create(
database = node.database,
schema = node.schema,
identifier = node.identifier
) %}
{% set new_relation = api.Relation.create(
database = node.database,
schema = node.schema,
identifier = node.name
) %}
{#-- future: this could be a "pointer view" OR a "table clone" depending on the DWH + relation type --#}
{#-- inspiration: https://github.com/dbt-labs/dbt-core/pull/7258/files#diff-073e6ed96ac92033f0b921e061b47226b87d4f358350a1ed94fc94165f247b7aR16-R36 --#}
{% set create_view_sql -%}
create or replace view {{ new_relation }} as select * from {{ versioned_relation }}
{%- endset %}
{{ log(create_view_sql, info = true) }}
{% do run_query(create_view_sql) %}
{% endif %}
{% endfor %}
{% endmacro %}
name: my_dbt_project
on-run-end:
- "{{ create_latest_version_views() }}"
@jtcohen6
Copy link
Author

Alternative would be a post-hook version:

{% macro create_latest_version_view() %}

    {% if model.get('version') and model.get('version') == model.get('latest_version') %}

        {% set new_relation = api.Relation.create(
            database = this.database,
            schema = this.schema,
            identifier = model['name']
        ) %}
      
        {#-- future: this could be a "pointer view" OR a "table clone" depending on the DWH + relation type --#}
        {#-- inspiration: https://github.com/dbt-labs/dbt-core/pull/7258/files#diff-073e6ed96ac92033f0b921e061b47226b87d4f358350a1ed94fc94165f247b7aR16-R36 --#}
        
        {% set create_view_sql -%}
            create or replace view {{ new_relation }} as select * from {{ this }}
        {%- endset %}
        
        {{ log(create_view_sql, info = true) }}
        
        {% do run_query(create_view_sql) %}
        
    {% endif %}

{% endmacro %}
# dbt_project.yml
models:
  post-hook: "{{ create_latest_version_view() }}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment