Skip to content

Commit 2a41ed7

Browse files
authored
Merge pull request #59 from dimkonko/fix_dataset_deprecation_warning
ISSUE-58: Remove usage of deprecated Client.dataset
2 parents 9926598 + 2685218 commit 2a41ed7

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

pybigquery/sqlalchemy_bigquery.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
from google.cloud import bigquery
88
from google.cloud.bigquery import dbapi, QueryJobConfig
99
from google.cloud.bigquery.schema import SchemaField
10-
from google.cloud.bigquery.table import EncryptionConfiguration
11-
from google.cloud.bigquery.dataset import DatasetReference
10+
from google.cloud.bigquery.table import EncryptionConfiguration, TableReference
1211
from google.oauth2 import service_account
1312
from google.api_core.exceptions import NotFound
1413
from sqlalchemy.exc import NoSuchTableError
@@ -350,7 +349,8 @@ def _json_deserializer(self, row):
350349
"""
351350
return row
352351

353-
def _split_table_name(self, full_table_name):
352+
@staticmethod
353+
def _split_table_name(full_table_name):
354354
# Split full_table_name to get project, dataset and table name
355355
dataset = None
356356
table_name = None
@@ -370,19 +370,20 @@ def _get_table(self, connection, table_name, schema=None):
370370
if isinstance(connection, Engine):
371371
connection = connection.connect()
372372

373-
project, dataset, table_name_prepared = self._split_table_name(table_name)
374-
if dataset is None:
375-
if schema is not None:
376-
dataset = schema
377-
elif self.dataset_id:
378-
dataset = self.dataset_id
373+
client = connection.connection._client
374+
375+
project_id, dataset_id, table_id = self._split_table_name(table_name)
376+
project_id = project_id or client.project
377+
dataset_id = dataset_id or schema or self.dataset_id
379378

380-
table = connection.connection._client.dataset(dataset, project=project).table(table_name_prepared)
379+
table_ref = TableReference.from_string("{}.{}.{}".format(
380+
project_id, dataset_id, table_id
381+
))
381382
try:
382-
t = connection.connection._client.get_table(table)
383-
except NotFound as e:
383+
table = client.get_table(table_ref)
384+
except NotFound:
384385
raise NoSuchTableError(table_name)
385-
return t
386+
return table
386387

387388
def has_table(self, connection, table_name, schema=None):
388389
try:

test/test_sqlalchemy_bigquery.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,8 @@ def test_get_columns(inspector, inspector_using_test_dataset):
480480
def test_has_table(engine, engine_using_test_dataset):
481481
assert engine.has_table('sample', 'test_pybigquery') is True
482482
assert engine.has_table('test_pybigquery.sample') is True
483+
assert engine.has_table('test_pybigquery.nonexistent_table') is False
484+
assert engine.has_table('nonexistent_table', 'nonexistent_dataset') is False
483485

484486
assert engine.has_table('sample_alt', 'test_pybigquery_alt') is True
485487
assert engine.has_table('test_pybigquery_alt.sample_alt') is True

0 commit comments

Comments
 (0)