Skip to content
Open
Show file tree
Hide file tree
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
129 changes: 87 additions & 42 deletions dbt/include/sqlserver/macros/adapters/catalog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@

tables as (
select
object_id,
name as table_name,
schema_id as schema_id,
principal_id as principal_id,
'BASE TABLE' as table_type
t.object_id,
t.name as table_name,
t.schema_id as schema_id,
t.principal_id as principal_id,
'BASE TABLE' as table_type,
cast(ep.value as nvarchar(max)) as table_comment
from
sys.tables {{ information_schema_hints() }}
sys.tables as t {{ information_schema_hints() }}
left join sys.extended_properties as ep {{ information_schema_hints() }}
on ep.class = 1
and ep.major_id = t.object_id
and ep.minor_id = 0
and ep.name = N'MS_Description'
),

tables_with_metadata as (
Expand All @@ -37,21 +43,28 @@
table_name,
schema_name,
coalesce(tables.principal_id, schemas.principal_id) as owner_principal_id,
table_type
table_type,
table_comment
from
tables
join schemas on tables.schema_id = schemas.schema_id
),

views as (
select
object_id,
name as table_name,
schema_id as schema_id,
principal_id as principal_id,
'VIEW' as table_type
v.object_id,
v.name as table_name,
v.schema_id as schema_id,
v.principal_id as principal_id,
'VIEW' as table_type,
cast(ep.value as nvarchar(max)) as table_comment
from
sys.views {{ information_schema_hints() }}
sys.views as v {{ information_schema_hints() }}
left join sys.extended_properties as ep {{ information_schema_hints() }}
on ep.class = 1
and ep.major_id = v.object_id
and ep.minor_id = 0
and ep.name = N'MS_Description'
),

views_with_metadata as (
Expand All @@ -60,7 +73,8 @@
table_name,
schema_name,
coalesce(views.principal_id, schemas.principal_id) as owner_principal_id,
table_type
table_type,
table_comment
from
views
join schemas on views.schema_id = schemas.schema_id
Expand All @@ -72,7 +86,8 @@
table_name,
schema_name,
principal_name,
table_type
table_type,
table_comment
from
tables_with_metadata
join principals on tables_with_metadata.owner_principal_id = principals.principal_id
Expand All @@ -82,34 +97,42 @@
table_name,
schema_name,
principal_name,
table_type
table_type,
table_comment
from
views_with_metadata
join principals on views_with_metadata.owner_principal_id = principals.principal_id
),

cols as (

select
c.object_id,
c.name as column_name,
c.column_id as column_index,
t.name as column_type
t.name as column_type,
cast(ep.value as nvarchar(max)) as column_comment
from sys.columns as c {{ information_schema_hints() }}
left join sys.types as t {{ information_schema_hints() }} on c.system_type_id = t.system_type_id
left join sys.types as t {{ information_schema_hints() }}
on c.system_type_id = t.system_type_id
and c.user_type_id = t.user_type_id
left join sys.extended_properties as ep {{ information_schema_hints() }}
on ep.class = 1
and ep.major_id = c.object_id
and ep.minor_id = c.column_id
and ep.name = N'MS_Description'
)

