Skip to content

Commit

Permalink
Added missing operation_id to update_ddl method (#6737)
Browse files Browse the repository at this point in the history
  • Loading branch information
potiuk committed Dec 4, 2018
1 parent 1e8907b commit e9745de
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
6 changes: 4 additions & 2 deletions spanner/google/cloud/spanner_v1/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def reload(self):
response = api.get_database_ddl(self.name, metadata=metadata)
self._ddl_statements = tuple(response.statements)

def update_ddl(self, ddl_statements):
def update_ddl(self, ddl_statements, operation_id=''):
"""Update DDL for this database.
Apply any configured schema from :attr:`ddl_statements`.
Expand All @@ -264,6 +264,8 @@ def update_ddl(self, ddl_statements):
:type ddl_statements: Sequence[str]
:param ddl_statements: a list of DDL statements to use on this database
:type operation_id: str
:param operation_id: (optional) a string ID for the long-running operation
:rtype: :class:`google.api_core.operation.Operation`
:returns: an operation instance
Expand All @@ -274,7 +276,7 @@ def update_ddl(self, ddl_statements):
metadata = _metadata_with_prefix(self.name)

future = api.update_database_ddl(
self.name, ddl_statements, "", metadata=metadata
self.name, ddl_statements, operation_id=operation_id, metadata=metadata
)
return future

Expand Down
12 changes: 8 additions & 4 deletions spanner/tests/system/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import threading
import time
import unittest
import uuid

import pytest

Expand Down Expand Up @@ -323,20 +324,23 @@ def test_table_not_found(self):
"5629"
)
)
def test_update_database_ddl(self):
def test_update_database_ddl_with_operation_id(self):
pool = BurstyPool(labels={"testcase": "update_database_ddl"})
temp_db_id = "temp_db" + unique_resource_id("_")
temp_db = Config.INSTANCE.database(temp_db_id, pool=pool)
create_op = temp_db.create()
self.to_delete.append(temp_db)

# We want to make sure the operation completes.
create_op.result(120) # raises on failure / timeout.
create_op.result(240) # raises on failure / timeout.
# random but shortish always start with letter
operation_id = 'a' + str(uuid.uuid4())[:8]
operation = temp_db.update_ddl(DDL_STATEMENTS, operation_id=operation_id)

operation = temp_db.update_ddl(DDL_STATEMENTS)
self.assertEqual(operation_id, operation.operation.name.split('/')[-1])

# We want to make sure the operation completes.
operation.result(120) # raises on failure / timeout.
operation.result(240) # raises on failure / timeout.

temp_db.reload()

Expand Down
22 changes: 22 additions & 0 deletions spanner/tests/unit/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,28 @@ def test_update_ddl(self):
metadata=[("google-cloud-resource-prefix", database.name)],
)

def test_update_ddl_w_operation_id(self):
from tests._fixtures import DDL_STATEMENTS

op_future = object()
client = _Client()
api = client.database_admin_api = self._make_database_admin_api()
api.update_database_ddl.return_value = op_future
instance = _Instance(self.INSTANCE_NAME, client=client)
pool = _Pool()
database = self._make_one(self.DATABASE_ID, instance, pool=pool)

future = database.update_ddl(DDL_STATEMENTS, operation_id='someOperationId')

self.assertIs(future, op_future)

api.update_database_ddl.assert_called_once_with(
self.DATABASE_NAME,
DDL_STATEMENTS,
"someOperationId",
metadata=[("google-cloud-resource-prefix", database.name)],
)

def test_drop_grpc_error(self):
from google.api_core.exceptions import Unknown

Expand Down

0 comments on commit e9745de

Please sign in to comment.