From 903ccea98d354a7eaa7d3dfd13679dc29e4d4597 Mon Sep 17 00:00:00 2001 From: Kaarthik Andavar Date: Fri, 15 Sep 2023 05:15:27 +1200 Subject: [PATCH] bug/Dynamic-table-comment-on-syntax (#770) * bug/Dynamic-table-comment-on-syntax * Add changelog * Fix relation type * Fix alter --- .../unreleased/Fixes-20230914-110800.yaml | 6 ++++ dbt/include/snowflake/macros/adapters.sql | 33 ++++++++++++++----- 2 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 .changes/unreleased/Fixes-20230914-110800.yaml diff --git a/.changes/unreleased/Fixes-20230914-110800.yaml b/.changes/unreleased/Fixes-20230914-110800.yaml new file mode 100644 index 000000000..3aa206a73 --- /dev/null +++ b/.changes/unreleased/Fixes-20230914-110800.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Fixing comment on syntax for dynamic tables +time: 2023-09-14T11:08:00.250684+12:00 +custom: + Author: kaarthik108 + Issue: '769' diff --git a/dbt/include/snowflake/macros/adapters.sql b/dbt/include/snowflake/macros/adapters.sql index c4afd525b..024c05274 100644 --- a/dbt/include/snowflake/macros/adapters.sql +++ b/dbt/include/snowflake/macros/adapters.sql @@ -171,13 +171,23 @@ {% endmacro %} {% macro snowflake__alter_relation_comment(relation, relation_comment) -%} - comment on {{ relation.type }} {{ relation }} IS $${{ relation_comment | replace('$', '[$]') }}$$; + {%- if relation.is_dynamic_table -%} + {%- set relation_type = 'dynamic table' -%} + {%- else -%} + {%- set relation_type = relation.type -%} + {%- endif -%} + comment on {{ relation_type }} {{ relation }} IS $${{ relation_comment | replace('$', '[$]') }}$$; {% endmacro %} {% macro snowflake__alter_column_comment(relation, column_dict) -%} {% set existing_columns = adapter.get_columns_in_relation(relation) | map(attribute="name") | list %} - alter {{ relation.type }} {{ relation }} alter + {% if relation.is_dynamic_table -%} + {% set relation_type = "dynamic table" %} + {% else -%} + {% set relation_type = relation.type %} + {% endif %} + alter {{ relation_type }} {{ relation }} alter {% for column_name in existing_columns if (column_name in existing_columns) or (column_name|lower in existing_columns) %} {{ get_column_comment_sql(column_name, column_dict) }} {{- ',' if not loop.last else ';' }} {% endfor %} @@ -227,10 +237,16 @@ {% macro snowflake__alter_relation_add_remove_columns(relation, add_columns, remove_columns) %} - {% if add_columns %} + {% if relation.is_dynamic_table -%} + {% set relation_type = "dynamic table" %} + {% else -%} + {% set relation_type = relation.type %} + {% endif %} + + {% if add_columns %} {% set sql -%} - alter {{ relation.type }} {{ relation }} add column + alter {{ relation_type }} {{ relation }} add column {% for column in add_columns %} {{ column.name }} {{ column.data_type }}{{ ',' if not loop.last }} {% endfor %} @@ -238,12 +254,12 @@ {% do run_query(sql) %} - {% endif %} + {% endif %} - {% if remove_columns %} + {% if remove_columns %} {% set sql -%} - alter {{ relation.type }} {{ relation }} drop column + alter {{ relation_type }} {{ relation }} drop column {% for column in remove_columns %} {{ column.name }}{{ ',' if not loop.last }} {% endfor %} @@ -251,11 +267,12 @@ {% do run_query(sql) %} - {% endif %} + {% endif %} {% endmacro %} + {% macro snowflake_dml_explicit_transaction(dml) %} {# Use this macro to wrap all INSERT, MERGE, UPDATE, DELETE, and TRUNCATE