diff --git a/.changes/unreleased/Under the Hood-20231214-122134.yaml b/.changes/unreleased/Under the Hood-20231214-122134.yaml new file mode 100644 index 00000000000..97f98fb0592 --- /dev/null +++ b/.changes/unreleased/Under the Hood-20231214-122134.yaml @@ -0,0 +1,6 @@ +kind: Under the Hood +body: have dbt-postgres use RelationConfig protocol for materialized views' +time: 2023-12-14T12:21:34.756973-08:00 +custom: + Author: colin-rogers-dbt + Issue: "9292" diff --git a/plugins/postgres/dbt/adapters/postgres/impl.py b/plugins/postgres/dbt/adapters/postgres/impl.py index 8dc5c487245..98e28255dbb 100644 --- a/plugins/postgres/dbt/adapters/postgres/impl.py +++ b/plugins/postgres/dbt/adapters/postgres/impl.py @@ -18,7 +18,7 @@ IndexConfigError, UnexpectedDbReferenceError, ) -import dbt.utils +from dbt.common.utils import encoding as dbt_encoding GET_RELATIONS_MACRO_NAME = "postgres__get_relations" @@ -38,7 +38,7 @@ def render(self, relation): now = datetime.utcnow().isoformat() inputs = self.columns + [relation.render(), str(self.unique), str(self.type), now] string = "_".join(inputs) - return dbt.utils.md5(string) + return dbt_encoding.md5(string) @classmethod def parse(cls, raw_index) -> Optional["PostgresIndexConfig"]: diff --git a/plugins/postgres/dbt/adapters/postgres/relation.py b/plugins/postgres/dbt/adapters/postgres/relation.py index 5c76052ebf4..31037ef7826 100644 --- a/plugins/postgres/dbt/adapters/postgres/relation.py +++ b/plugins/postgres/dbt/adapters/postgres/relation.py @@ -6,8 +6,7 @@ RelationConfigChangeAction, RelationResults, ) -from dbt.context.providers import RuntimeConfigObject -from dbt.adapters.contracts.relation import RelationType +from dbt.adapters.contracts.relation import RelationType, RelationConfig from dbt.common.exceptions import DbtRuntimeError from dbt.adapters.postgres.relation_configs import ( @@ -52,16 +51,14 @@ def relation_max_name_length(self): return MAX_CHARACTERS_IN_IDENTIFIER def get_materialized_view_config_change_collection( - self, relation_results: RelationResults, runtime_config: RuntimeConfigObject + self, relation_results: RelationResults, relation_config: RelationConfig ) -> Optional[PostgresMaterializedViewConfigChangeCollection]: config_change_collection = PostgresMaterializedViewConfigChangeCollection() existing_materialized_view = PostgresMaterializedViewConfig.from_relation_results( relation_results ) - new_materialized_view = PostgresMaterializedViewConfig.from_model_node( - runtime_config.model - ) + new_materialized_view = PostgresMaterializedViewConfig.from_config(relation_config) config_change_collection.indexes = self._get_index_config_changes( existing_materialized_view.indexes, new_materialized_view.indexes diff --git a/plugins/postgres/dbt/adapters/postgres/relation_configs/materialized_view.py b/plugins/postgres/dbt/adapters/postgres/relation_configs/materialized_view.py index 70a0805f79e..8a804cea518 100644 --- a/plugins/postgres/dbt/adapters/postgres/relation_configs/materialized_view.py +++ b/plugins/postgres/dbt/adapters/postgres/relation_configs/materialized_view.py @@ -1,5 +1,6 @@ from dataclasses import dataclass, field -from typing import Set, FrozenSet, List +from typing import Set, FrozenSet, List, Dict +from typing_extensions import Self import agate from dbt.adapters.relation_configs import ( @@ -8,7 +9,7 @@ RelationConfigValidationMixin, RelationConfigValidationRule, ) -from dbt.contracts.graph.nodes import ModelNode +from dbt.adapters.contracts.relation import RelationConfig from dbt.common.exceptions import DbtRuntimeError from dbt.adapters.postgres.relation_configs.constants import MAX_CHARACTERS_IN_IDENTIFIER @@ -54,7 +55,7 @@ def validation_rules(self) -> Set[RelationConfigValidationRule]: } @classmethod - def from_dict(cls, config_dict: dict) -> "PostgresMaterializedViewConfig": + def from_dict(cls, config_dict: dict) -> Self: kwargs_dict = { "table_name": config_dict.get("table_name"), "query": config_dict.get("query"), @@ -62,29 +63,27 @@ def from_dict(cls, config_dict: dict) -> "PostgresMaterializedViewConfig": PostgresIndexConfig.from_dict(index) for index in config_dict.get("indexes", {}) ), } - materialized_view: "PostgresMaterializedViewConfig" = super().from_dict(kwargs_dict) # type: ignore + materialized_view: Self = super().from_dict(kwargs_dict) # type: ignore return materialized_view @classmethod - def from_model_node(cls, model_node: ModelNode) -> "PostgresMaterializedViewConfig": - materialized_view_config = cls.parse_model_node(model_node) + def from_config(cls, relation_config: RelationConfig) -> Self: + materialized_view_config = cls.parse_config(relation_config) materialized_view = cls.from_dict(materialized_view_config) return materialized_view @classmethod - def parse_model_node(cls, model_node: ModelNode) -> dict: - indexes: List[dict] = model_node.config.extra.get("indexes", []) + def parse_config(cls, relation_config: RelationConfig) -> Dict: + indexes: List[dict] = relation_config.config.extra.get("indexes", []) config_dict = { - "table_name": model_node.identifier, - "query": model_node.compiled_code, + "table_name": relation_config.identifier, + "query": relation_config.compiled_code, "indexes": [PostgresIndexConfig.parse_model_node(index) for index in indexes], } return config_dict @classmethod - def from_relation_results( - cls, relation_results: RelationResults - ) -> "PostgresMaterializedViewConfig": + def from_relation_results(cls, relation_results: RelationResults) -> Self: materialized_view_config = cls.parse_relation_results(relation_results) materialized_view = cls.from_dict(materialized_view_config) return materialized_view diff --git a/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/alter.sql b/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/alter.sql index 7592f802639..ee53c113627 100644 --- a/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/alter.sql +++ b/plugins/postgres/dbt/include/postgres/macros/relations/materialized_view/alter.sql @@ -45,6 +45,6 @@ {% macro postgres__get_materialized_view_configuration_changes(existing_relation, new_config) %} {% set _existing_materialized_view = postgres__describe_materialized_view(existing_relation) %} - {% set _configuration_changes = existing_relation.get_materialized_view_config_change_collection(_existing_materialized_view, new_config) %} + {% set _configuration_changes = existing_relation.get_materialized_view_config_change_collection(_existing_materialized_view, new_config.model) %} {% do return(_configuration_changes) %} {% endmacro %}