Skip to content

Commit

Permalink
Merge pull request #169 from dlarsen5/add-snapshot-alter-column-macro
Browse files Browse the repository at this point in the history
add sqlserver__alter_column_type macro
  • Loading branch information
dataders authored Nov 5, 2021
2 parents 4091a07 + 0dacf08 commit d25c300
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions dbt/include/sqlserver/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,18 @@
{%- set result = "CONVERT(DATETIME2, '" ~ timestamp ~ "')" -%}
{{ return(result) }}
{%- endmacro %}

{% macro sqlserver__alter_column_type(relation, column_name, new_column_type) %}

{%- set tmp_column = column_name + "__dbt_alter" -%}

{% call statement('alter_column_type') -%}

alter {{ relation.type }} {{ relation }} add {{ tmp_column }} {{ new_column_type }};
update {{ relation }} set {{ tmp_column }} = {{ column_name }};
alter {{ relation.type }} {{ relation }} drop column {{ column_name }};
exec sp_rename '{{ relation | replace('"', '') }}.{{ tmp_column }}', '{{ column_name }}', 'column'
{%- endcall -%}
{% endmacro %}

0 comments on commit d25c300

Please sign in to comment.