31
31
from google .cloud import spanner
32
32
from google .cloud .spanner_admin_instance_v1 .types import spanner_instance_admin
33
33
from google .cloud .spanner_v1 import param_types
34
- from google .type import expr_pb2
35
- from google .iam .v1 import policy_pb2
36
34
from google .cloud .spanner_v1 .data_types import JsonObject
35
+ from google .iam .v1 import policy_pb2
37
36
from google .protobuf import field_mask_pb2 # type: ignore
37
+ from google .type import expr_pb2
38
+
38
39
OPERATION_TIMEOUT_SECONDS = 240
39
40
40
41
@@ -196,6 +197,43 @@ def create_database(instance_id, database_id):
196
197
# [END spanner_create_database]
197
198
198
199
200
+ # [START spanner_update_database_drop_protection]
201
+ def update_database_drop_protection (instance_id , database_id ):
202
+ """Updates the drop protection setting for a database by first enabling and then disabling it."""
203
+ spanner_client = spanner .Client ()
204
+ instance = spanner_client .instance (instance_id )
205
+
206
+ db = instance .database (database_id )
207
+ db .drop_protection_enabled = True
208
+
209
+ operation = db .update ()
210
+
211
+ print ("Waiting for operation to complete..." )
212
+ operation .result (OPERATION_TIMEOUT_SECONDS )
213
+
214
+ print (
215
+ "Drop protection for database {} updated to {}" .format (
216
+ db .name , db .drop_protection_enabled
217
+ )
218
+ )
219
+
220
+ db .drop_protection_enabled = False
221
+
222
+ operation = db .update ()
223
+
224
+ print ("Waiting for operation to complete..." )
225
+ operation .result (OPERATION_TIMEOUT_SECONDS )
226
+
227
+ print (
228
+ "Drop protection for database {} updated to {}" .format (
229
+ db .name , db .drop_protection_enabled
230
+ )
231
+ )
232
+
233
+
234
+ # [END spanner_update_database_drop_protection]
235
+
236
+
199
237
# [START spanner_create_database_with_encryption_key]
200
238
def create_database_with_encryption_key (instance_id , database_id , kms_key_name ):
201
239
"""Creates a database with tables using a Customer Managed Encryption Key (CMEK)."""
@@ -1402,7 +1440,7 @@ def delete_singers(transaction):
1402
1440
1403
1441
1404
1442
def delete_data_with_dml_returning (instance_id , database_id ):
1405
- """Deletes sample data from the database using a DML statement having a THEN RETURN clause. """
1443
+ """Deletes sample data from the database using a DML statement having a THEN RETURN clause."""
1406
1444
# [START spanner_dml_delete_returning]
1407
1445
# instance_id = "your-spanner-instance"
1408
1446
# database_id = "your-spanner-db-id"
@@ -1538,7 +1576,7 @@ def insert_singers(transaction):
1538
1576
1539
1577
1540
1578
def insert_with_dml_returning (instance_id , database_id ):
1541
- """Inserts sample data into the given database using a DML statement having a THEN RETURN clause. """
1579
+ """Inserts sample data into the given database using a DML statement having a THEN RETURN clause."""
1542
1580
# [START spanner_dml_insert_returning]
1543
1581
# instance_id = "your-spanner-instance"
1544
1582
# database_id = "your-spanner-db-id"
@@ -1727,7 +1765,7 @@ def update_albums(transaction):
1727
1765
1728
1766
1729
1767
def create_table_with_datatypes (instance_id , database_id ):
1730
- """Creates a table with supported datatypes. """
1768
+ """Creates a table with supported datatypes."""
1731
1769
# [START spanner_create_table_with_datatypes]
1732
1770
# instance_id = "your-spanner-instance"
1733
1771
# database_id = "your-spanner-db-id"
@@ -2460,7 +2498,9 @@ def enable_fine_grained_access(
2460
2498
query_data_with_index_parser .add_argument ("--end_title" , default = "Goo" )
2461
2499
subparsers .add_parser ("read_data_with_index" , help = read_data_with_index .__doc__ )
2462
2500
subparsers .add_parser ("add_storing_index" , help = add_storing_index .__doc__ )
2463
- subparsers .add_parser ("read_data_with_storing_index" , help = read_data_with_storing_index .__doc__ )
2501
+ subparsers .add_parser (
2502
+ "read_data_with_storing_index" , help = read_data_with_storing_index .__doc__
2503
+ )
2464
2504
subparsers .add_parser (
2465
2505
"create_table_with_timestamp" , help = create_table_with_timestamp .__doc__
2466
2506
)
@@ -2486,9 +2526,13 @@ def enable_fine_grained_access(
2486
2526
subparsers .add_parser ("insert_data_with_dml" , help = insert_data_with_dml .__doc__ )
2487
2527
subparsers .add_parser ("log_commit_stats" , help = log_commit_stats .__doc__ )
2488
2528
subparsers .add_parser ("update_data_with_dml" , help = update_data_with_dml .__doc__ )
2489
- subparsers .add_parser ("update_data_with_dml_returning" , help = update_data_with_dml_returning .__doc__ )
2529
+ subparsers .add_parser (
2530
+ "update_data_with_dml_returning" , help = update_data_with_dml_returning .__doc__
2531
+ )
2490
2532
subparsers .add_parser ("delete_data_with_dml" , help = delete_data_with_dml .__doc__ )
2491
- subparsers .add_parser ("delete_data_with_dml_returning" , help = delete_data_with_dml_returning .__doc__ )
2533
+ subparsers .add_parser (
2534
+ "delete_data_with_dml_returning" , help = delete_data_with_dml_returning .__doc__
2535
+ )
2492
2536
subparsers .add_parser (
2493
2537
"update_data_with_dml_timestamp" , help = update_data_with_dml_timestamp .__doc__
2494
2538
)
@@ -2499,7 +2543,9 @@ def enable_fine_grained_access(
2499
2543
"update_data_with_dml_struct" , help = update_data_with_dml_struct .__doc__
2500
2544
)
2501
2545
subparsers .add_parser ("insert_with_dml" , help = insert_with_dml .__doc__ )
2502
- subparsers .add_parser ("insert_with_dml_returning" , help = insert_with_dml_returning .__doc__ )
2546
+ subparsers .add_parser (
2547
+ "insert_with_dml_returning" , help = insert_with_dml_returning .__doc__
2548
+ )
2503
2549
subparsers .add_parser (
2504
2550
"query_data_with_parameter" , help = query_data_with_parameter .__doc__
2505
2551
)
0 commit comments