From b38b192f3f4913d337bd69e3a47ce25ea5245d4b Mon Sep 17 00:00:00 2001 From: Mila Page <67295367+versusfacit@users.noreply.github.com> Date: Thu, 21 Mar 2024 14:54:54 -0400 Subject: [PATCH] Fix the renamed relations code (#723) * Add test * Add FrozenSet to imports * removed extraneous semicolons * update alter table to alter view for rename --------- Co-authored-by: Mila Page Co-authored-by: Mike Alfare <13974384+mikealfare@users.noreply.github.com> Co-authored-by: Mike Alfare (cherry picked from commit c99c73bd0386e2641c825b8e36c5bc0229cd3258) --- .../Under the Hood-20240227-002713.yaml | 6 +++++ dbt/adapters/redshift/relation.py | 26 +++++++++++-------- .../relations/materialized_view/create.sql | 2 +- .../redshift/macros/relations/view/rename.sql | 2 +- .../macros/relations/view/replace.sql | 2 +- tests/unit/test_renamed_relations.py | 17 ++++++++++++ 6 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 .changes/unreleased/Under the Hood-20240227-002713.yaml create mode 100644 tests/unit/test_renamed_relations.py diff --git a/.changes/unreleased/Under the Hood-20240227-002713.yaml b/.changes/unreleased/Under the Hood-20240227-002713.yaml new file mode 100644 index 000000000..48b8de8e4 --- /dev/null +++ b/.changes/unreleased/Under the Hood-20240227-002713.yaml @@ -0,0 +1,6 @@ +kind: Under the Hood +body: Add unit test for transaction semantics. +time: 2024-02-27T00:27:13.299107-08:00 +custom: + Author: versusfacit + Issue: "722" diff --git a/dbt/adapters/redshift/relation.py b/dbt/adapters/redshift/relation.py index 182b9b810..0decee539 100644 --- a/dbt/adapters/redshift/relation.py +++ b/dbt/adapters/redshift/relation.py @@ -1,5 +1,5 @@ -from dataclasses import dataclass -from typing import Optional +from dataclasses import dataclass, field +from typing import FrozenSet, Optional from dbt.adapters.base.relation import BaseRelation from dbt.adapters.relation_configs import ( @@ -32,16 +32,20 @@ class RedshiftRelation(BaseRelation): relation_configs = { RelationType.MaterializedView.value: RedshiftMaterializedViewConfig, } - renameable_relations = frozenset( - { - RelationType.View, - RelationType.Table, - } + renameable_relations: FrozenSet[RelationType] = field( + default_factory=lambda: frozenset( + { + RelationType.View, + RelationType.Table, + } + ) ) - replaceable_relations = frozenset( - { - RelationType.View, - } + replaceable_relations: FrozenSet[RelationType] = field( + default_factory=lambda: frozenset( + { + RelationType.View, + } + ) ) def __post_init__(self): diff --git a/dbt/include/redshift/macros/relations/materialized_view/create.sql b/dbt/include/redshift/macros/relations/materialized_view/create.sql index b84680525..48def28b1 100644 --- a/dbt/include/redshift/macros/relations/materialized_view/create.sql +++ b/dbt/include/redshift/macros/relations/materialized_view/create.sql @@ -10,6 +10,6 @@ auto refresh {% if materialized_view.autorefresh %}yes{% else %}no{% endif %} as ( {{ materialized_view.query }} - ); + ) {% endmacro %} diff --git a/dbt/include/redshift/macros/relations/view/rename.sql b/dbt/include/redshift/macros/relations/view/rename.sql index 0c6cdcdfa..a96b04451 100644 --- a/dbt/include/redshift/macros/relations/view/rename.sql +++ b/dbt/include/redshift/macros/relations/view/rename.sql @@ -1,3 +1,3 @@ {% macro redshift__get_rename_view_sql(relation, new_name) %} - alter view {{ relation }} rename to {{ new_name }} + alter table {{ relation }} rename to {{ new_name }} {% endmacro %} diff --git a/dbt/include/redshift/macros/relations/view/replace.sql b/dbt/include/redshift/macros/relations/view/replace.sql index 25a9d8b38..7ae89ab45 100644 --- a/dbt/include/redshift/macros/relations/view/replace.sql +++ b/dbt/include/redshift/macros/relations/view/replace.sql @@ -13,6 +13,6 @@ {{ get_assert_columns_equivalent(sql) }} {%- endif %} as ( {{ sql }} - ) {{ bind_qualifier }}; + ) {{ bind_qualifier }} {%- endmacro %} diff --git a/tests/unit/test_renamed_relations.py b/tests/unit/test_renamed_relations.py new file mode 100644 index 000000000..4817ab100 --- /dev/null +++ b/tests/unit/test_renamed_relations.py @@ -0,0 +1,17 @@ +from dbt.adapters.redshift.relation import RedshiftRelation +from dbt.adapters.contracts.relation import RelationType + + +def test_renameable_relation(): + relation = RedshiftRelation.create( + database="my_db", + schema="my_schema", + identifier="my_table", + type=RelationType.Table, + ) + assert relation.renameable_relations == frozenset( + { + RelationType.View, + RelationType.Table, + } + )