select
DB_NAME() as table_database,
tv.schema_name as table_schema,
tv.table_name,
tv.table_type,
null as table_comment,
tv.table_comment,
tv.principal_name as table_owner,
cols.column_name,
cols.column_index,
cols.column_type,
null as column_comment
cols.column_comment
from tables_and_views tv
join cols on tv.object_id = cols.object_id
where ({%- for schema in schemas -%}
Expand Down Expand Up @@ -152,13 +175,19 @@

tables as (
select
object_id,
name as table_name,
schema_id as schema_id,
principal_id as principal_id,
'BASE TABLE' as table_type
t.object_id,
t.name as table_name,
t.schema_id as schema_id,
t.principal_id as principal_id,
'BASE TABLE' as table_type,
cast(ep.value as nvarchar(max)) as table_comment
from
sys.tables {{ information_schema_hints() }}
sys.tables as t {{ information_schema_hints() }}
left join sys.extended_properties as ep {{ information_schema_hints() }}
on ep.class = 1
and ep.major_id = t.object_id
and ep.minor_id = 0
and ep.name = N'MS_Description'
),

tables_with_metadata as (
Expand All @@ -167,21 +196,28 @@
table_name,
schema_name,
coalesce(tables.principal_id, schemas.principal_id) as owner_principal_id,
table_type
table_type,
table_comment
from
tables
join schemas on tables.schema_id = schemas.schema_id
),

views as (
select
object_id,
name as table_name,
schema_id as schema_id,
principal_id as principal_id,
'VIEW' as table_type
v.object_id,
v.name as table_name,
v.schema_id as schema_id,
v.principal_id as principal_id,
'VIEW' as table_type,
cast(ep.value as nvarchar(max)) as table_comment
from
sys.views {{ information_schema_hints() }}
sys.views as v {{ information_schema_hints() }}
left join sys.extended_properties as ep {{ information_schema_hints() }}
on ep.class = 1
and ep.major_id = v.object_id
and ep.minor_id = 0
and ep.name = N'MS_Description'
),

views_with_metadata as (
Expand All @@ -190,7 +226,8 @@
table_name,
schema_name,
coalesce(views.principal_id, schemas.principal_id) as owner_principal_id,
table_type
table_type,
table_comment
from
views
join schemas on views.schema_id = schemas.schema_id
Expand All @@ -202,7 +239,8 @@
table_name,
schema_name,
principal_name,
table_type
table_type,
table_comment
from
tables_with_metadata
join principals on tables_with_metadata.owner_principal_id = principals.principal_id
Expand All @@ -212,34 +250,41 @@
table_name,
schema_name,
principal_name,
table_type
table_type,
table_comment
from
views_with_metadata
join principals on views_with_metadata.owner_principal_id = principals.principal_id
),

cols as (

select
c.object_id,
c.name as column_name,
c.column_id as column_index,
t.name as column_type
t.name as column_type,
cast(ep.value as nvarchar(max)) as column_comment
from sys.columns as c {{ information_schema_hints() }}
left join sys.types as t on c.system_type_id = t.system_type_id
left join sys.types as t {{ information_schema_hints() }}
on c.user_type_id = t.user_type_id
left join sys.extended_properties as ep {{ information_schema_hints() }}
on ep.class = 1
and ep.major_id = c.object_id
and ep.minor_id = c.column_id
and ep.name = N'MS_Description'
)

select
DB_NAME() as table_database,
tv.schema_name as table_schema,
tv.table_name,
tv.table_type,
null as table_comment,
tv.table_comment,
tv.principal_name as table_owner,
cols.column_name,
cols.column_index,
cols.column_type,
null as column_comment
cols.column_comment
from tables_and_views tv
join cols on tv.object_id = cols.object_id
where (
Expand Down
137 changes: 137 additions & 0 deletions dbt/include/sqlserver/macros/adapters/persist_docs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{% macro sqlserver__alter_relation_comment(relation, relation_comment) -%}
{%- set escaped_comment = (relation_comment or '') | replace("'", "''") -%}
{%- set relation_name = relation.identifier | replace("'", "''") -%}
{%- set schema_name = relation.schema | replace("'", "''") -%}

declare @relation_schema sysname = N'{{ schema_name }}';
declare @relation_name sysname = N'{{ relation_name }}';
declare @relation_comment nvarchar(3750) = N'{{ escaped_comment }}';
declare @relation_type nvarchar(128);

select
@relation_type =
case
when obj.[type] = 'V' then N'VIEW'
when obj.[type] = 'U' then N'TABLE'
end
from sys.objects as obj {{ information_schema_hints() }}
inner join sys.schemas as sch {{ information_schema_hints() }}
on sch.schema_id = obj.schema_id
where sch.name = @relation_schema
and obj.name = @relation_name
and obj.[type] in ('U', 'V');

if @relation_type is not null
begin
if exists (
select 1
from sys.extended_properties as ep {{ information_schema_hints() }}
inner join sys.objects as obj {{ information_schema_hints() }}
on obj.object_id = ep.major_id
inner join sys.schemas as sch {{ information_schema_hints() }}
on sch.schema_id = obj.schema_id
where ep.class = 1
and ep.minor_id = 0
and ep.name = N'MS_Description'
and sch.name = @relation_schema
and obj.name = @relation_name
and obj.[type] in ('U', 'V')
)
begin
exec sys.sp_updateextendedproperty
@name = N'MS_Description',
@value = @relation_comment,
@level0type = N'SCHEMA',
@level0name = @relation_schema,
@level1type = @relation_type,
@level1name = @relation_name;
end
else
begin
exec sys.sp_addextendedproperty
@name = N'MS_Description',
@value = @relation_comment,
@level0type = N'SCHEMA',
@level0name = @relation_schema,
@level1type = @relation_type,
@level1name = @relation_name;
end
end;
{%- endmacro %}


{% macro sqlserver__alter_column_comment(relation, column_dict) -%}
{%- set relation_name = relation.identifier | replace("'", "''") -%}
{%- set schema_name = relation.schema | replace("'", "''") -%}

{%- for column_name, column_config in column_dict.items() %}
{%- set escaped_column_name = column_name | replace("'", "''") -%}
{%- set escaped_comment = (column_config.get('description') or '') | replace("'", "''") -%}

declare @schema_{{ loop.index }} sysname = N'{{ schema_name }}';
declare @relation_{{ loop.index }} sysname = N'{{ relation_name }}';
declare @column_{{ loop.index }} sysname = N'{{ escaped_column_name }}';
declare @comment_{{ loop.index }} nvarchar(3750) = N'{{ escaped_comment }}';
declare @relation_type_{{ loop.index }} nvarchar(128);

select
@relation_type_{{ loop.index }} =
case
when obj.[type] = 'V' then N'VIEW'
when obj.[type] = 'U' then N'TABLE'
end
from sys.objects as obj {{ information_schema_hints() }}
inner join sys.schemas as sch {{ information_schema_hints() }}
on sch.schema_id = obj.schema_id
inner join sys.columns as col {{ information_schema_hints() }}
on col.object_id = obj.object_id
where sch.name = @schema_{{ loop.index }}
and obj.name = @relation_{{ loop.index }}
and col.name = @column_{{ loop.index }}
and obj.[type] in ('U', 'V');

if @relation_type_{{ loop.index }} is not null
begin
if exists (
select 1
from sys.extended_properties as ep {{ information_schema_hints() }}
inner join sys.objects as obj {{ information_schema_hints() }}
on obj.object_id = ep.major_id
inner join sys.schemas as sch {{ information_schema_hints() }}
on sch.schema_id = obj.schema_id
inner join sys.columns as col {{ information_schema_hints() }}
on col.object_id = ep.major_id
and col.column_id = ep.minor_id
where ep.class = 1
and ep.name = N'MS_Description'
and sch.name = @schema_{{ loop.index }}
and obj.name = @relation_{{ loop.index }}
and col.name = @column_{{ loop.index }}
and obj.[type] in ('U', 'V')
)
begin
exec sys.sp_updateextendedproperty
@name = N'MS_Description',
@value = @comment_{{ loop.index }},
@level0type = N'SCHEMA',
@level0name = @schema_{{ loop.index }},
@level1type = @relation_type_{{ loop.index }},
@level1name = @relation_{{ loop.index }},
@level2type = N'COLUMN',
@level2name = @column_{{ loop.index }};
end
else
begin
exec sys.sp_addextendedproperty
@name = N'MS_Description',
@value = @comment_{{ loop.index }},
@level0type = N'SCHEMA',
@level0name = @schema_{{ loop.index }},
@level1type = @relation_type_{{ loop.index }},
@level1name = @relation_{{ loop.index }},
@level2type = N'COLUMN',
@level2name = @column_{{ loop.index }};
end
end;
{%- endfor %}
{%- endmacro %}
Loading