Skip to content

Commit

Permalink
bug/Dynamic-table-comment-on-syntax (#770)
Browse files Browse the repository at this point in the history
* bug/Dynamic-table-comment-on-syntax
* Add changelog
* Fix relation type
* Fix alter
  • Loading branch information
kaarthik108 authored Sep 14, 2023
1 parent cf04fd7 commit 903ccea
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230914-110800.yaml
Original file line number Diff line number Diff line change
@@ -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'
33 changes: 25 additions & 8 deletions dbt/include/snowflake/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down Expand Up @@ -227,35 +237,42 @@

{% 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 %}
{%- endset -%}

{% 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 %}
{%- endset -%}

{% 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
Expand Down

0 comments on commit 903ccea

Please sign in to comment.