Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implementation of persist_docs using extending properties #289

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions dbt/include/sqlserver/macros/adapters/persist_docs.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
{# we don't support "persist docs" today, but we'd like to!
https://github.com/dbt-msft/dbt-sqlserver/issues/134

#}
{% macro sqlserver__alter_column_comment(relation, column_dict) -%}
{%- set existing_columns = adapter.get_columns_in_relation(relation)|map(attribute="name")|list %}
{%- for column_name in column_dict if (column_name in existing_columns) %}
{{ log('Alter extended property "MS_Description" to "' ~ column_dict[column_name]['description'] ~ '" for ' ~ relation ~ ' column "' ~ column_name ~ '"') }}
if not exists (
select 1
from
sys.extended_properties as ep
inner join sys.all_columns as cols
on cols.object_id = ep.major_id
and cols.column_id = ep.minor_id
where
ep.major_id = object_id('{{ relation }}')
and ep.name = N'MS_Description'
and cols.name = N'{{ column_name }}'
)
execute sp_addextendedproperty @name = N'MS_Description', @value = N'{{ column_dict[column_name]['description'] }}'
, @level0type = N'SCHEMA', @level0name = N'{{ relation.schema }}'
, @level1type = N'{{ relation.type }}', @level1name = N'{{ relation.identifier }}'
, @level2type = N'COLUMN', @level2name = N'{{ column_name }}';
else
execute sp_updateextendedproperty @name = N'MS_Description', @value = N'{{ column_dict[column_name]['description'] }}'
, @level0type = N'SCHEMA', @level0name = N'{{ relation.schema }}'
, @level1type = N'{{ relation.type }}', @level1name = N'{{ relation.identifier }}'
, @level2type = N'COLUMN', @level2name = N'{{ column_name }}';
{%- endfor %}
{%- endmacro %}