diff --git a/CHANGELOG.md b/CHANGELOG.md index 7df96b63..6ed8ce08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ #### under the hood: - hotfix for regression introduced by [#126](https://github.com/dbt-msft/dbt-sqlserver/issues/126) that wouldn't surface syntax errors from the SQL engine [#140](https://github.com/dbt-msft/dbt-sqlserver/issues/140) thanks [@jeroen-mostert](https://github.com/jeroen-mostert)! +- ensure that macros are not recreated for incremental models [#116](https://github.com/dbt-msft/dbt-sqlserver/issues/116) thanks [@infused-kim](https://github.com/infused-kim) - authentication now is case-insensitive and accepts both `CLI` and `cli` as options. [#100](https://github.com/dbt-msft/dbt-sqlserver/issues/100) thanks (@JCZuurmond)[https://github.com/JCZuurmond] - add unit tests for azure-identity related token fetching diff --git a/dbt/include/sqlserver/macros/indexes.sql b/dbt/include/sqlserver/macros/indexes.sql index 396cda72..7a2d37ce 100644 --- a/dbt/include/sqlserver/macros/indexes.sql +++ b/dbt/include/sqlserver/macros/indexes.sql @@ -108,14 +108,23 @@ select @drop_remaining_indexes_last = ( {{ log("Creating clustered index...") }} +{% set idx_name = this.table + '__clustered_index_on_' + columns|join('_') %} + +if not exists(select * from sys.indexes + where + name = '{{ idx_name }}' and + object_id = OBJECT_ID('{{ this }}') +) +begin + create {% if unique -%} unique {% endif %} clustered index - {{ this.table }}__clustered_index_on_{{ columns|join("_") }} + {{ idx_name }} on {{ this }} ({{ '[' + columns|join("], [") + ']' }}) - +end {%- endmacro %} @@ -123,11 +132,19 @@ clustered index {{ log("Creating nonclustered index...") }} +{% set idx_name = this.table + '__index_on_' + columns|join('_') %} + +if not exists(select * from sys.indexes + where + name = '{{ idx_name }}' and + object_id = OBJECT_ID('{{ this }}') +) +begin create nonclustered index - {{ this.table }}__index_on_{{ columns|join("_") }} + {{ idx_name }} on {{ this }} ({{ '[' + columns|join("], [") + ']' }}) {% if includes -%} include ({{ '[' + includes|join("], [") + ']' }}) {% endif %} - +end {% endmacro %}