diff --git a/plugins/postgres/dbt/adapters/postgres/impl.py b/plugins/postgres/dbt/adapters/postgres/impl.py index adffc4d3a62..a47295f7693 100644 --- a/plugins/postgres/dbt/adapters/postgres/impl.py +++ b/plugins/postgres/dbt/adapters/postgres/impl.py @@ -28,8 +28,17 @@ class PostgresIndexConfig(dbtClassMixin): columns: List[str] unique: bool = False type: Optional[str] = None + name: Optional[str] = None def render(self, relation): + # Use the index name if given + # Otherwise, generate a unique index name + if self.name: + return self.name + else: + return self.default_index_name(relation) + + def default_index_name(self, relation): # We append the current timestamp to the index name because otherwise # the index will only be created on every other run. See # https://github.com/dbt-labs/dbt-core/issues/1945#issuecomment-576714925 diff --git a/plugins/postgres/dbt/adapters/postgres/relation_configs/index.py b/plugins/postgres/dbt/adapters/postgres/relation_configs/index.py index 3a072ea4307..7aa5f0c1543 100644 --- a/plugins/postgres/dbt/adapters/postgres/relation_configs/index.py +++ b/plugins/postgres/dbt/adapters/postgres/relation_configs/index.py @@ -29,7 +29,7 @@ def default(cls) -> "PostgresIndexMethod": @dataclass(frozen=True, eq=True, unsafe_hash=True) class PostgresIndexConfig(RelationConfigBase, RelationConfigValidationMixin): """ - This config fallows the specs found here: + This config follows the specs found here: https://www.postgresql.org/docs/current/sql-createindex.html The following parameters are configurable by dbt: @@ -76,6 +76,7 @@ def from_dict(cls, config_dict) -> "PostgresIndexConfig": @classmethod def parse_model_node(cls, model_node_entry: dict) -> dict: config_dict = { + "name": model_node_entry.get("name"), "column_names": set(model_node_entry.get("columns", set())), "unique": model_node_entry.get("unique"), "method": model_node_entry.get("type"), @@ -98,10 +99,12 @@ def as_node_config(self) -> dict: Returns: a dictionary that can be passed into `get_create_index_sql()` """ node_config = { + "name": self.name, "columns": list(self.column_names), "unique": self.unique, - "type": self.method.value, + "type": self.method, } + return node_config