Skip to content

Commit 176f183

Browse files
Alter/Rename Dynamic Tables on full refresh (#1203)
1 parent d7d5e58 commit 176f183

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Fixes
2+
body: Rename DT temp tables to avoid problematic drops
3+
time: 2025-07-16T09:55:18.719362-07:00
4+
custom:
5+
Author: colin-rogers-dbt
6+
Issue: "713"

dbt-snowflake/src/dbt/adapters/snowflake/relation.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class SnowflakeRelation(BaseRelation):
4242
{
4343
SnowflakeRelationType.Table, # type: ignore
4444
SnowflakeRelationType.View, # type: ignore
45+
SnowflakeRelationType.DynamicTable, # type: ignore
4546
}
4647
)
4748
)
@@ -60,6 +61,10 @@ class SnowflakeRelation(BaseRelation):
6061
def is_dynamic_table(self) -> bool:
6162
return self.type == SnowflakeRelationType.DynamicTable
6263

64+
@property
65+
def is_materialized_view(self) -> bool:
66+
return self.type == SnowflakeRelationType.DynamicTable
67+
6368
@property
6469
def is_iceberg_format(self) -> bool:
6570
return self.table_format == constants.ICEBERG_TABLE_FORMAT
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{%- macro snowflake__get_rename_materialized_view_sql(relation, new_name) -%}
2+
/*
3+
Rename or move a dynamic table to the new name.
4+
5+
Args:
6+
relation: SnowflakeRelation - dynamic table relation to be renamed
7+
new_name: Union[str, SnowflakeRelation] - new name for `relation`
8+
if providing a string, the default database/schema will be used if that string is just an identifier
9+
if providing a SnowflakeRelation, `render` will be used to produce a fully qualified name
10+
Returns: templated string
11+
*/
12+
alter dynamic table {{ relation }} rename to {{ new_name }}
13+
{%- endmacro -%}

dbt-snowflake/tests/functional/relation_tests/dynamic_table_tests/models.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,30 @@
110110
) }}
111111
select * from {{ ref('my_seed') }}
112112
"""
113+
114+
DYNAMIC_TABLE_CUSTOM_SCHEMA = """
115+
{{ config(
116+
materialized='dynamic_table',
117+
snowflake_warehouse='DBT_TESTING',
118+
target_lag='30 minutes',
119+
schema='custom_schema'
120+
) }}
121+
select * from {{ ref('simple_model') }}
122+
"""
123+
124+
DYNAMIC_TABLE_CUSTOM_DB_SCHEMA = """
125+
{{ config(
126+
materialized='dynamic_table',
127+
snowflake_warehouse='DBT_TESTING',
128+
target_lag='30 minutes',
129+
schema='custom_schema'
130+
) }}
131+
select * from {{ ref('simple_model') }}
132+
"""
133+
134+
SIMPLE_MODEL = """
135+
{{ config(
136+
materialized='table'
137+
) }}
138+
SELECT 1 as id
139+
"""
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
3+
from dbt.tests.util import run_dbt, run_dbt_and_capture
4+
5+
from tests.functional.relation_tests.dynamic_table_tests import models
6+
from tests.functional.utils import query_relation_type
7+
8+
9+
class TestDynamicTableFullRefreshCustomSchema:
10+
11+
@pytest.fixture(scope="class", autouse=True)
12+
def models(self):
13+
return {
14+
"simple_model.sql": models.SIMPLE_MODEL,
15+
"custom_schema_dynamic_table.sql": models.DYNAMIC_TABLE_CUSTOM_SCHEMA,
16+
}
17+
18+
def test_dynamic_table_full_refresh_with_custom_schema(self, project):
19+
"""Test that dynamic table full refresh works with custom schema configuration."""
20+
# Initial run to create the dynamic table
21+
run_dbt(["seed"])
22+
run_dbt(["run"])
23+
24+
# Run full refresh - this was failing before the fix
25+
run_dbt(["run", "--full-refresh"])
26+
run_dbt(["run", "--full-refresh"])

dbt-snowflake/tests/unit/test_renamed_relations.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ def test_renameable_relation():
1313
{
1414
SnowflakeRelationType.Table,
1515
SnowflakeRelationType.View,
16+
SnowflakeRelationType.DynamicTable,
1617
}
1718
)

0 commit comments

Comments
 (0)