Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new paradigm for creating replacing and dropping relations #473

Closed
dataders opened this issue Oct 5, 2023 · 2 comments
Closed

new paradigm for creating replacing and dropping relations #473

dataders opened this issue Oct 5, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@dataders
Copy link
Contributor

dataders commented Oct 5, 2023

What

Restructure macros so that operations so that within a materialization, macros need not get more relation-specific.

This means there's now a catch all macro that can be used regardless of the underlying relation type

examples

drop_relation()

{% macro default__drop_relation(relation) -%}
    {% call statement('drop_relation', auto_begin=False) -%}
        {%- if relation.is_table -%}
            {{- drop_table(relation) -}}
        {%- elif relation.is_view -%}
            {{- drop_view(relation) -}}
        {%- elif relation.is_materialized_view -%}
            {{- drop_materialized_view(relation) -}}
        {%- else -%}
            drop {{ relation.type }} if exists {{ relation }} cascade
        {%- endif -%}
    {%- endcall %}
{% endmacro %}

get_create_intermediate_sql()

{%- macro default__get_create_intermediate_sql(relation, sql) -%}

    -- get the standard intermediate name
    {% set intermediate_relation = make_intermediate_relation(relation) %}

    -- drop any pre-existing intermediate
    {{ get_drop_sql(intermediate_relation) }};

    {{ get_create_sql(intermediate_relation, sql) }}

{%- endmacro -%}

Why

Before MV/DTs, when materializing a dbt model as a table or view, dbt would first check if the target name already existed, and drop it if so.

For example, when creating a table with the target three-part name foo.bar.baz, dbt will check to see if that name is being already used by a view.

Furthermore, dbt’s out-of-the-box materializations will create backup and temp objects so if dbt run fails, the user is back to where they started, instead of having dropped all database objects were in dbt’s way.

Now, dbt must also check that the current model and corresponding backup and temp relations won’t collide with any existing materialized views.

The two-adapter related features here are

  • checking if a relation exists, and if so, what type it is
  • dropping a relation

Relevant PRs

@benc-db if this strikes you as a lot of moving pieces and I haven't yet explained everything, you'd be right! Please let me know how I can help. Thanks for agreeing to being our guinea pig

@benc-db
Copy link
Collaborator

benc-db commented Nov 10, 2023

@dataders can you schedule a meeting with me and either you or someone else from your team to go over the details. I did what was called out in the migration guide for 1.7.0, but I suspect this work was pushed out along with MV/ST and want to make sure I have all the context before I pick it back up.

@benc-db
Copy link
Collaborator

benc-db commented Jan 12, 2024

Going to close this issue as we've got an implementation for MV in review.

@benc-db benc-db closed this as completed Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants