Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for UpdateDatabase in Cloud Spanner #941

Merged
merged 2 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions google/cloud/spanner_admin_database_v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
from .types.spanner_database_admin import RestoreInfo
from .types.spanner_database_admin import UpdateDatabaseDdlMetadata
from .types.spanner_database_admin import UpdateDatabaseDdlRequest
from .types.spanner_database_admin import UpdateDatabaseMetadata
from .types.spanner_database_admin import UpdateDatabaseRequest
from .types.spanner_database_admin import RestoreSourceType

__all__ = (
Expand Down Expand Up @@ -107,4 +109,6 @@
"UpdateBackupRequest",
"UpdateDatabaseDdlMetadata",
"UpdateDatabaseDdlRequest",
"UpdateDatabaseMetadata",
"UpdateDatabaseRequest",
)
15 changes: 15 additions & 0 deletions google/cloud/spanner_admin_database_v1/gapic_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
"update_backup"
]
},
"UpdateDatabase": {
"methods": [
"update_database"
]
},
"UpdateDatabaseDdl": {
"methods": [
"update_database_ddl"
Expand Down Expand Up @@ -200,6 +205,11 @@
"update_backup"
]
},
"UpdateDatabase": {
"methods": [
"update_database"
]
},
"UpdateDatabaseDdl": {
"methods": [
"update_database_ddl"
Expand Down Expand Up @@ -300,6 +310,11 @@
"update_backup"
]
},
"UpdateDatabase": {
"methods": [
"update_database"
]
},
"UpdateDatabaseDdl": {
"methods": [
"update_database_ddl"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,188 @@ async def sample_get_database():
# Done; return the response.
return response

async def update_database(
self,
request: Optional[
Union[spanner_database_admin.UpdateDatabaseRequest, dict]
] = None,
*,
database: Optional[spanner_database_admin.Database] = None,
update_mask: Optional[field_mask_pb2.FieldMask] = None,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, str]] = (),
) -> operation_async.AsyncOperation:
r"""Updates a Cloud Spanner database. The returned [long-running
operation][google.longrunning.Operation] can be used to track
the progress of updating the database. If the named database
does not exist, returns ``NOT_FOUND``.

While the operation is pending:

- The database's
[reconciling][google.spanner.admin.database.v1.Database.reconciling]
field is set to true.
- Cancelling the operation is best-effort. If the cancellation
succeeds, the operation metadata's
[cancel_time][google.spanner.admin.database.v1.UpdateDatabaseMetadata.cancel_time]
is set, the updates are reverted, and the operation
terminates with a ``CANCELLED`` status.
- New UpdateDatabase requests will return a
``FAILED_PRECONDITION`` error until the pending operation is
done (returns successfully or with error).
- Reading the database via the API continues to give the
pre-request values.

Upon completion of the returned operation:

- The new values are in effect and readable via the API.
- The database's
[reconciling][google.spanner.admin.database.v1.Database.reconciling]
field becomes false.

The returned [long-running
operation][google.longrunning.Operation] will have a name of the
format
``projects/<project>/instances/<instance>/databases/<database>/operations/<operation_id>``
and can be used to track the database modification. The
[metadata][google.longrunning.Operation.metadata] field type is
[UpdateDatabaseMetadata][google.spanner.admin.database.v1.UpdateDatabaseMetadata].
The [response][google.longrunning.Operation.response] field type
is [Database][google.spanner.admin.database.v1.Database], if
successful.

.. code-block:: python

# This snippet has been automatically generated and should be regarded as a
# code template only.
# It will require modifications to work:
# - It may require correct/in-range values for request initialization.
# - It may require specifying regional endpoints when creating the service
# client as shown in:
# https://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import spanner_admin_database_v1

async def sample_update_database():
# Create a client
client = spanner_admin_database_v1.DatabaseAdminAsyncClient()

# Initialize request argument(s)
database = spanner_admin_database_v1.Database()
database.name = "name_value"

request = spanner_admin_database_v1.UpdateDatabaseRequest(
database=database,
)

# Make the request
operation = client.update_database(request=request)

print("Waiting for operation to complete...")

response = (await operation).result()

# Handle the response
print(response)

Args:
request (Optional[Union[google.cloud.spanner_admin_database_v1.types.UpdateDatabaseRequest, dict]]):
The request object. The request for
[UpdateDatabase][google.spanner.admin.database.v1.DatabaseAdmin.UpdateDatabase].
database (:class:`google.cloud.spanner_admin_database_v1.types.Database`):
Required. The database to update. The ``name`` field of
the database is of the form
``projects/<project>/instances/<instance>/databases/<database>``.

This corresponds to the ``database`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
update_mask (:class:`google.protobuf.field_mask_pb2.FieldMask`):
Required. The list of fields to update. Currently, only
``enable_drop_protection`` field can be updated.

This corresponds to the ``update_mask`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
retry (google.api_core.retry.Retry): Designation of what errors, if any,
should be retried.
timeout (float): The timeout for this request.
metadata (Sequence[Tuple[str, str]]): Strings which should be
sent along with the request as metadata.

Returns:
google.api_core.operation_async.AsyncOperation:
An object representing a long-running operation.

The result type for the operation will be
:class:`google.cloud.spanner_admin_database_v1.types.Database`
A Cloud Spanner database.

"""
# Create or coerce a protobuf request object.
# Quick check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
has_flattened_params = any([database, update_mask])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
)

request = spanner_database_admin.UpdateDatabaseRequest(request)

# If we have keyword arguments corresponding to fields on the
# request, apply these.
if database is not None:
request.database = database
if update_mask is not None:
request.update_mask = update_mask

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.update_database,
default_retry=retries.Retry(
initial=1.0,
maximum=32.0,
multiplier=1.3,
predicate=retries.if_exception_type(
core_exceptions.DeadlineExceeded,
core_exceptions.ServiceUnavailable,
),
deadline=3600.0,
),
default_timeout=3600.0,
client_info=DEFAULT_CLIENT_INFO,
)

# Certain fields should be provided within the metadata header;
# add these here.
metadata = tuple(metadata) + (
gapic_v1.routing_header.to_grpc_metadata(
(("database.name", request.database.name),)
),
)

# Send the request.
response = await rpc(
request,
retry=retry,
timeout=timeout,
metadata=metadata,
)

# Wrap the response in an operation future.
response = operation_async.from_gapic(
response,
self._client._transport.operations_client,
spanner_database_admin.Database,
metadata_type=spanner_database_admin.UpdateDatabaseMetadata,
)

# Done; return the response.
return response

async def update_database_ddl(
self,
request: Optional[
Expand Down
Loading