Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable authorization of materialized views #788

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dbt-bigquery/.changes/unreleased/Fixes-20250202-105422.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Fix silently ignoring materialized view authorization
time: 2025-02-02T10:54:22.12198+01:00
custom:
Author: kubikb
Issue: "1471"
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% materialization materialized_view, adapter='bigquery' -%}

{% set relations = materialization_materialized_view_default() %}

{% if 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) }}

{%- endmaterialization %}
43 changes: 40 additions & 3 deletions dbt-bigquery/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,16 +93,35 @@ 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):
def test_grant_access_succeeds(
self, project, setup_grant_schema, teardown_grant_schema, unique_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"])
# Materialized view excluded since it would produce an error since source table is replaced
results = run_dbt(["run", "--exclude", "select_1_materialized_view"])
assert len(results) == 2

with project.adapter.connection_named("__test_grants"):
client = project.adapter.connections.get_thread_connection().handle
dataset_name = get_schema_name(unique_schema)
dataset_id = "{}.{}".format("dbt-test-env", dataset_name)
bq_dataset = client.get_dataset(dataset_id)

authorized_view_names = []
for access_entry in bq_dataset.access_entries:
if access_entry.entity_type != "view":
continue

authorized_view_names.append(access_entry.entity_id["tableId"])

assert set(authorized_view_names) == set(["select_1", "select_1_materialized_view"])


class TestAccessGrantFails:
@pytest.fixture(scope="class")
Expand Down
Loading