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

Make grant_access_to thread safe #265

Closed
Closed
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
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20220811-103049.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Fixes
body: Fix bug where calls to `grant_access_to` from multiple threads could fail.
time: 2022-08-11T10:30:49.358387+01:00
custom:
Author: judahrand
Issue: "266"
PR: "265"
23 changes: 13 additions & 10 deletions dbt/adapters/bigquery/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import google.cloud.bigquery

from google.cloud.bigquery import AccessEntry, SchemaField
from google.cloud.bigquery.enums import EntityTypes

import time
import agate
Expand Down Expand Up @@ -811,21 +812,23 @@ def grant_access_to(self, entity, entity_type, role, grant_target_dict):
GrantTarget.validate(grant_target_dict)
grant_target = GrantTarget.from_dict(grant_target_dict)
dataset_ref = self.connections.dataset_ref(grant_target.project, grant_target.dataset)
dataset = client.get_dataset(dataset_ref)

if entity_type == "view":
if entity_type == EntityTypes.VIEW:
entity = self.get_table_ref_from_relation(entity).to_api_repr()

access_entry = AccessEntry(role, entity_type, entity)
access_entries = dataset.access_entries
with self.connections.lock:
dataset = client.get_dataset(dataset_ref)

if access_entry in access_entries:
logger.debug(f"Access entry {access_entry} " f"already exists in dataset")
return
access_entry = AccessEntry(role, entity_type, entity)
access_entries = dataset.access_entries

if access_entry in access_entries:
logger.debug(f"Access entry {access_entry} " f"already exists in dataset")
return

access_entries.append(AccessEntry(role, entity_type, entity))
dataset.access_entries = access_entries
client.update_dataset(dataset, ["access_entries"])
access_entries.append(AccessEntry(role, entity_type, entity))
dataset.access_entries = access_entries
client.update_dataset(dataset, ["access_entries"])

@available.parse_none
def get_dataset_location(self, relation):
Expand Down