Skip to content

Commit f16fff4

Browse files
committed
Spanner: Fix database not found error, Closes googleapis#4071
1 parent a62931a commit f16fff4

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

spanner/google/cloud/spanner_v1/database.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import threading
1919

2020
import google.auth.credentials
21+
from google.api_core import exceptions
2122
from google.gax.errors import GaxError
2223
from google.gax.grpc import exc_to_code
2324
from google.cloud.spanner_v1.gapic.spanner_client import SpannerClient
@@ -208,14 +209,8 @@ def create(self):
208209
options=options,
209210
)
210211
except GaxError as exc:
211-
if exc_to_code(exc.cause) == StatusCode.ALREADY_EXISTS:
212-
raise Conflict(self.name)
213-
elif exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
214-
raise NotFound('Instance not found: {name}'.format(
215-
name=self._instance.name,
216-
))
217-
raise
218-
212+
exception = exceptions.from_grpc_error(exc.cause)
213+
raise exception
219214
return future
220215

221216
def exists(self):

spanner/tests/system/test_system.py

+30
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
from google.cloud._helpers import UTC
3838
from google.cloud.exceptions import GrpcRendezvous
39+
from google.cloud.exceptions import NotFound
3940
from google.cloud.spanner_v1._helpers import TimestampWithNanoseconds
4041
from google.cloud.spanner import Client
4142
from google.cloud.spanner import KeyRange
@@ -282,6 +283,35 @@ def test_create_database(self):
282283
for database in Config.INSTANCE.list_databases()]
283284
self.assertIn(temp_db_id, database_ids)
284285

286+
def test_table_not_found(self):
287+
temp_db_id = 'temp_db' + unique_resource_id('_')
288+
289+
table_name1 = 'MyTable'
290+
table_name2 = 'NotMyTable'
291+
self.assertNotEqual(table_name1, table_name2)
292+
293+
create_table = (
294+
'CREATE TABLE {} (\n'
295+
' Id STRING(36) NOT NULL,\n'
296+
' Field1 STRING(36) NOT NULL\n'
297+
') PRIMARY KEY (Id)')
298+
create_table = create_table.format(table_name1)
299+
create_index = 'CREATE INDEX IDX ON {} (Field1)'.format(table_name2)
300+
301+
temp_db = Config.INSTANCE.database(
302+
temp_db_id,
303+
ddl_statements=[
304+
create_table,
305+
create_index,
306+
],
307+
)
308+
with self.assertRaisesRegexp(NotFound,
309+
'404 Table not found: '
310+
+ table_name2):
311+
temp_db.create()
312+
313+
314+
285315
def test_update_database_ddl(self):
286316
pool = BurstyPool()
287317
temp_db_id = 'temp_db' + unique_resource_id('_')

0 commit comments

Comments
 (0)