Skip to content

Commit

Permalink
first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Arzav Jain authored and arzavj committed Apr 6, 2021
1 parent 6acd4b9 commit 2e5ff40
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
8 changes: 8 additions & 0 deletions core/dbt/include/global_project/macros/adapters/common.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@

{% endmacro %}

{% macro create_indexes(relation) -%}
{{ adapter.dispatch('create_indexes')(relation) }}
{%- endmacro %}

{% macro default__create_indexes(relation) -%}
-- NOOP
{% endmacro %}

{% macro create_view_as(relation, sql) -%}
{{ adapter.dispatch('create_view_as')(relation, sql) }}
{%- endmacro %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@

{{ adapter.rename_relation(intermediate_relation, target_relation) }}

{% post_hooks.append(create_indexes(target_relation)) %}

{{ run_hooks(post_hooks, inside_transaction=True) }}

{% do persist_docs(target_relation, model) %}
Expand Down
3 changes: 2 additions & 1 deletion plugins/postgres/dbt/adapters/postgres/impl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Optional, Set
from typing import Optional, Set, Dict, List, Any
from dbt.adapters.base.meta import available
from dbt.adapters.base.impl import AdapterConfig
from dbt.adapters.sql import SQLAdapter
Expand All @@ -16,6 +16,7 @@
@dataclass
class PostgresConfig(AdapterConfig):
unlogged: Optional[bool] = None
indexes: Optional[List[Dict[str, Any]]] = None


class PostgresAdapter(SQLAdapter):
Expand Down
38 changes: 38 additions & 0 deletions plugins/postgres/dbt/include/postgres/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,44 @@
);
{%- endmacro %}

{% macro truncate_string(str, length=3) %}
{% set substrings = [] %}

{% for word in str.split("_") %}
{{ substrings.append(word[:length]) }}
{% endfor %}

{{ return("_".join(substrings)) }}
{% endmacro %}

{% macro create_index(unique, columns) -%}
{%- set comma_columns_str = ", ".join(columns) -%}
{%- set underscore_columns_str = "_".join(columns) -%}
{%- set truncated_table_name = truncate_string(relation) -%}
{%- set truncated_col_names = truncate_string(underscore_columns_str) -%}

{#- We append the current timestamp to the index name because otherwise the index will -#}
{#- only be created on every other run. See -#}
{#- https:/fishtown-analytics/dbt/issues/1945#issuecomment-576714925 for -#}
{#- an explanation. -#}
{%- set index_name = "_".join([run_started_at.strftime("%s"), truncated_table_name, truncated_col_names, "idx"]) -%}

create {% if unique -%}
unique
{%- endif %} index if not exists
"{{ index_name }}" on {{ relation }}
({{ comma_columns_str }});
{%- endmacro %}

{% macro postgres__create_indexes(relation) -%}
{%- set _indexes = config.get('indexes', default=[]) -%}

{% for _index in _indexes %}
{{ create_index(_index['unique'], _index['columns']) }}
{% endfor %}
{% endmacro %}


{% macro postgres__create_schema(relation) -%}
{% if relation.database -%}
{{ adapter.verify_database(relation.database) }}
Expand Down

0 comments on commit 2e5ff40

Please sign in to comment.