feat: add support for updateDatabase in Cloud Spanner by aayushimalik · Pull Request #914 · googleapis/python-spanner · GitHub
Skip to content
This repository was archived by the owner on Jun 8, 2026. It is now read-only.
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
74 changes: 73 additions & 1 deletion google/cloud/spanner_v1/database.py
6 changes: 6 additions & 0 deletions google/cloud/spanner_v1/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ def database(
encryption_config=None,
database_dialect=DatabaseDialect.DATABASE_DIALECT_UNSPECIFIED,
database_role=None,
enable_drop_protection=False,
):
"""Factory to create a database within this instance.

Expand Down Expand Up @@ -467,6 +468,10 @@ def database(
:param database_dialect:
(Optional) database dialect for the database

:type enable_drop_protection: boolean
:param enable_drop_protection: (Optional) Represents whether the database
has drop protection enabled or not.

:rtype: :class:`~google.cloud.spanner_v1.database.Database`
:returns: a database owned by this instance.
"""
Expand All @@ -479,6 +484,7 @@ def database(
encryption_config=encryption_config,
database_dialect=database_dialect,
database_role=database_role,
enable_drop_protection=enable_drop_protection,
)

def list_databases(self, page_size=None):
Expand Down
21 changes: 21 additions & 0 deletions samples/samples/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)."""
Expand Down
13 changes: 13 additions & 0 deletions samples/samples/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down
38 changes: 38 additions & 0 deletions tests/system/test_database_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,41 @@ def _unit_of_work(transaction, name):
rows = list(after.read(sd.COUNTERS_TABLE, sd.COUNTERS_COLUMNS, sd.ALL))

assert len(rows) == 2


def test_update_database_success(
not_emulator, shared_database, shared_instance, database_operation_timeout
):
old_protection = shared_database.enable_drop_protection
new_protection = True
shared_database.enable_drop_protection = new_protection
operation = shared_database.update(["enable_drop_protection"])

# We want to make sure the operation completes.
operation.result(database_operation_timeout) # raises on failure / timeout.

# Create a new database instance and reload it.
database_alt = shared_instance.database(shared_database.name.split("/")[-1])
assert database_alt.enable_drop_protection != new_protection

database_alt.reload()
assert database_alt.enable_drop_protection == new_protection

with pytest.raises(exceptions.FailedPrecondition):
database_alt.drop()

with pytest.raises(exceptions.FailedPrecondition):
shared_instance.delete()

# Make sure to put the database back the way it was for the
# other test cases.
shared_database.enable_drop_protection = old_protection
shared_database.update(["enable_drop_protection"])


def test_update_database_invalid(not_emulator, shared_database):
shared_database.enable_drop_protection = True

# Empty `fields` is not supported.
with pytest.raises(exceptions.InvalidArgument):
shared_database.update([])
32 changes: 32 additions & 0 deletions tests/unit/test_database.py