From 8fd02ab80fb953e4d596876eda3cd53d5ee04cf6 Mon Sep 17 00:00:00 2001 From: Matthew McKnight Date: Tue, 23 Apr 2024 18:10:02 -0500 Subject: [PATCH 1/2] add fix from dbt-core 1.7.latest to remove materialized_view from renamtable type and test that we do the create and drop instead, and remove a unneeded semi colon --- .../unreleased/Fixes-20240423-180916.yaml | 6 ++ dbt/adapters/postgres/relation.py | 1 - dbt/include/postgres/macros/adapters.sql | 2 +- .../test_postgres_materialized_view.py | 63 +++++++++++++++++++ tests/unit/test_renamed_relations.py | 1 - 5 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 .changes/unreleased/Fixes-20240423-180916.yaml create mode 100644 tests/functional/materializations/materialized_view_tests/test_postgres_materialized_view.py diff --git a/.changes/unreleased/Fixes-20240423-180916.yaml b/.changes/unreleased/Fixes-20240423-180916.yaml new file mode 100644 index 00000000..48015bcb --- /dev/null +++ b/.changes/unreleased/Fixes-20240423-180916.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: remove materialized views from renambeable relation and remove a quote +time: 2024-04-23T18:09:16.865258-05:00 +custom: + Author: McKnight-42 + Issue: "127" diff --git a/dbt/adapters/postgres/relation.py b/dbt/adapters/postgres/relation.py index 05d55237..e546eab4 100644 --- a/dbt/adapters/postgres/relation.py +++ b/dbt/adapters/postgres/relation.py @@ -25,7 +25,6 @@ class PostgresRelation(BaseRelation): { RelationType.View, RelationType.Table, - RelationType.MaterializedView, } ) ) diff --git a/dbt/include/postgres/macros/adapters.sql b/dbt/include/postgres/macros/adapters.sql index ee864e9b..294443be 100644 --- a/dbt/include/postgres/macros/adapters.sql +++ b/dbt/include/postgres/macros/adapters.sql @@ -39,7 +39,7 @@ on {{ relation }} {% if index_config.type -%} using {{ index_config.type }} {%- endif %} - ({{ comma_separated_columns }}); + ({{ comma_separated_columns }}) {%- endmacro %} {% macro postgres__create_schema(relation) -%} diff --git a/tests/functional/materializations/materialized_view_tests/test_postgres_materialized_view.py b/tests/functional/materializations/materialized_view_tests/test_postgres_materialized_view.py new file mode 100644 index 00000000..6ed9858d --- /dev/null +++ b/tests/functional/materializations/materialized_view_tests/test_postgres_materialized_view.py @@ -0,0 +1,63 @@ +import pytest +from dbt.tests.util import run_dbt + +SEED = """ +order_id,customer_id,total_amount,order_date +1,101,50.00,2024-04-01 +2,102,75.00,2024-04-02 +3,103,100.00,2024-04-03 +4,101,30.00,2024-04-04 +5,104,45.00,2024-04-05 +""".strip() + +ORDERS = """ +-- models/orders.sql +{{ + config( + materialized='materialized_view' + ) +}} +SELECT + order_id, + customer_id, + total_amount, + order_date +FROM + {{ ref('source_orders') }} +""" + +PRODUCT_SALES = """ +{{ + config( + materialized='materialized_view' + ) +}} +SELECT + order_id, + SUM(total_amount) AS total_sales_amount +FROM + {{ ref('orders') }} +GROUP BY + order_id +""" + + +class TestPostgresTestRefreshMaterializedView: + """ + this test addresses a issue in postgres around materialized views, + and renaming against a model who has dependent models that are also materialized views + related pr: https://github.com/dbt-labs/dbt-core/pull/9959 + """ + + @pytest.fixture(scope="class") + def models(self): + yield {"orders.sql": ORDERS, "product_sales.sql": PRODUCT_SALES} + + @pytest.fixture(scope="class") + def seeds(self): + yield {"source_orders.csv": SEED} + + def test_postgres_refresh_dependent_naterialized_views(self, project): + run_dbt(["seed"]) + run_dbt(["run", "--full-refresh"]) + run_dbt(["run", "--full-refresh"]) \ No newline at end of file diff --git a/tests/unit/test_renamed_relations.py b/tests/unit/test_renamed_relations.py index 29bbabf2..5d2bcb04 100644 --- a/tests/unit/test_renamed_relations.py +++ b/tests/unit/test_renamed_relations.py @@ -13,6 +13,5 @@ def test_renameable_relation(): { RelationType.View, RelationType.Table, - RelationType.MaterializedView, } ) From 2a2a03d801242e2e98c263eeb99cf859e4318519 Mon Sep 17 00:00:00 2001 From: Matthew McKnight Date: Tue, 23 Apr 2024 18:21:09 -0500 Subject: [PATCH 2/2] empty line fix for pre-commit --- .../materialized_view_tests/test_postgres_materialized_view.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/functional/materializations/materialized_view_tests/test_postgres_materialized_view.py b/tests/functional/materializations/materialized_view_tests/test_postgres_materialized_view.py index 6ed9858d..4ee44bae 100644 --- a/tests/functional/materializations/materialized_view_tests/test_postgres_materialized_view.py +++ b/tests/functional/materializations/materialized_view_tests/test_postgres_materialized_view.py @@ -60,4 +60,4 @@ def seeds(self): def test_postgres_refresh_dependent_naterialized_views(self, project): run_dbt(["seed"]) run_dbt(["run", "--full-refresh"]) - run_dbt(["run", "--full-refresh"]) \ No newline at end of file + run_dbt(["run", "--full-refresh"])