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

Add ability to configure index names #8656

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions plugins/postgres/dbt/adapters/postgres/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"),
Expand All @@ -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


Expand Down
Loading