Repository: googleapis/google-cloud-python

Issue:
BigQuery .exist method in new version 0.28.0
Hello,

I used to use the following condition in version 0.26.0 of google-cloud-bigquery:
`if dataset.table(my_table_name).exists():
`      

I get the following error message when running it with the newer version 0.28.0:

> AttributeError: 'TableReference' object has no attribute 'exists' 

What is the equivalent of the .exist() method in version 0.28.0 ?

Environment: Python 2.7.10, macOS Sierra 10.12.6, google-cloud 0.31.0

Many thanks


Discussion:
That's correct. We removed this method, since all it was doing was an API call to get a table and then discarding the response. The equivalent in 0.28.0 is to call `get_table` and catch the `NotFound` error.

https://github.com/GoogleCloudPlatform/google-cloud-python/blob/fbe4e243d8cf03d4ffc60c7b19e86a0166e44514/bigquery/tests/system.py#L1517-L1523

I see that I overlooked this method when creating the [migration guide](https://cloud.google.com/bigquery/docs/python-client-migration#table_code_changes). I'll leave this issue open until we publish a sample for this method.

Resolving patch:
diff --git a/docs/bigquery/snippets.py b/docs/bigquery/snippets.py
--- a/docs/bigquery/snippets.py
+++ b/docs/bigquery/snippets.py
@@ -130,6 +130,41 @@ def test_get_dataset(client, to_delete):
     # [END get_dataset]
 
 
+# [START bigquery_dataset_exists]
+def dataset_exists(client, dataset_reference):
+    """Return if a dataset exists.
+
+    Args:
+        client (google.cloud.bigquery.client.Client):
+            A client to connect to the BigQuery API.
+        dataset_reference (google.cloud.bigquery.dataset.DatasetReference):
+            A reference to the dataset to look for.
+
+    Returns:
+        bool: ``True`` if the dataset exists, ``False`` otherwise.
+    """
+    from google.cloud.exceptions import NotFound
+
+    try:
+        client.get_dataset(dataset_reference)
+        return True
+    except NotFound:
+        return False
+# [END bigquery_dataset_exists]
+
+
+def test_dataset_exists(client, to_delete):
+    """Determine if a dataset exists."""
+    DATASET_ID = 'get_table_dataset_{}'.format(_millis())
+    dataset_ref = client.dataset(DATASET_ID)
+    dataset = bigquery.Dataset(dataset_ref)
+    dataset = client.create_dataset(dataset)
+    to_delete.append(dataset)
+
+    assert dataset_exists(client, dataset_ref)
+    assert not dataset_exists(client, client.dataset('i_dont_exist'))
+
+
 def test_update_dataset_simple(client, to_delete):
     """Update a dataset's metadata."""
     DATASET_ID = 'update_dataset_simple_{}'.format(_millis())
@@ -279,6 +314,46 @@ def test_get_table(client, to_delete):
     # [END get_table]
 
 
+# [START bigquery_table_exists]
+def table_exists(client, table_reference):
+    """Return if a table exists.
+
+    Args:
+        client (google.cloud.bigquery.client.Client):
+            A client to connect to the BigQuery API.
+        table_reference (google.cloud.bigquery.table.TableReference):
+            A reference to the table to look for.
+
+    Returns:
+        bool: ``True`` if the table exists, ``False`` otherwise.
+    """
+    from google.cloud.exceptions import NotFound
+
+    try:
+        client.get_table(table_reference)
+        return True
+    except NotFound:
+        return False
+# [END bigquery_table_exists]
+
+
+def test_table_exists(client, to_delete):
+    """Determine if a table exists."""
+    DATASET_ID = 'get_table_dataset_{}'.format(_millis())
+    TABLE_ID = 'get_table_table_{}'.format(_millis())
+    dataset = bigquery.Dataset(client.dataset(DATASET_ID))
+    dataset = client.create_dataset(dataset)
+    to_delete.append(dataset)
+
+    table_ref = dataset.table(TABLE_ID)
+    table = bigquery.Table(table_ref, schema=SCHEMA)
+    table = client.create_table(table)
+    to_delete.insert(0, table)
+
+    assert table_exists(client, table_ref)
+    assert not table_exists(client, dataset.table('i_dont_exist'))
+
+
 def test_update_table_simple(client, to_delete):
     """Patch a table's metadata."""
     DATASET_ID = 'update_table_simple_dataset_{}'.format(_millis())


Test patch: