From 27e17a788abfe0fa233b5c12db8e3fa866b06c70 Mon Sep 17 00:00:00 2001 From: Mila Page <67295367+VersusFacit@users.noreply.github.com> Date: Thu, 21 Mar 2024 12:10:57 -0700 Subject: [PATCH] Add factory wrappers to renamed_relations (#9682) * Add factory wrappers to renamed_relations * add test and postgres semantics --------- Co-authored-by: Mila Page Co-authored-by: Mike Alfare <13974384+mikealfare@users.noreply.github.com> --- .../unreleased/Fixes-20240226-215842.yaml | 6 ++++ core/dbt/adapters/base/relation.py | 4 +-- .../dbt/adapters/postgres/relation.py | 29 +++++++++++-------- tests/unit/test_renamed_relations.py | 18 ++++++++++++ 4 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 .changes/unreleased/Fixes-20240226-215842.yaml create mode 100644 tests/unit/test_renamed_relations.py diff --git a/.changes/unreleased/Fixes-20240226-215842.yaml b/.changes/unreleased/Fixes-20240226-215842.yaml new file mode 100644 index 00000000000..b170c4cf44d --- /dev/null +++ b/.changes/unreleased/Fixes-20240226-215842.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Add field wrapper to BaseRelation members that were missing it. +time: 2024-02-26T21:58:42.161273-08:00 +custom: + Author: versusfacit + Issue: "9681" diff --git a/core/dbt/adapters/base/relation.py b/core/dbt/adapters/base/relation.py index 67a50d9061f..1cdce317c3f 100644 --- a/core/dbt/adapters/base/relation.py +++ b/core/dbt/adapters/base/relation.py @@ -41,13 +41,13 @@ class BaseRelation(FakeAPIObject, Hashable): # adding a relation type here also requires defining the associated rename macro # e.g. adding RelationType.View in dbt-postgres requires that you define: # include/postgres/macros/relations/view/rename.sql::postgres__get_rename_view_sql() - renameable_relations: SerializableIterable = () + renameable_relations: SerializableIterable = field(default_factory=frozenset) # register relation types that are atomically replaceable, e.g. they have "create or replace" syntax # adding a relation type here also requires defining the associated replace macro # e.g. adding RelationType.View in dbt-postgres requires that you define: # include/postgres/macros/relations/view/replace.sql::postgres__get_replace_view_sql() - replaceable_relations: SerializableIterable = () + replaceable_relations: SerializableIterable = field(default_factory=frozenset) def _is_exactish_match(self, field: ComponentName, value: str) -> bool: if self.dbt_created and self.quote_policy.get_part(field) is False: diff --git a/plugins/postgres/dbt/adapters/postgres/relation.py b/plugins/postgres/dbt/adapters/postgres/relation.py index e6a302d4143..e722f05bedc 100644 --- a/plugins/postgres/dbt/adapters/postgres/relation.py +++ b/plugins/postgres/dbt/adapters/postgres/relation.py @@ -1,4 +1,4 @@ -from dataclasses import dataclass +from dataclasses import dataclass, field from typing import Optional, Set, FrozenSet from dbt.adapters.base.relation import BaseRelation @@ -21,18 +21,23 @@ @dataclass(frozen=True, eq=False, repr=False) class PostgresRelation(BaseRelation): - renameable_relations = frozenset( - { - RelationType.View, - RelationType.Table, - RelationType.MaterializedView, - } + renameable_relations: FrozenSet[RelationType] = field( + default_factory=lambda: frozenset( + { + RelationType.View, + RelationType.Table, + RelationType.MaterializedView, + } + ) ) - replaceable_relations = frozenset( - { - RelationType.View, - RelationType.Table, - } + + replaceable_relations: FrozenSet[RelationType] = field( + default_factory=lambda: frozenset( + { + RelationType.View, + RelationType.Table, + } + ) ) def __post_init__(self): diff --git a/tests/unit/test_renamed_relations.py b/tests/unit/test_renamed_relations.py new file mode 100644 index 00000000000..20f2453e089 --- /dev/null +++ b/tests/unit/test_renamed_relations.py @@ -0,0 +1,18 @@ +from dbt.adapters.postgres.relation import PostgresRelation +from dbt.contracts.relation import RelationType + + +def test_renameable_relation(): + relation = PostgresRelation.create( + database="my_db", + schema="my_schema", + identifier="my_table", + type=RelationType.Table, + ) + assert relation.renameable_relations == frozenset( + { + RelationType.View, + RelationType.Table, + RelationType.MaterializedView, + } + )