|
18 | 18 | {# {% exceptions.raise_compiler_error('Indexes are not supported') %} #} |
19 | 19 | {% endmacro %} |
20 | 20 |
|
21 | | -{% macro drop_all_indexes_on_table() -%} |
22 | | - {# {% exceptions.raise_compiler_error('Indexes are not supported') %} #} |
| 21 | +{% macro drop_fk_indexes_on_table(relation) -%} |
| 22 | + {% call statement('find_references', fetch_result=true) %} |
| 23 | + USE [{{ relation.database }}]; |
| 24 | + SELECT obj.name AS FK_NAME, |
| 25 | + sch.name AS [schema_name], |
| 26 | + tab1.name AS [table], |
| 27 | + col1.name AS [column], |
| 28 | + tab2.name AS [referenced_table], |
| 29 | + col2.name AS [referenced_column] |
| 30 | + FROM sys.foreign_key_columns fkc |
| 31 | + INNER JOIN sys.objects obj |
| 32 | + ON obj.object_id = fkc.constraint_object_id |
| 33 | + INNER JOIN sys.tables tab1 |
| 34 | + ON tab1.object_id = fkc.parent_object_id |
| 35 | + INNER JOIN sys.schemas sch |
| 36 | + ON tab1.schema_id = sch.schema_id |
| 37 | + INNER JOIN sys.columns col1 |
| 38 | + ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_id |
| 39 | + INNER JOIN sys.tables tab2 |
| 40 | + ON tab2.object_id = fkc.referenced_object_id |
| 41 | + INNER JOIN sys.columns col2 |
| 42 | + ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id |
| 43 | + WHERE sch.name = '{{ relation.schema }}' and tab2.name = '{{ relation.identifier }}' |
| 44 | + {% endcall %} |
| 45 | + {% set references = load_result('find_references')['data'] %} |
| 46 | + {% for reference in references -%} |
| 47 | + {% call statement('main') -%} |
| 48 | + alter table [{{reference[1]}}].[{{reference[2]}}] drop constraint [{{reference[0]}}] |
| 49 | + {%- endcall %} |
| 50 | + {% endfor %} |
23 | 51 | {% endmacro %} |
24 | 52 |
|
25 | 53 | {% macro create_clustered_index(columns, unique=False) -%} |
|
29 | 57 | {% macro create_nonclustered_index(columns, includes=False) %} |
30 | 58 | {# {% exceptions.raise_compiler_error('Indexes are not supported') %} #} |
31 | 59 | {% endmacro %} |
| 60 | + |
| 61 | +{% macro fabric__list_nonclustered_rowstore_indexes(relation) -%} |
| 62 | + {% call statement('list_nonclustered_rowstore_indexes', fetch_result=True) -%} |
| 63 | + |
| 64 | + SELECT i.name AS index_name |
| 65 | + , i.name + '__dbt_backup' as index_new_name |
| 66 | + , COL_NAME(ic.object_id,ic.column_id) AS column_name |
| 67 | + FROM sys.indexes AS i |
| 68 | + INNER JOIN sys.index_columns AS ic |
| 69 | + ON i.object_id = ic.object_id AND i.index_id = ic.index_id and i.type <> 5 |
| 70 | + WHERE i.object_id = OBJECT_ID('{{ relation.schema }}.{{ relation.identifier }}') |
| 71 | + |
| 72 | + UNION ALL |
| 73 | + |
| 74 | + SELECT obj.name AS index_name |
| 75 | + , obj.name + '__dbt_backup' as index_new_name |
| 76 | + , col1.name AS column_name |
| 77 | + FROM sys.foreign_key_columns fkc |
| 78 | + INNER JOIN sys.objects obj |
| 79 | + ON obj.object_id = fkc.constraint_object_id |
| 80 | + INNER JOIN sys.tables tab1 |
| 81 | + ON tab1.object_id = fkc.parent_object_id |
| 82 | + INNER JOIN sys.schemas sch |
| 83 | + ON tab1.schema_id = sch.schema_id |
| 84 | + INNER JOIN sys.columns col1 |
| 85 | + ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_id |
| 86 | + INNER JOIN sys.tables tab2 |
| 87 | + ON tab2.object_id = fkc.referenced_object_id |
| 88 | + INNER JOIN sys.columns col2 |
| 89 | + ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id |
| 90 | + WHERE sch.name = '{{ relation.schema }}' and tab1.name = '{{ relation.identifier }}' |
| 91 | + |
| 92 | + {% endcall %} |
| 93 | + {{ return(load_result('list_nonclustered_rowstore_indexes').table) }} |
| 94 | +{% endmacro %} |
0 commit comments