From f42ab4a7e043a8a9e35c1fefadd42554a59836f4 Mon Sep 17 00:00:00 2001 From: Septimiu Turcu Date: Thu, 5 Sep 2024 02:38:31 +0200 Subject: [PATCH 1/2] Update code for create_columns and get_true_sql macros --- .../materializations/snapshot/helpers.sql | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/dbt/include/teradata/macros/materializations/snapshot/helpers.sql b/dbt/include/teradata/macros/materializations/snapshot/helpers.sql index 6ba36453..6c51c494 100644 --- a/dbt/include/teradata/macros/materializations/snapshot/helpers.sql +++ b/dbt/include/teradata/macros/materializations/snapshot/helpers.sql @@ -1,3 +1,19 @@ +{% macro teradata__create_columns(relation, columns) %} + {% for column in columns %} + {% call statement() %} + alter table {{ relation.render() }} add "{{ column.name }}" {{ column.data_type }}; + {% endcall %} + {% endfor %} +{% endmacro %} + +{% macro teradata__post_snapshot(staging_relation) %} + {{ adapter.dispatch('drop_relation', 'dbt')(staging_relation) }} +{% endmacro %} + +{% macro teradata__get_true_sql() %} + {{ return('1 = 1') }} +{% endmacro %} + {% macro teradata__snapshot_staging_table(strategy, source_sql, target_relation) -%} with snapshot_query as ( @@ -125,8 +141,3 @@ ) sbq {% endmacro %} - -{% macro teradata__post_snapshot(staging_relation) %} - {{ adapter.dispatch('drop_relation', 'dbt')(staging_relation) }} -{% endmacro %} - From 421c5e9da897fc7c1fed31f39cf6235bb7369a08 Mon Sep 17 00:00:00 2001 From: Varun Sharma Date: Wed, 13 Nov 2024 12:15:26 +0530 Subject: [PATCH 2/2] testcase for snapshot with schema change --- .../snapshot_on_schema_change.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tests/functional/adapter/simple_snapshot/snapshot_on_schema_change.py diff --git a/tests/functional/adapter/simple_snapshot/snapshot_on_schema_change.py b/tests/functional/adapter/simple_snapshot/snapshot_on_schema_change.py new file mode 100644 index 00000000..7a2976e8 --- /dev/null +++ b/tests/functional/adapter/simple_snapshot/snapshot_on_schema_change.py @@ -0,0 +1,56 @@ +import pytest +from dbt.tests.util import run_dbt + +seeds_base_csv = """ +id,name,some_date +1,Easton,1981-05-20T06:46:51 +2,Lillian,1978-09-03T18:10:33 +3,Jeremiah,1982-03-11T03:59:51 +4,Nolan,1976-05-06T20:21:35 +5,Hannah,1982-06-23T05:41:26 +6,Eleanor,1991-08-10T23:12:21 +7,Lily,1971-03-29T14:58:02 +8,Jonathan,1988-02-26T02:55:24 +9,Adrian,1994-02-09T13:14:23 +10,Nora,1976-03-01T16:51:39 +""".lstrip() + +ts_snapshot_sql = """ +{% snapshot ts_snapshot %} + {{ config( + strategy='timestamp', + unique_key='id', + updated_at='some_date', + target_database=database, + target_schema=schema, + )}} + select * from {{ ref(var('seed_name', 'base')) }} +{% endsnapshot %} +""".strip() + + +class TestSnapshotOnSchemaChange: + @pytest.fixture(scope="class") + def seeds(self): + return { + "base.csv": seeds_base_csv, + } + + @pytest.fixture(scope="class") + def snapshots(self): + return { + "ts_snapshot.sql": ts_snapshot_sql, + } + + def test_snapshot_on_schema_change(self, project): + # seed command + results = run_dbt(["seed"]) + + # snapshot command + results = run_dbt(["snapshot"]) + + project.run_sql("ALTER TABLE {schema}.base ADD last_initial varchar(30)"); + + results = run_dbt(["seed"]) + + results = run_dbt(["snapshot"])