Skip to content

Commit

Permalink
Add factory wrappers to renamed_relations (#9682)
Browse files Browse the repository at this point in the history
* Add factory wrappers to renamed_relations
* add test and postgres semantics

---------

Co-authored-by: Mila Page <[email protected]>
Co-authored-by: Mike Alfare <[email protected]>
  • Loading branch information
3 people authored Mar 21, 2024
1 parent a0372fc commit 27e17a7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20240226-215842.yaml
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 2 additions & 2 deletions core/dbt/adapters/base/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 17 additions & 12 deletions plugins/postgres/dbt/adapters/postgres/relation.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_renamed_relations.py
Original file line number Diff line number Diff line change
@@ -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,
}
)

0 comments on commit 27e17a7

Please sign in to comment.