Skip to content

Commit

Permalink
Add cascade to drop mv (#904)
Browse files Browse the repository at this point in the history
* add drop cascade for materialized views
  • Loading branch information
mikealfare authored Sep 6, 2024
1 parent 8bbdbf2 commit d189acb
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20240906-102642.yaml
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% macro redshift__drop_materialized_view(relation) -%}
drop materialized view if exists {{ relation }}
drop materialized view if exists {{ relation }} cascade
{%- endmacro %}
1 change: 1 addition & 0 deletions tests/functional/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# provides namespacing for test discovery
1 change: 1 addition & 0 deletions tests/functional/adapter/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# provides namespacing for test discovery
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# provides namespacing for test discovery
Original file line number Diff line number Diff line change
@@ -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"])

0 comments on commit d189acb

Please sign in to comment.