From 88c2631c03c6df1741819c17c382a68948e8ac42 Mon Sep 17 00:00:00 2001 From: arover Date: Fri, 12 May 2023 18:01:41 +0200 Subject: [PATCH] bug on first launch --- macros/materializations/incremental_stream.sql | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/macros/materializations/incremental_stream.sql b/macros/materializations/incremental_stream.sql index b4a16da..3853400 100644 --- a/macros/materializations/incremental_stream.sql +++ b/macros/materializations/incremental_stream.sql @@ -33,13 +33,12 @@ DROP STREAM IF EXISTS {{target_stream}}; {#-- CREATE OBJECTS (STREAM, TABLE) IF NOT EXISTS --#} CREATE STREAM IF NOT EXISTS {{target_stream}} ON TABLE {{source_table}} APPEND_ONLY=TRUE; -CREATE TABLE IF NOT EXISTS {{ this }} AS SELECT * FROM {{target_view}}; - {%- set v_count = 0 -%} -{%- if full_refresh_mode %} +{% set relation_exists = load_relation(this) is not none %} +{%- if full_refresh_mode or not relation_exists%} CREATE OR REPLACE VIEW {{target_view}} AS ({{ sql }}); -INSERT INTO {{this}} SELECT * FROM {{target_view}}; +CREATE TABLE IF NOT EXISTS {{ this }} AS SELECT * FROM {{target_view}}; {%- else -%} {#-- Check the presence of records to merge --#} {%- set v_count -%} @@ -53,7 +52,6 @@ INSERT INTO {{this}} SELECT * FROM {{target_view}}; {%- set v_count = 0 -%} {%- endif -%} - {{ log(" => " +v_count | string + " record(s) to merge" , info=True) }} {%- endif -%}