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

Make index macros not recreate indexes on incremental models #116

Merged
merged 4 commits into from
Jun 23, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 21 additions & 4 deletions dbt/include/sqlserver/macros/indexes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -108,26 +108,43 @@ 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 %}


{% macro create_nonclustered_index(columns, includes=False) %}

{{ 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 %}