From 47e7b1cc80266e1a484434c2dccae8e3c058958e Mon Sep 17 00:00:00 2001 From: Mike Alfare <13974384+mikealfare@users.noreply.github.com> Date: Mon, 15 May 2023 15:51:19 -0400 Subject: [PATCH] Feature/drop relation/ct 2581 (#7626) * changie * move drop_relation macros into their own file, add scenarios for table, view, and materialized view --- .../unreleased/Features-20230515-122304.yaml | 7 +++ .../macros/adapters/drop_relation.sql | 44 +++++++++++++++++++ .../macros/adapters/relation.sql | 10 ----- 3 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 .changes/unreleased/Features-20230515-122304.yaml create mode 100644 core/dbt/include/global_project/macros/adapters/drop_relation.sql diff --git a/.changes/unreleased/Features-20230515-122304.yaml b/.changes/unreleased/Features-20230515-122304.yaml new file mode 100644 index 00000000000..d423e564551 --- /dev/null +++ b/.changes/unreleased/Features-20230515-122304.yaml @@ -0,0 +1,7 @@ +kind: Features +body: Update drop_relation macro to allow for configuration of drop statement separately + from object name +time: 2023-05-15T12:23:04.177141-04:00 +custom: + Author: mikealfare + Issue: "7625" diff --git a/core/dbt/include/global_project/macros/adapters/drop_relation.sql b/core/dbt/include/global_project/macros/adapters/drop_relation.sql new file mode 100644 index 00000000000..bd254c78d51 --- /dev/null +++ b/core/dbt/include/global_project/macros/adapters/drop_relation.sql @@ -0,0 +1,44 @@ +{% macro drop_relation(relation) -%} + {{ return(adapter.dispatch('drop_relation', 'dbt')(relation)) }} +{% endmacro %} + +{% 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 %} + + +{% macro drop_table(relation) -%} + {{ return(adapter.dispatch('drop_table', 'dbt')(relation)) }} +{%- endmacro %} + +{% macro default__drop_table(relation) -%} + drop table if exists {{ relation }} cascade +{%- endmacro %} + + +{% macro drop_view(relation) -%} + {{ return(adapter.dispatch('drop_view', 'dbt')(relation)) }} +{%- endmacro %} + +{% macro default__drop_view(relation) -%} + drop view if exists {{ relation }} cascade +{%- endmacro %} + + +{% macro drop_materialized_view(relation) -%} + {{ return(adapter.dispatch('drop_materialized_view', 'dbt')(relation)) }} +{%- endmacro %} + +{% macro default__drop_materialized_view(relation) -%} + drop materialized view if exists {{ relation }} cascade +{%- endmacro %} diff --git a/core/dbt/include/global_project/macros/adapters/relation.sql b/core/dbt/include/global_project/macros/adapters/relation.sql index d02e3e20a26..f0dde7f20f0 100644 --- a/core/dbt/include/global_project/macros/adapters/relation.sql +++ b/core/dbt/include/global_project/macros/adapters/relation.sql @@ -31,16 +31,6 @@ {{ return(backup_relation) }} {% endmacro %} -{% macro drop_relation(relation) -%} - {{ return(adapter.dispatch('drop_relation', 'dbt')(relation)) }} -{% endmacro %} - -{% macro default__drop_relation(relation) -%} - {% call statement('drop_relation', auto_begin=False) -%} - drop {{ relation.type }} if exists {{ relation }} cascade - {%- endcall %} -{% endmacro %} - {% macro truncate_relation(relation) -%} {{ return(adapter.dispatch('truncate_relation', 'dbt')(relation)) }}