Skip to content

Commit

Permalink
fixup! Change table materialization logic when on_table_exists = 'ren…
Browse files Browse the repository at this point in the history
…ame'
  • Loading branch information
hovaesco committed Jul 30, 2024
1 parent 2615b20 commit eb00329
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
3 changes: 1 addition & 2 deletions dbt/include/trino/macros/materializations/table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@
{% call statement('main') -%}
{{ create_table_as(False, target_relation, sql) }}
{%- endcall %}
{% endif %}

{#-- table does exists #}
{% if existing_relation is not none %}
{% else %}
{#-- build modeldock #}
{% call statement('main') -%}
{{ create_table_as(False, intermediate_relation, sql) }}
Expand Down
60 changes: 41 additions & 19 deletions tests/functional/adapter/materialization/test_on_table_exists.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,30 @@ def test_run_seed_test(self, project):
results = run_dbt(["seed"], expect_pass=True)
assert len(results) == 1
# run models two times to check on_table_exists = 'rename'
results = run_dbt(["run"], expect_pass=True)
results, logs = run_dbt_and_capture(["--debug", "run"], expect_pass=True)
assert len(results) == 1
results = run_dbt(["run"], expect_pass=True)
assert (
f'create table "{project.database}"."{project.test_schema}"."materialization"' in logs
)
assert "alter table" not in logs
results, logs = run_dbt_and_capture(["--debug", "run"], expect_pass=True)
assert len(results) == 1
assert (
f'create table "{project.database}"."{project.test_schema}"."materialization__dbt_tmp"'
in logs
)
assert (
f'alter table "{project.database}"."{project.test_schema}"."materialization" rename to "{project.database}"."{project.test_schema}"."materialization__dbt_backup"'
in logs
)
assert (
f'alter table "{project.database}"."{project.test_schema}"."materialization__dbt_tmp" rename to "{project.database}"."{project.test_schema}"."materialization"'
in logs
)
assert (
f'drop table if exists "{project.database}"."{project.test_schema}"."materialization__dbt_backup"'
in logs
)
# test tests
results = run_dbt(["test"], expect_pass=True)
assert len(results) == 3
Expand All @@ -60,11 +80,6 @@ def test_run_seed_test(self, project):
check_relations_equal(project.adapter, ["seed", "materialization"])


@pytest.mark.iceberg
class TestOnTableExistsRenameIceberg(TestOnTableExistsRename):
pass


class TestOnTableExistsRenameIncrementalFullRefresh(BaseOnTableExists):
"""
Testing on_table_exists = `rename` configuration for incremental materialization and full refresh flag,
Expand All @@ -89,14 +104,28 @@ def test_run_seed_test(self, project):
assert len(results) == 1
# run models two times to check on_table_exists = 'rename'
results, logs = run_dbt_and_capture(["--debug", "run"], expect_pass=True)
assert len(results) == 1
assert (
f'drop table if exists "{project.database}"."{project.test_schema}"."materialization"'
not in logs
f'create table "{project.database}"."{project.test_schema}"."materialization"' in logs
)
assert "dbt_tmp" and "dbt_backup" not in logs
results, logs = run_dbt_and_capture(["--debug", "run", "--full-refresh"], expect_pass=True)
assert "alter table" not in logs
results, logs = run_dbt_and_capture(["--debug", "run"], expect_pass=True)
assert len(results) == 1
assert (
f'create table "{project.database}"."{project.test_schema}"."materialization__dbt_tmp"'
in logs
)
assert (
f'alter table "{project.database}"."{project.test_schema}"."materialization" rename to "{project.database}"."{project.test_schema}"."materialization__dbt_backup"'
in logs
)
assert (
f'alter table "{project.database}"."{project.test_schema}"."materialization__dbt_tmp" rename to "{project.database}"."{project.test_schema}"."materialization"'
in logs
)
assert (
f'drop table if exists "{project.database}"."{project.test_schema}"."materialization__dbt_backup"'
in logs
)
assert "dbt_tmp" and "dbt_backup" in logs
# test tests
results = run_dbt(["test"], expect_pass=True)
Expand All @@ -106,13 +135,6 @@ def test_run_seed_test(self, project):
check_relations_equal(project.adapter, ["seed", "materialization"])


@pytest.mark.iceberg
class TestOnTableExistsRenameIcebergIncrementalFullRefresh(
TestOnTableExistsRenameIncrementalFullRefresh
):
pass


class TestOnTableExistsDrop(BaseOnTableExists):
"""
Testing on_table_exists = `drop` configuration for table materialization,
Expand Down

0 comments on commit eb00329

Please sign in to comment.