diff --git a/samples/samples/snippets.py b/samples/samples/snippets.py index a447121010..57590551ad 100644 --- a/samples/samples/snippets.py +++ b/samples/samples/snippets.py @@ -196,6 +196,27 @@ def create_database(instance_id, database_id): # [END spanner_create_database] +# [START spanner_update_database] +def update_database(instance_id, database_id): + """Updates the drop protection setting for a database.""" + spanner_client = spanner.Client() + instance = spanner_client.instance(instance_id) + + db = instance.database(database_id) + db.enable_drop_protection = True + + operation = db.update(["enable_drop_protection"]) + + print("Waiting for update operation for {} to complete...".format( + db.name)) + operation.result(OPERATION_TIMEOUT_SECONDS) + + print("Updated database {}.".format(db.name)) + + +# [END spanner_update_database] + + # [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).""" diff --git a/samples/samples/snippets_test.py b/samples/samples/snippets_test.py index 6d5822e37b..b8e1e093a1 100644 --- a/samples/samples/snippets_test.py +++ b/samples/samples/snippets_test.py @@ -154,6 +154,19 @@ def test_create_instance_with_processing_units(capsys, lci_instance_id): retry_429(instance.delete)() +def test_update_database(capsys, instance_id, sample_database): + snippets.update_database( + instance_id, sample_database.database_id + ) + out, _ = capsys.readouterr() + assert "Updated database {}.".format(sample_database.name) in out + + # Cleanup + sample_database.enable_drop_protection = False + op = sample_database.update(["enable_drop_protection"]) + op.result() + + def test_create_database_with_encryption_config( capsys, instance_id, cmek_database_id, kms_key_name ):