Last active
April 23, 2023 20:43
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: my_dbt_project | |
on-run-end: | |
- "{{ create_latest_version_views() }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alternative would be a
post-hook
version: