Skip to content

Commit 2a9a00f

Browse files
committed
Added support for indexes in hooks!
1 parent be4629d commit 2a9a00f

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
{# most of this code is from https://github.com/jacobm001/dbt-mssql/blob/master/dbt/include/mssql/macros/indexes.sql #}
2+
3+
{% macro drop_xml_indexes() -%}
4+
{# Altered from https://stackoverflow.com/q/1344401/10415173 #}
5+
{# and https://stackoverflow.com/a/33785833/10415173 #}
6+
7+
8+
{{ log("Running drop_xml_indexes() macro...") }}
9+
10+
declare @drop_xml_indexes nvarchar(max);
11+
select @drop_xml_indexes = (
12+
select 'IF INDEXPROPERTY(' + CONVERT(VARCHAR(MAX), sys.tables.[object_id]) + ', ''' + sys.indexes.[name] + ''', ''IndexId'') IS NOT NULL DROP INDEX [' + sys.indexes.[name] + '] ON ' + '[' + SCHEMA_NAME(sys.tables.[schema_id]) + '].[' + OBJECT_NAME(sys.tables.[object_id]) + ']; '
13+
from sys.indexes
14+
inner join sys.tables on sys.indexes.object_id = sys.tables.object_id
15+
where sys.indexes.[name] is not null
16+
and sys.indexes.type_desc = 'XML'
17+
and sys.tables.[name] = '{{ this.table }}'
18+
for xml path('')
19+
); exec sp_executesql @drop_xml_indexes;
20+
21+
{%- endmacro %}
22+
23+
24+
{% macro drop_spatial_indexes() -%}
25+
{# Altered from https://stackoverflow.com/q/1344401/10415173 #}
26+
{# and https://stackoverflow.com/a/33785833/10415173 #}
27+
28+
{{ log("Running drop_spatial_indexes() macro...") }}
29+
30+
declare @drop_spatial_indexes nvarchar(max);
31+
select @drop_spatial_indexes = (
32+
select 'IF INDEXPROPERTY(' + CONVERT(VARCHAR(MAX), sys.tables.[object_id]) + ', ''' + sys.indexes.[name] + ''', ''IndexId'') IS NOT NULL DROP INDEX [' + sys.indexes.[name] + '] ON ' + '[' + SCHEMA_NAME(sys.tables.[schema_id]) + '].[' + OBJECT_NAME(sys.tables.[object_id]) + ']; '
33+
from sys.indexes
34+
inner join sys.tables on sys.indexes.object_id = sys.tables.object_id
35+
where sys.indexes.[name] is not null
36+
and sys.indexes.type_desc = 'Spatial'
37+
and sys.tables.[name] = '{{ this.table }}'
38+
for xml path('')
39+
); exec sp_executesql @drop_spatial_indexes;
40+
41+
{%- endmacro %}
42+
43+
44+
{% macro drop_fk_constraints() -%}
45+
{# Altered from https://stackoverflow.com/q/1344401/10415173 #}
46+
47+
{{ log("Running drop_fk_constraints() macro...") }}
48+
49+
declare @drop_fk_constraints nvarchar(max);
50+
select @drop_fk_constraints = (
51+
select 'IF OBJECT_ID(''' + SCHEMA_NAME(CONVERT(VARCHAR(MAX), sys.foreign_keys.[schema_id])) + '.' + sys.foreign_keys.[name] + ''', ''F'') IS NOT NULL ALTER TABLE [' + SCHEMA_NAME(sys.foreign_keys.[schema_id]) + '].[' + OBJECT_NAME(sys.foreign_keys.[parent_object_id]) + '] DROP CONSTRAINT [' + sys.foreign_keys.[name]+ '];'
52+
from sys.foreign_keys
53+
inner join sys.tables on sys.foreign_keys.[referenced_object_id] = sys.tables.[object_id]
54+
where sys.tables.[name] = '{{ this.table }}'
55+
for xml path('')
56+
); exec sp_executesql @drop_fk_constraints;
57+
58+
{%- endmacro %}
59+
60+
61+
{% macro drop_pk_constraints() -%}
62+
{# Altered from https://stackoverflow.com/q/1344401/10415173 #}
63+
{# and https://stackoverflow.com/a/33785833/10415173 #}
64+
65+
{{ drop_xml_indexes() }}
66+
67+
{{ drop_spatial_indexes() }}
68+
69+
{{ drop_fk_constraints() }}
70+
71+
{{ log("Running drop_pk_constraints() macro...") }}
72+
73+
declare @drop_pk_constraints nvarchar(max);
74+
select @drop_pk_constraints = (
75+
select 'IF INDEXPROPERTY(' + CONVERT(VARCHAR(MAX), sys.tables.[object_id]) + ', ''' + sys.indexes.[name] + ''', ''IndexId'') IS NOT NULL ALTER TABLE [' + SCHEMA_NAME(sys.tables.[schema_id]) + '].[' + sys.tables.[name] + '] DROP CONSTRAINT [' + sys.indexes.[name]+ '];'
76+
from sys.indexes
77+
inner join sys.tables on sys.indexes.[object_id] = sys.tables.[object_id]
78+
where sys.indexes.is_primary_key = 1
79+
and sys.tables.[name] = '{{ this.table }}'
80+
for xml path('')
81+
); exec sp_executesql @drop_pk_constraints;
82+
83+
{%- endmacro %}
84+
85+
86+
{% macro drop_all_indexes_on_table() -%}
87+
{# Altered from https://stackoverflow.com/q/1344401/10415173 #}
88+
{# and https://stackoverflow.com/a/33785833/10415173 #}
89+
90+
{{ drop_pk_constraints() }}
91+
92+
{{ log("Dropping remaining indexes...") }}
93+
94+
declare @drop_remaining_indexes_last nvarchar(max);
95+
select @drop_remaining_indexes_last = (
96+
select 'IF INDEXPROPERTY(' + CONVERT(VARCHAR(MAX), sys.tables.[object_id]) + ', ''' + sys.indexes.[name] + ''', ''IndexId'') IS NOT NULL DROP INDEX [' + sys.indexes.[name] + '] ON ' + '[' + SCHEMA_NAME(sys.tables.[schema_id]) + '].[' + OBJECT_NAME(sys.tables.[object_id]) + ']; '
97+
from sys.indexes
98+
inner join sys.tables on sys.indexes.object_id = sys.tables.object_id
99+
where sys.indexes.[name] is not null
100+
and sys.tables.[name] = '{{ this.table }}'
101+
for xml path('')
102+
); exec sp_executesql @drop_remaining_indexes_last;
103+
104+
{%- endmacro %}
105+
106+
107+
{% macro create_clustered_index(columns, unique=False) -%}
108+
109+
{{ log("Creating clustered index...") }}
110+
111+
create
112+
{% if unique -%}
113+
unique
114+
{% endif %}
115+
clustered index
116+
{{ this.table }}__clustered_index_on_{{ columns|join("_") }}
117+
on {{ this }} ({{ '[' + columns|join("], [") + ']' }})
118+
119+
{%- endmacro %}
120+
121+
122+
{% macro create_nonclustered_index(columns, includes=False) %}
123+
124+
{{ log("Creating nonclustered index...") }}
125+
126+
create nonclustered index
127+
{{ this.table }}__index_on_{{ columns|join("_") }}
128+
on {{ this }} ({{ '[' + columns|join("], [") + ']' }})
129+
{% if includes -%}
130+
include ({{ '[' + includes|join("], [") + ']' }})
131+
{% endif %}
132+
133+
{% endmacro %}

0 commit comments

Comments
 (0)