From 76788d65c784e9b571aa32a38bee7f5037716732 Mon Sep 17 00:00:00 2001 From: bentsileviav Date: Thu, 6 Nov 2025 19:57:02 +0200 Subject: [PATCH] move mvs drop to be before the exchange operation --- .../macros/materializations/materialized_view.sql | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/dbt/include/clickhouse/macros/materializations/materialized_view.sql b/dbt/include/clickhouse/macros/materializations/materialized_view.sql index 3b3f48ad..03d0f616 100644 --- a/dbt/include/clickhouse/macros/materializations/materialized_view.sql +++ b/dbt/include/clickhouse/macros/materializations/materialized_view.sql @@ -72,11 +72,13 @@ {% endfor %} {% endif %} {% if should_full_refresh() %} - {{ clickhouse__drop_mvs_by_suffixes(target_relation, cluster_clause, views) }} - {% call statement('main') -%} {{ get_create_table_as_sql(False, backup_relation, sql) }} {%- endcall %} + + {# Drop MV just before exchange to minimize blind period while avoiding old MV writing to new table #} + {{ clickhouse__drop_mvs_by_suffixes(target_relation, cluster_clause, views) }} + {% do exchange_tables_atomic(backup_relation, existing_relation) %} {{ clickhouse__create_mvs(existing_relation, cluster_clause, refreshable_clause, views) }} @@ -371,6 +373,3 @@ ) %} {% endif %} {% endmacro %} - - -