Skip to content

Commit c35e20e

Browse files
committed
Fix get_table_names and get_view_names without a default dataset
1 parent 155e5b0 commit c35e20e

File tree

1 file changed

+25
-34
lines changed

1 file changed

+25
-34
lines changed

sqlalchemy_bigquery/base.py

+25-34
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
import datetime
2323
from decimal import Decimal
2424
import random
25-
import operator
2625
import uuid
2726

2827
from google import auth
29-
import google.api_core.exceptions
3028
from google.cloud.bigquery import dbapi
3129
from google.cloud.bigquery.table import (
3230
RangePartitioning,
@@ -1047,11 +1045,6 @@ def dbapi(cls):
10471045
def import_dbapi(cls):
10481046
return dbapi
10491047

1050-
@staticmethod
1051-
def _build_formatted_table_id(table):
1052-
"""Build '<dataset_id>.<table_id>' string using given table."""
1053-
return "{}.{}".format(table.reference.dataset_id, table.table_id)
1054-
10551048
@staticmethod
10561049
def _add_default_dataset_to_job_config(job_config, project_id, dataset_id):
10571050
# If dataset_id is set, then we know the job_config isn't None
@@ -1100,36 +1093,34 @@ def create_connect_args(self, url):
11001093
)
11011094
return ([], {"client": client})
11021095

1103-
def _get_table_or_view_names(self, connection, item_types, schema=None):
1104-
current_schema = schema or self.dataset_id
1105-
get_table_name = (
1106-
self._build_formatted_table_id
1107-
if self.dataset_id is None
1108-
else operator.attrgetter("table_id")
1109-
)
1096+
def _get_default_schema_name(self, connection) -> str:
1097+
return connection.dialect.dataset_id
11101098

1099+
def _get_table_or_view_names(self, connection, item_types, schema=None):
11111100
client = connection.connection._client
1112-
datasets = client.list_datasets()
1113-
1114-
result = []
1115-
for dataset in datasets:
1116-
if current_schema is not None and current_schema != dataset.dataset_id:
1117-
continue
1118-
1119-
try:
1120-
tables = client.list_tables(
1121-
dataset.reference, page_size=self.list_tables_page_size
1101+
# `schema=None` means to search the default schema. If one isn't set in the
1102+
# connection string, then we have nothing to search so return an empty list.
1103+
#
1104+
# When using Alembic with `include_schemas=False`, it expects to compare to a
1105+
# single schema. If `include_schemas=True`, it will enumerate all schemas and
1106+
# then call `get_table_names`/`get_view_names` for each schema.
1107+
current_schema = schema or self.default_schema_name
1108+
if current_schema is None:
1109+
return []
1110+
try:
1111+
return [
1112+
table.table_id
1113+
for table in client.list_tables(
1114+
current_schema, page_size=self.list_tables_page_size
11221115
)
1123-
for table in tables:
1124-
if table.table_type in item_types:
1125-
result.append(get_table_name(table))
1126-
except google.api_core.exceptions.NotFound:
1127-
# It's possible that the dataset was deleted between when we
1128-
# fetched the list of datasets and when we try to list the
1129-
# tables from it. See:
1130-
# https://github.com/googleapis/python-bigquery-sqlalchemy/issues/105
1131-
pass
1132-
return result
1116+
if table.table_type in item_types
1117+
]
1118+
except NotFound:
1119+
# It's possible that the dataset was deleted between when we
1120+
# fetched the list of datasets and when we try to list the
1121+
# tables from it. See:
1122+
# https://github.com/googleapis/python-bigquery-sqlalchemy/issues/105
1123+
pass
11331124

11341125
@staticmethod
11351126
def _split_table_name(full_table_name):

0 commit comments

Comments
 (0)