From d189acbf49a3ef7cc4a987d5453d6c5e80a18f40 Mon Sep 17 00:00:00 2001 From: Mike Alfare <13974384+mikealfare@users.noreply.github.com> Date: Fri, 6 Sep 2024 17:22:49 -0400 Subject: [PATCH] Add cascade to drop mv (#904) * add drop cascade for materialized views --- .../unreleased/Fixes-20240906-102642.yaml | 7 +++ .../relations/materialized_view/drop.sql | 2 +- tests/functional/__init__.py | 1 + tests/functional/adapter/__init__.py | 1 + .../materialized_view_tests/__init__.py | 1 + .../test_drop_cascade.py | 56 +++++++++++++++++++ 6 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 .changes/unreleased/Fixes-20240906-102642.yaml create mode 100644 tests/functional/__init__.py create mode 100644 tests/functional/adapter/__init__.py create mode 100644 tests/functional/adapter/materialized_view_tests/__init__.py create mode 100644 tests/functional/adapter/materialized_view_tests/test_drop_cascade.py diff --git a/.changes/unreleased/Fixes-20240906-102642.yaml b/.changes/unreleased/Fixes-20240906-102642.yaml new file mode 100644 index 000000000..622f908e7 --- /dev/null +++ b/.changes/unreleased/Fixes-20240906-102642.yaml @@ -0,0 +1,7 @@ +kind: Fixes +body: Support DROP CASCADE for materialized views; fixes bug that occurs when running + dbt on materialized views that reference other materialized views +time: 2024-09-06T10:26:42.501014-04:00 +custom: + Author: mikealfare + Issue: "642" diff --git a/dbt/include/redshift/macros/relations/materialized_view/drop.sql b/dbt/include/redshift/macros/relations/materialized_view/drop.sql index 76f92e0e9..0db283817 100644 --- a/dbt/include/redshift/macros/relations/materialized_view/drop.sql +++ b/dbt/include/redshift/macros/relations/materialized_view/drop.sql @@ -1,3 +1,3 @@ {% macro redshift__drop_materialized_view(relation) -%} - drop materialized view if exists {{ relation }} + drop materialized view if exists {{ relation }} cascade {%- endmacro %} diff --git a/tests/functional/__init__.py b/tests/functional/__init__.py new file mode 100644 index 000000000..30e204d08 --- /dev/null +++ b/tests/functional/__init__.py @@ -0,0 +1 @@ +# provides namespacing for test discovery diff --git a/tests/functional/adapter/__init__.py b/tests/functional/adapter/__init__.py new file mode 100644 index 000000000..30e204d08 --- /dev/null +++ b/tests/functional/adapter/__init__.py @@ -0,0 +1 @@ +# provides namespacing for test discovery diff --git a/tests/functional/adapter/materialized_view_tests/__init__.py b/tests/functional/adapter/materialized_view_tests/__init__.py new file mode 100644 index 000000000..30e204d08 --- /dev/null +++ b/tests/functional/adapter/materialized_view_tests/__init__.py @@ -0,0 +1 @@ +# provides namespacing for test discovery diff --git a/tests/functional/adapter/materialized_view_tests/test_drop_cascade.py b/tests/functional/adapter/materialized_view_tests/test_drop_cascade.py new file mode 100644 index 000000000..3d226a01e --- /dev/null +++ b/tests/functional/adapter/materialized_view_tests/test_drop_cascade.py @@ -0,0 +1,56 @@ +""" +This test addresses this bug: https://github.com/dbt-labs/dbt-redshift/issues/642 + +Redshift did not initially support DROP CASCADE for materialized views, +or at least did not document that they did. Now that they do, we should +use DROP CASCADE instead of DROP. +""" + +from dbt.tests.util import run_dbt +import pytest + + +SEED = """ +id +1 +""".strip() + + +PARENT_MATERIALIZED_VIEW = """ +{{ config( + materialized='materialized_view', + on_configuration_change='apply', +) }} + +select * from {{ ref('my_seed') }} +""" + + +CHILD_MATERIALIZED_VIEW = """ +{{ config( + materialized='materialized_view', + on_configuration_change='apply', +) }} + +select * from {{ ref('parent_mv') }} +""" + + +@pytest.fixture(scope="class") +def seeds(): + return {"my_seed.csv": SEED} + + +@pytest.fixture(scope="class") +def models(): + return { + "parent_mv.sql": PARENT_MATERIALIZED_VIEW, + "child_mv.sql": CHILD_MATERIALIZED_VIEW, + } + + +def test_drop_cascade(project): + run_dbt(["seed"]) + run_dbt(["run"]) + # this originally raised an error when it should not have + run_dbt(["run", "--full-refresh"])