Repository: dbt-labs/dbt-redshift
Pull request #737 (2024-03-21 19:00:23)

[Backport 1.7.latest] Fix the renamed relations code
Backport c99c73bd0386e2641c825b8e36c5bc0229cd3258 from #723.


Patch:
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 00000000..48b8de8e
--- /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 182b9b81..0decee53 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 b8468052..48def28b 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 0c6cdcdf..a96b0445 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 25a9d8b3..7ae89ab4 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 %}


Test patch:
diff --git a/tests/unit/test_renamed_relations.py b/tests/unit/test_renamed_relations.py
new file mode 100644
index 00000000..feb41d0e
--- /dev/null
+++ b/tests/unit/test_renamed_relations.py
@@ -0,0 +1,18 @@
+from dbt.contracts.relation import RelationType
+
+from dbt.adapters.redshift.relation import RedshiftRelation
+
+
+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,
+        }
+    )