Skip to content
Merged
Prev Previous commit
Next Next commit
update renameable relations to a config
  • Loading branch information
mikealfare committed Aug 17, 2023
commit 4483f6ca78e7c5d2b78fcfe05a501cc8046a147b
6 changes: 3 additions & 3 deletions core/dbt/adapters/base/relation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections.abc import Hashable
from dataclasses import dataclass, field
from typing import Optional, TypeVar, Any, Type, Dict, Iterator, Tuple, Set
from typing import Optional, TypeVar, Any, Type, Dict, Iterator, Tuple, Set, List

from dbt.contracts.graph.nodes import SourceDefinition, ManifestNode, ResultNode, ParsedNode
from dbt.contracts.relation import (
Expand Down Expand Up @@ -35,6 +35,7 @@ class BaseRelation(FakeAPIObject, Hashable):
include_policy: Policy = field(default_factory=lambda: Policy())
quote_policy: Policy = field(default_factory=lambda: Policy())
dbt_created: bool = False
relations_that_can_be_renamed: List[RelationType] = field(default_factory=list)

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 Expand Up @@ -169,7 +170,6 @@ def without_identifier(self) -> "BaseRelation":
return self.include(identifier=False).replace_path(identifier=None)

def _render_iterator(self) -> Iterator[Tuple[Optional[ComponentName], Optional[str]]]:

for key in ComponentName:
path_part: Optional[str] = None
if self.include_policy.get_part(key):
Expand Down Expand Up @@ -288,7 +288,7 @@ def create(

@property
def can_be_renamed(self):
return any([self.is_view, self.is_table, self.is_materialized_view])
return self.type in self.relations_that_can_be_renamed

def __repr__(self) -> str:
return "<{} {}>".format(self.__class__.__name__, self.render())
Expand Down
7 changes: 7 additions & 0 deletions plugins/postgres/dbt/adapters/postgres/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
RelationResults,
)
from dbt.context.providers import RuntimeConfigObject
from dbt.contracts.relation import RelationType
from dbt.exceptions import DbtRuntimeError

from dbt.adapters.postgres.relation_configs import (
Expand All @@ -20,6 +21,12 @@

@dataclass(frozen=True, eq=False, repr=False)
class PostgresRelation(BaseRelation):
relations_that_can_be_renamed = [
RelationType.View,
RelationType.Table,
RelationType.MaterializedView,
]

def __post_init__(self):
# Check for length of Postgres table/view names.
# Check self.type to exclude test relation identifiers
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_relation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from dataclasses import replace

import pytest

from dbt.adapters.base import BaseRelation
from dbt.contracts.relation import RelationType


@pytest.mark.parametrize(
"relation_type,result",
[
(RelationType.View, True),
(RelationType.Table, False),
],
)
def test_can_be_renamed(relation_type, result):
my_relation = BaseRelation.create(type=relation_type)
my_relation = replace(my_relation, relations_that_can_be_renamed=[RelationType.View])
assert my_relation.can_be_renamed is result