Skip to content

Commit

Permalink
Refresh the materialized_view table only if --full-refresh is speci…
Browse files Browse the repository at this point in the history
…fied (#288)
  • Loading branch information
the4thamigo-uk committed Jun 19, 2024
1 parent 9c212f2 commit 5d31bc9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,17 @@
{% call statement('drop existing materialized view') %}
drop view if exists {{ mv_relation }} {{ cluster_clause }}
{% endcall %}
{% call statement('main') -%}
{{ get_create_table_as_sql(False, backup_relation, sql) }}
{%- endcall %}
{% do exchange_tables_atomic(backup_relation, existing_relation) %}
{% if should_full_refresh() %}
{% call statement('main') -%}
{{ get_create_table_as_sql(False, backup_relation, sql) }}
{%- endcall %}
{% do exchange_tables_atomic(backup_relation, existing_relation) %}
{% else %}
-- we need to have a 'main' statement
{% call statement('main') -%}
select 1
{%- endcall %}
{% endif %}
{% call statement('create new materialized view') %}
{{ clickhouse__create_mv_sql(mv_relation, existing_relation, cluster_clause, sql) }}
{% endcall %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def models(self):
"hackers.sql": MV_MODEL,
}

def test_update(self, project):
def test_update_incremental(self, project):
schema = quote_identifier(project.test_schema + "_custom_schema")
# create our initial materialized view
run_dbt(["seed"])
Expand All @@ -168,3 +168,26 @@ def test_update(self, project):
f"select distinct hacker_alias from {schema}.hackers where name = 'Dade'", fetch="all"
)
assert len(result) == 2

def test_update_full_refresh(self, project):
schema = quote_identifier(project.test_schema + "_custom_schema")
# create our initial materialized view
run_dbt(["seed"])
run_dbt()

# re-run dbt but this time with the new MV SQL
run_vars = {"run_type": "extended_schema"}
run_dbt(["run", "--full-refresh", "--vars", json.dumps(run_vars)])

project.run_sql(
f"""
insert into {quote_identifier(project.test_schema)}.people ("id", "name", "age", "department")
values (1232,'Dade',11,'engineering'), (9999,'eugene',40,'malware');
"""
)

# assert that we now have both of Dade's aliases in our hackers table
result = project.run_sql(
f"select distinct hacker_alias from {schema}.hackers where name = 'Dade'", fetch="all"
)
assert len(result) == 2

0 comments on commit 5d31bc9

Please sign in to comment.