Skip to content

Commit

Permalink
Add test to grant access to materialized view, extend table materiali…
Browse files Browse the repository at this point in the history
…zation SQL
  • Loading branch information
kubikb committed Feb 2, 2025
1 parent a219818 commit d9a609d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions dbt/include/bigquery/macros/materializations/table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
23 changes: 21 additions & 2 deletions tests/functional/adapter/test_grant_access_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit d9a609d

Please sign in to comment.