From 91ec79503d1b637160392437fa4d3db656bbedde Mon Sep 17 00:00:00 2001 From: Mila Page Date: Tue, 27 Feb 2024 00:34:08 -0800 Subject: [PATCH 1/8] Add test --- .../Under the Hood-20240227-002713.yaml | 6 +++++ dbt/adapters/redshift/relation.py | 24 +++++++++++-------- dev-requirements.txt | 2 +- tests/unit/test_renamed_relations.py | 17 +++++++++++++ 4 files changed, 38 insertions(+), 11 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 7255288e2..b59bc6381 100644 --- a/dbt/adapters/redshift/relation.py +++ b/dbt/adapters/redshift/relation.py @@ -1,4 +1,4 @@ -from dataclasses import dataclass +from dataclasses import dataclass, field from dbt.adapters.contracts.relation import RelationConfig from typing import Optional @@ -30,16 +30,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/dev-requirements.txt b/dev-requirements.txt index 6d29276cf..891995b85 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,6 +1,6 @@ # install latest changes in dbt-core + dbt-postgres git+https://github.com/dbt-labs/dbt-adapters.git -git+https://github.com/dbt-labs/dbt-adapters.git#subdirectory=dbt-tests-adapter +git+https://github.com/dbt-labs/dbt-adapters.git@ADAP-108/fix_renamed_relations_for_1.8#subdirectory=dbt-tests-adapter git+https://github.com/dbt-labs/dbt-common.git git+https://github.com/dbt-labs/dbt-core.git#subdirectory=core git+https://github.com/dbt-labs/dbt-postgres.git 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, + } + ) From 67683b27f397bfe549aea744d6f2c3186acb11c1 Mon Sep 17 00:00:00 2001 From: Mila Page Date: Tue, 27 Feb 2024 01:12:28 -0800 Subject: [PATCH 2/8] Add FrozenSet to imports --- dbt/adapters/redshift/relation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbt/adapters/redshift/relation.py b/dbt/adapters/redshift/relation.py index b59bc6381..e120a2fac 100644 --- a/dbt/adapters/redshift/relation.py +++ b/dbt/adapters/redshift/relation.py @@ -1,6 +1,6 @@ from dataclasses import dataclass, field from dbt.adapters.contracts.relation import RelationConfig -from typing import Optional +from typing import FrozenSet, Optional from dbt.adapters.base.relation import BaseRelation from dbt.adapters.relation_configs import ( From 9b0f7596156452df26aeba1ced4245826f2f5ae4 Mon Sep 17 00:00:00 2001 From: Mila Page Date: Wed, 28 Feb 2024 10:35:53 -0800 Subject: [PATCH 3/8] Fix the dev requirements for testing. --- dev-requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index 891995b85..d0ca8205e 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,6 +1,6 @@ # install latest changes in dbt-core + dbt-postgres -git+https://github.com/dbt-labs/dbt-adapters.git -git+https://github.com/dbt-labs/dbt-adapters.git@ADAP-108/fix_renamed_relations_for_1.8#subdirectory=dbt-tests-adapter +git+https://github.com/dbt-labs/dbt-adapters.git@ADAP-108/fix_renamed_relations_for_1.8 +git+https://github.com/dbt-labs/dbt-adapters.git#subdirectory=dbt-tests-adapter git+https://github.com/dbt-labs/dbt-common.git git+https://github.com/dbt-labs/dbt-core.git#subdirectory=core git+https://github.com/dbt-labs/dbt-postgres.git From 018a148f4d404db27a54f78ea990bbcd7cf2049e Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Wed, 20 Mar 2024 13:47:10 -0400 Subject: [PATCH 4/8] temporarily point to the fix branches for dev --- dev-requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index d0ca8205e..eaa70658f 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,6 +1,6 @@ # install latest changes in dbt-core + dbt-postgres git+https://github.com/dbt-labs/dbt-adapters.git@ADAP-108/fix_renamed_relations_for_1.8 -git+https://github.com/dbt-labs/dbt-adapters.git#subdirectory=dbt-tests-adapter +git+https://github.com/dbt-labs/dbt-adapters.git@ADAP-108/fix_renamed_relations_for_1.8#subdirectory=dbt-tests-adapter git+https://github.com/dbt-labs/dbt-common.git git+https://github.com/dbt-labs/dbt-core.git#subdirectory=core git+https://github.com/dbt-labs/dbt-postgres.git From f9ab5db49e00e8dd769e7e3cb04b3e1d7454a92f Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Wed, 20 Mar 2024 17:18:08 -0400 Subject: [PATCH 5/8] removed extraneous semicolons --- .../redshift/macros/relations/materialized_view/create.sql | 2 +- dbt/include/redshift/macros/relations/view/replace.sql | 2 +- .../adapter/materialized_view_tests/test_materialized_views.py | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/dbt/include/redshift/macros/relations/materialized_view/create.sql b/dbt/include/redshift/macros/relations/materialized_view/create.sql index 06fe2b6b5..1b81992e4 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/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/functional/adapter/materialized_view_tests/test_materialized_views.py b/tests/functional/adapter/materialized_view_tests/test_materialized_views.py index 64f697e77..6eeab362a 100644 --- a/tests/functional/adapter/materialized_view_tests/test_materialized_views.py +++ b/tests/functional/adapter/materialized_view_tests/test_materialized_views.py @@ -75,6 +75,9 @@ def test_materialized_view_create_idempotent(self, project, my_materialized_view ) assert self.query_relation_type(project, my_materialized_view) == "materialized_view" + def test_materialized_view_replaces_table(self, project, my_table): + super().test_materialized_view_replaces_table(project, my_table) + class RedshiftMaterializedViewChanges(MaterializedViewChanges): @pytest.fixture(scope="class", autouse=True) From abba63c9e79f19a4f29c5ad5723d30a2a5d7f6fa Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Wed, 20 Mar 2024 17:20:21 -0400 Subject: [PATCH 6/8] remove troubleshooting artifacts --- .../adapter/materialized_view_tests/test_materialized_views.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/functional/adapter/materialized_view_tests/test_materialized_views.py b/tests/functional/adapter/materialized_view_tests/test_materialized_views.py index 6eeab362a..64f697e77 100644 --- a/tests/functional/adapter/materialized_view_tests/test_materialized_views.py +++ b/tests/functional/adapter/materialized_view_tests/test_materialized_views.py @@ -75,9 +75,6 @@ def test_materialized_view_create_idempotent(self, project, my_materialized_view ) assert self.query_relation_type(project, my_materialized_view) == "materialized_view" - def test_materialized_view_replaces_table(self, project, my_table): - super().test_materialized_view_replaces_table(project, my_table) - class RedshiftMaterializedViewChanges(MaterializedViewChanges): @pytest.fixture(scope="class", autouse=True) From f1b7e6615caf740faf088827389f41dca78541ca Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Wed, 20 Mar 2024 18:50:11 -0400 Subject: [PATCH 7/8] update alter table to alter view for rename --- dbt/include/redshift/macros/relations/view/rename.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 %} From 45b123b9ab57387736efbe2e6c4ab7a8be447de3 Mon Sep 17 00:00:00 2001 From: Mike Alfare Date: Thu, 21 Mar 2024 10:28:33 -0400 Subject: [PATCH 8/8] repoint dev deps to main --- dev-requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index caafbc3e3..079a56062 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,6 +1,6 @@ # install latest changes in dbt-core + dbt-postgres -git+https://github.com/dbt-labs/dbt-adapters.git@ADAP-108/fix_renamed_relations_for_1.8 -git+https://github.com/dbt-labs/dbt-adapters.git@ADAP-108/fix_renamed_relations_for_1.8#subdirectory=dbt-tests-adapter +git+https://github.com/dbt-labs/dbt-adapters.git +git+https://github.com/dbt-labs/dbt-adapters.git#subdirectory=dbt-tests-adapter git+https://github.com/dbt-labs/dbt-common.git git+https://github.com/dbt-labs/dbt-core.git#subdirectory=core git+https://github.com/dbt-labs/dbt-postgres.git