This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
feat: add support for updateDatabase in Cloud Spanner #914
Merged
rajatbhatta
merged 14 commits into
googleapis:main
from
aayushimalik:drop-database-protection
May 16, 2023
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e4163d0
feat: drop database protection
aayushimalik 1d64d5d
feat: drop database protection sample and sample test
aayushimalik ee653cd
Make sample changes follow client lib spec
aayushimalik 167f68b
Reviewer suggested changes.
aayushimalik b27fd76
Add test for invalid UpdateDatabaseRequest.
aayushimalik 545cd8c
Update field name `drop_protection_enabled` to `enable_drop_protection`.
aayushimalik 430543c
Formatting fix.
aayushimalik babd236
Update the `reconciling` field when reloading a database.
aayushimalik 5420284
Merge remote-tracking branch 'googleapi-python-spanner/main' into dro…
aayushimalik a700a02
linting fix
rajatbhatta effbfbf
Merge branch 'main' into drop-database-protection
rajatbhatta 8ad7820
Merge branch 'main' into drop-database-protection
rajatbhatta 23c2014
Merge branch 'googleapis:main' into drop-database-protection-samples
rajatbhatta 970cb8a
Merge branch 'drop-database-protection-samples' into drop-database-pr…
rajatbhatta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |
| from google.api_core import gapic_v1 | ||
| from google.iam.v1 import iam_policy_pb2 | ||
| from google.iam.v1 import options_pb2 | ||
| from google.protobuf.field_mask_pb2 import FieldMask | ||
|
|
||
| from google.cloud.spanner_admin_database_v1 import CreateDatabaseRequest | ||
| from google.cloud.spanner_admin_database_v1 import Database as DatabasePB | ||
|
|
@@ -127,6 +128,9 @@ class Database(object): | |
| (Optional) database dialect for the database | ||
| :type database_role: str or None | ||
| :param database_role: (Optional) user-assigned database_role for the session. | ||
| :type enable_drop_protection: boolean | ||
| :param enable_drop_protection: (Optional) Represents whether the database | ||
| has drop protection enabled or not. | ||
| """ | ||
|
|
||
| _spanner_api = None | ||
|
|
@@ -141,6 +145,7 @@ def __init__( | |
| encryption_config=None, | ||
| database_dialect=DatabaseDialect.DATABASE_DIALECT_UNSPECIFIED, | ||
| database_role=None, | ||
| enable_drop_protection=False, | ||
| ): | ||
| self.database_id = database_id | ||
| self._instance = instance | ||
|
|
@@ -159,6 +164,8 @@ def __init__( | |
| self._database_dialect = database_dialect | ||
| self._database_role = database_role | ||
| self._route_to_leader_enabled = self._instance._client.route_to_leader_enabled | ||
| self._enable_drop_protection = enable_drop_protection | ||
| self._reconciling = False | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't see any place where this value is updated?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is updated at line 359 of this file, using the property setter.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am referring to |
||
|
|
||
| if pool is None: | ||
| pool = BurstyPool(database_role=database_role) | ||
|
|
@@ -332,6 +339,29 @@ def database_role(self): | |
| """ | ||
| return self._database_role | ||
|
|
||
| @property | ||
| def reconciling(self): | ||
| """Whether the database is currently reconciling. | ||
|
|
||
| :rtype: boolean | ||
| :returns: a boolean representing whether the database is reconciling | ||
| """ | ||
| return self._reconciling | ||
|
|
||
| @property | ||
| def enable_drop_protection(self): | ||
| """Whether the database has drop protection enabled. | ||
|
|
||
| :rtype: boolean | ||
| :returns: a boolean representing whether the database has drop | ||
| protection enabled | ||
| """ | ||
| return self._enable_drop_protection | ||
|
|
||
| @enable_drop_protection.setter | ||
| def enable_drop_protection(self, value): | ||
| self._enable_drop_protection = value | ||
|
|
||
| @property | ||
| def logger(self): | ||
| """Logger used by the database. | ||
|
|
@@ -461,14 +491,16 @@ def reload(self): | |
| self._encryption_info = response.encryption_info | ||
| self._default_leader = response.default_leader | ||
| self._database_dialect = response.database_dialect | ||
| self._enable_drop_protection = response.enable_drop_protection | ||
| self._reconciling = response.reconciling | ||
|
|
||
| def update_ddl(self, ddl_statements, operation_id=""): | ||
| """Update DDL for this database. | ||
|
|
||
| Apply any configured schema from :attr:`ddl_statements`. | ||
|
|
||
| See | ||
| https://cloud.google.com/spanner/reference/rpc/google.spanner.admin.database.v1#google.spanner.admin.database.v1.DatabaseAdmin.UpdateDatabase | ||
| https://cloud.google.com/spanner/reference/rpc/google.spanner.admin.database.v1#google.spanner.admin.database.v1.DatabaseAdmin.UpdateDatabaseDdl | ||
|
|
||
| :type ddl_statements: Sequence[str] | ||
| :param ddl_statements: a list of DDL statements to use on this database | ||
|
|
@@ -492,6 +524,46 @@ def update_ddl(self, ddl_statements, operation_id=""): | |
| future = api.update_database_ddl(request=request, metadata=metadata) | ||
| return future | ||
|
|
||
| def update(self, fields): | ||
| """Update this database. | ||
|
|
||
| See | ||
| https://cloud.google.com/spanner/reference/rpc/google.spanner.admin.database.v1#google.spanner.admin.database.v1.DatabaseAdmin.UpdateDatabase | ||
|
|
||
| .. note:: | ||
|
|
||
| Updates the specified fields of a Cloud Spanner database. Currently, | ||
| only the `enable_drop_protection` field supports updates. To change | ||
| this value before updating, set it via | ||
|
|
||
| .. code:: python | ||
|
|
||
| database.enable_drop_protection = True | ||
asthamohta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| before calling :meth:`update`. | ||
|
|
||
| :type fields: Sequence[str] | ||
| :param fields: a list of fields to update | ||
|
|
||
| :rtype: :class:`google.api_core.operation.Operation` | ||
| :returns: an operation instance | ||
| :raises NotFound: if the database does not exist | ||
| """ | ||
| api = self._instance._client.database_admin_api | ||
| database_pb = DatabasePB( | ||
| name=self.name, enable_drop_protection=self._enable_drop_protection | ||
| ) | ||
|
|
||
| # Only support updating drop protection for now. | ||
| field_mask = FieldMask(paths=fields) | ||
| metadata = _metadata_with_prefix(self.name) | ||
|
|
||
| future = api.update_database( | ||
| database=database_pb, update_mask=field_mask, metadata=metadata | ||
| ) | ||
|
|
||
| return future | ||
|
|
||
| def drop(self): | ||
| """Drop this database. | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,10 @@ | |
|
|
||
| import mock | ||
| from google.api_core import gapic_v1 | ||
| from google.cloud.spanner_admin_database_v1 import Database as DatabasePB | ||
| from google.cloud.spanner_v1.param_types import INT64 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks |
||
| from google.api_core.retry import Retry | ||
| from google.protobuf.field_mask_pb2 import FieldMask | ||
|
|
||
| from google.cloud.spanner_v1 import RequestOptions | ||
|
|
||
|
|
@@ -760,6 +762,8 @@ def test_reload_success(self): | |
| encryption_config=encryption_config, | ||
| encryption_info=encryption_info, | ||
| default_leader=default_leader, | ||
| reconciling=True, | ||
| enable_drop_protection=True, | ||
| ) | ||
| api.get_database.return_value = db_pb | ||
| instance = _Instance(self.INSTANCE_NAME, client=client) | ||
|
|
@@ -776,6 +780,8 @@ def test_reload_success(self): | |
| self.assertEqual(database._encryption_config, encryption_config) | ||
| self.assertEqual(database._encryption_info, encryption_info) | ||
| self.assertEqual(database._default_leader, default_leader) | ||
| self.assertEqual(database._reconciling, True) | ||
| self.assertEqual(database._enable_drop_protection, True) | ||
|
|
||
| api.get_database_ddl.assert_called_once_with( | ||
| database=self.DATABASE_NAME, | ||
|
|
@@ -892,6 +898,32 @@ def test_update_ddl_w_operation_id(self): | |
| metadata=[("google-cloud-resource-prefix", database.name)], | ||
| ) | ||
|
|
||
| def test_update_success(self): | ||
| op_future = object() | ||
| client = _Client() | ||
| api = client.database_admin_api = self._make_database_admin_api() | ||
| api.update_database.return_value = op_future | ||
|
|
||
| instance = _Instance(self.INSTANCE_NAME, client=client) | ||
| pool = _Pool() | ||
| database = self._make_one( | ||
| self.DATABASE_ID, instance, enable_drop_protection=True, pool=pool | ||
| ) | ||
|
|
||
| future = database.update(["enable_drop_protection"]) | ||
|
|
||
| self.assertIs(future, op_future) | ||
|
|
||
| expected_database = DatabasePB(name=database.name, enable_drop_protection=True) | ||
|
|
||
| field_mask = FieldMask(paths=["enable_drop_protection"]) | ||
|
|
||
| api.update_database.assert_called_once_with( | ||
| database=expected_database, | ||
| update_mask=field_mask, | ||
| metadata=[("google-cloud-resource-prefix", database.name)], | ||
| ) | ||
|
|
||
| def test_drop_grpc_error(self): | ||
| from google.api_core.exceptions import Unknown | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks