From d9a609d255fc7cb483eab3c8242eed0846481922 Mon Sep 17 00:00:00 2001 From: kubikb Date: Sun, 2 Feb 2025 10:50:28 +0100 Subject: [PATCH] Add test to grant access to materialized view, extend table materialization SQL --- .../macros/materializations/table.sql | 6 +++++ .../adapter/test_grant_access_to.py | 23 +++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/dbt/include/bigquery/macros/materializations/table.sql b/dbt/include/bigquery/macros/materializations/table.sql index 41bb69770..25f944c5c 100644 --- a/dbt/include/bigquery/macros/materializations/table.sql +++ b/dbt/include/bigquery/macros/materializations/table.sql @@ -41,6 +41,12 @@ {% do persist_docs(target_relation, model) %} + {% if config.get('materialized') == "materialized_view" and config.get('grant_access_to') %} + {% for grant_target_dict in config.get('grant_access_to') %} + {% do adapter.grant_access_to(this, 'view', None, grant_target_dict) %} + {% endfor %} + {% endif %} + {{ return({'relations': [target_relation]}) }} {% endmaterialization %} diff --git a/tests/functional/adapter/test_grant_access_to.py b/tests/functional/adapter/test_grant_access_to.py index 633cebe92..bcc49c310 100644 --- a/tests/functional/adapter/test_grant_access_to.py +++ b/tests/functional/adapter/test_grant_access_to.py @@ -21,6 +21,24 @@ def select_1(dataset: str, materialized: str): ) +def select_1_materialized_view(dataset: str): + config = f"""config( + materialized='materialized_view', + grant_access_to=[ + {{'project': 'dbt-test-env', 'dataset': '{dataset}'}}, + ] + )""" + return ( + "{{" + + config + + "}}" + + """ + SELECT one, COUNT(1) AS count_one + FROM {{ ref('select_1_table') }} + GROUP BY one""" + ) + + BAD_CONFIG_TABLE_NAME = "bad_view" BAD_CONFIG_TABLE = """ {{ config( @@ -75,15 +93,16 @@ def models(self, unique_schema): return { "select_1.sql": select_1(dataset=dataset, materialized="view"), "select_1_table.sql": select_1(dataset=dataset, materialized="table"), + "select_1_materialized_view.sql": select_1_materialized_view(dataset=dataset), } def test_grant_access_succeeds(self, project, setup_grant_schema, teardown_grant_schema): # Need to run twice to validate idempotency results = run_dbt(["run"]) - assert len(results) == 2 + assert len(results) == 3 time.sleep(10) results = run_dbt(["run"]) - assert len(results) == 2 + assert len(results) == 3 class TestAccessGrantFails: