Skip to content

Commit

Permalink
feat: drop database protection sample and sample test
Browse files Browse the repository at this point in the history
  • Loading branch information
aayushimalik committed Mar 21, 2023
1 parent e4163d0 commit 9ca2103
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 82 deletions.
64 changes: 55 additions & 9 deletions samples/samples/snippets.py
Expand Up @@ -31,10 +31,11 @@
from google.cloud import spanner
from google.cloud.spanner_admin_instance_v1.types import spanner_instance_admin
from google.cloud.spanner_v1 import param_types
from google.type import expr_pb2
from google.iam.v1 import policy_pb2
from google.cloud.spanner_v1.data_types import JsonObject
from google.iam.v1 import policy_pb2
from google.protobuf import field_mask_pb2 # type: ignore
from google.type import expr_pb2

OPERATION_TIMEOUT_SECONDS = 240


Expand Down Expand Up @@ -196,6 +197,43 @@ def create_database(instance_id, database_id):
# [END spanner_create_database]


# [START spanner_update_database_drop_protection]
def update_database_drop_protection(instance_id, database_id):
"""Updates the drop protection setting for a database by first enabling and then disabling it."""
spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)

db = instance.database(database_id)
db.drop_protection_enabled = True

operation = db.update()

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

print(
"Drop protection for database {} updated to {}".format(
db.name, db.drop_protection_enabled
)
)

db.drop_protection_enabled = False

operation = db.update()

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

print(
"Drop protection for database {} updated to {}".format(
db.name, db.drop_protection_enabled
)
)


# [END spanner_update_database_drop_protection]


# [START spanner_create_database_with_encryption_key]
def create_database_with_encryption_key(instance_id, database_id, kms_key_name):
"""Creates a database with tables using a Customer Managed Encryption Key (CMEK)."""
Expand Down Expand Up @@ -1402,7 +1440,7 @@ def delete_singers(transaction):


def delete_data_with_dml_returning(instance_id, database_id):
"""Deletes sample data from the database using a DML statement having a THEN RETURN clause. """
"""Deletes sample data from the database using a DML statement having a THEN RETURN clause."""
# [START spanner_dml_delete_returning]
# instance_id = "your-spanner-instance"
# database_id = "your-spanner-db-id"
Expand Down Expand Up @@ -1538,7 +1576,7 @@ def insert_singers(transaction):


def insert_with_dml_returning(instance_id, database_id):
"""Inserts sample data into the given database using a DML statement having a THEN RETURN clause. """
"""Inserts sample data into the given database using a DML statement having a THEN RETURN clause."""
# [START spanner_dml_insert_returning]
# instance_id = "your-spanner-instance"
# database_id = "your-spanner-db-id"
Expand Down Expand Up @@ -1727,7 +1765,7 @@ def update_albums(transaction):


def create_table_with_datatypes(instance_id, database_id):
"""Creates a table with supported datatypes. """
"""Creates a table with supported datatypes."""
# [START spanner_create_table_with_datatypes]
# instance_id = "your-spanner-instance"
# database_id = "your-spanner-db-id"
Expand Down Expand Up @@ -2460,7 +2498,9 @@ def enable_fine_grained_access(
query_data_with_index_parser.add_argument("--end_title", default="Goo")
subparsers.add_parser("read_data_with_index", help=read_data_with_index.__doc__)
subparsers.add_parser("add_storing_index", help=add_storing_index.__doc__)
subparsers.add_parser("read_data_with_storing_index", help=read_data_with_storing_index.__doc__)
subparsers.add_parser(
"read_data_with_storing_index", help=read_data_with_storing_index.__doc__
)
subparsers.add_parser(
"create_table_with_timestamp", help=create_table_with_timestamp.__doc__
)
Expand All @@ -2486,9 +2526,13 @@ def enable_fine_grained_access(
subparsers.add_parser("insert_data_with_dml", help=insert_data_with_dml.__doc__)
subparsers.add_parser("log_commit_stats", help=log_commit_stats.__doc__)
subparsers.add_parser("update_data_with_dml", help=update_data_with_dml.__doc__)
subparsers.add_parser("update_data_with_dml_returning", help=update_data_with_dml_returning.__doc__)
subparsers.add_parser(
"update_data_with_dml_returning", help=update_data_with_dml_returning.__doc__
)
subparsers.add_parser("delete_data_with_dml", help=delete_data_with_dml.__doc__)
subparsers.add_parser("delete_data_with_dml_returning", help=delete_data_with_dml_returning.__doc__)
subparsers.add_parser(
"delete_data_with_dml_returning", help=delete_data_with_dml_returning.__doc__
)
subparsers.add_parser(
"update_data_with_dml_timestamp", help=update_data_with_dml_timestamp.__doc__
)
Expand All @@ -2499,7 +2543,9 @@ def enable_fine_grained_access(
"update_data_with_dml_struct", help=update_data_with_dml_struct.__doc__
)
subparsers.add_parser("insert_with_dml", help=insert_with_dml.__doc__)
subparsers.add_parser("insert_with_dml_returning", help=insert_with_dml_returning.__doc__)
subparsers.add_parser(
"insert_with_dml_returning", help=insert_with_dml_returning.__doc__
)
subparsers.add_parser(
"query_data_with_parameter", help=query_data_with_parameter.__doc__
)
Expand Down

0 comments on commit 9ca2103

Please sign in to comment.