Skip to content

Commit 5b324a7

Browse files
committed
on_table_exists=skip
implement table materialization option on_table_exists=skip to allow user to skip a model if the target relation already exists.
1 parent 34dd3ab commit 5b324a7

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

dbt/include/trino/macros/materializations/table.sql

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% materialization table, adapter = 'trino' %}
22
{%- set on_table_exists = config.get('on_table_exists', 'rename') -%}
3-
{% if on_table_exists not in ['rename', 'drop', 'replace'] %}
3+
{% if on_table_exists not in ['rename', 'drop', 'replace', 'skip'] %}
44
{%- set log_message = 'Invalid value for on_table_exists (%s) specified. Setting default value (%s).' % (on_table_exists, 'rename') -%}
55
{% do log(log_message) %}
66
{%- set on_table_exists = 'rename' -%}
@@ -86,5 +86,26 @@
8686
{% call statement('main') -%}
8787
{{ create_table_as(False, target_relation, sql, True) }}
8888
{%- endcall %}
89+
90+
{% elif on_table_exists == 'skip' %}
91+
92+
{#-- table does not exists #}
93+
{% if existing_relation is none %}
94+
{#-- build model #}
95+
{% call statement('main') -%}
96+
{{ create_table_as(False, target_relation, sql) }}
97+
{%- endcall %}
98+
99+
{#-- table does exists #}
100+
{% else %}
101+
{#-- do nothing #}
102+
{%- set log_message = 'table %s already exists. skipping.' % (existing_relation) -%}
103+
{% do log(log_message) %}
104+
{% call statement('main') -%}
105+
select 1
106+
{%- endcall %}
107+
108+
{% endif %}
109+
89110
{% endif %}
90111
{% endmacro %}

tests/functional/adapter/materialization/test_on_table_exists.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,47 @@ def test_run_seed_test(self, project):
8080
check_relations_equal(project.adapter, ["seed", "materialization"])
8181

8282

83+
class TestOnTableExistsSkip(BaseOnTableExists):
84+
"""
85+
Testing on_table_exists = `skip` configuration for table materialization,
86+
using dbt seed, run and tests commands and validate data load correctness.
87+
"""
88+
89+
@pytest.fixture(scope="class")
90+
def project_config_update(self):
91+
return {
92+
"name": "table_rename",
93+
"models": {"+materialized": "table", "+on_table_exists": "skip"},
94+
"seeds": {
95+
"+column_types": {"some_date": "timestamp(6)"},
96+
},
97+
}
98+
99+
# The actual sequence of dbt commands and assertions
100+
# pytest will take care of all "setup" + "teardown"
101+
def test_run_seed_test(self, project):
102+
# seed seeds
103+
results = run_dbt(["seed"], expect_pass=True)
104+
assert len(results) == 1
105+
# run models two times to check on_table_exists = 'skip'
106+
results, logs = run_dbt_and_capture(["--debug", "run"], expect_pass=True)
107+
assert len(results) == 1
108+
assert (
109+
f'create table "{project.database}"."{project.test_schema}"."materialization"' in logs
110+
)
111+
assert "alter table" not in logs
112+
results, logs = run_dbt_and_capture(["--debug", "run"], expect_pass=True)
113+
assert len(results) == 1
114+
assert "drop table" not in logs
115+
assert "create table" not in logs
116+
# test tests
117+
results = run_dbt(["test"], expect_pass=True)
118+
assert len(results) == 3
119+
120+
# check if the data was loaded correctly
121+
check_relations_equal(project.adapter, ["seed", "materialization"])
122+
123+
83124
class TestOnTableExistsRenameIncrementalFullRefresh(BaseOnTableExists):
84125
"""
85126
Testing on_table_exists = `rename` configuration for incremental materialization and full refresh flag,

0 commit comments

Comments
 (0)