From f9e834df34e11b41e5ac6f423da6453ee1fed783 Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Tue, 17 Nov 2026 23:46:00 +0000 Subject: [PATCH 1/5] feat(spanner): Add change stream DDL creation sample Creating a Spanner change stream with custom retention and transaction exclusion options previously had only SQL DDL snippets. --- spanner/change_streams/README.md | 27 ++++ spanner/change_streams/noxfile_config.py | 21 +++ spanner/change_streams/pyproject.toml | 30 ++++ spanner/change_streams/requirements-test.txt | 2 + spanner/change_streams/requirements.txt | 2 + .../src/spanner_change_streams/__init__.py | 13 ++ .../create_change_stream.py | 102 +++++++++++++ spanner/change_streams/tests/__init__.py | 13 ++ spanner/change_streams/tests/conftest.py | 38 +++++ .../tests/create_change_stream_test.py | 143 ++++++++++++++++++ 10 files changed, 391 insertions(+) create mode 100644 spanner/change_streams/README.md create mode 100644 spanner/change_streams/noxfile_config.py create mode 100644 spanner/change_streams/pyproject.toml create mode 100644 spanner/change_streams/requirements-test.txt create mode 100644 spanner/change_streams/requirements.txt create mode 100644 spanner/change_streams/src/spanner_change_streams/__init__.py create mode 100644 spanner/change_streams/src/spanner_change_streams/create_change_stream.py create mode 100644 spanner/change_streams/tests/__init__.py create mode 100644 spanner/change_streams/tests/conftest.py create mode 100644 spanner/change_streams/tests/create_change_stream_test.py diff --git a/spanner/change_streams/README.md b/spanner/change_streams/README.md new file mode 100644 index 00000000000..9681bb857ba --- /dev/null +++ b/spanner/change_streams/README.md @@ -0,0 +1,27 @@ +# Cloud Spanner Change Streams Samples + +Python samples demonstrating Cloud Spanner change stream DDL creation, transaction-level write exclusion, and `READ_` TVF queries. + +## Setup + +1. Create and activate a Python 3.13+ virtual environment: + + ```sh + python3 -m venv env + source env/bin/activate + ``` + +2. Install dependencies: + + ```sh + pip install -r requirements.txt + ``` + +## Running Tests + +Run unit tests offline: + +```sh +pip install -r requirements-test.txt +pytest tests +``` diff --git a/spanner/change_streams/noxfile_config.py b/spanner/change_streams/noxfile_config.py new file mode 100644 index 00000000000..fe371aaf480 --- /dev/null +++ b/spanner/change_streams/noxfile_config.py @@ -0,0 +1,21 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +TEST_CONFIG_OVERRIDE = { + "ignored_versions": ["2.7", "3.7", "3.8", "3.9", "3.10", "3.11", "3.12"], + "enforce_type_hints": True, + "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", + "pip_version_override": None, + "envs": {}, +} diff --git a/spanner/change_streams/pyproject.toml b/spanner/change_streams/pyproject.toml new file mode 100644 index 00000000000..fe963552748 --- /dev/null +++ b/spanner/change_streams/pyproject.toml @@ -0,0 +1,30 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Local samples only. This package isn't published: install it from this +# directory with `pip install -e .`, never by name from PyPI. +[build-system] +requires = ["setuptools>=64"] +build-backend = "setuptools.build_meta" + +[project] +name = "spanner-change-streams-samples" +version = "0.0.0" +requires-python = ">=3.13" +dependencies = [ + "google-api-core==2.44.0", + "google-cloud-spanner==3.72.0", +] +# PyPI rejects uploads with this classifier. +classifiers = ["Private :: Do Not Upload"] diff --git a/spanner/change_streams/requirements-test.txt b/spanner/change_streams/requirements-test.txt new file mode 100644 index 00000000000..ac63bdc5a92 --- /dev/null +++ b/spanner/change_streams/requirements-test.txt @@ -0,0 +1,2 @@ +-e . +pytest==9.2.0 diff --git a/spanner/change_streams/requirements.txt b/spanner/change_streams/requirements.txt new file mode 100644 index 00000000000..a56c81cc2eb --- /dev/null +++ b/spanner/change_streams/requirements.txt @@ -0,0 +1,2 @@ +google-api-core==2.44.0 +google-cloud-spanner==3.72.0 diff --git a/spanner/change_streams/src/spanner_change_streams/__init__.py b/spanner/change_streams/src/spanner_change_streams/__init__.py new file mode 100644 index 00000000000..58d482ea386 --- /dev/null +++ b/spanner/change_streams/src/spanner_change_streams/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/spanner/change_streams/src/spanner_change_streams/create_change_stream.py b/spanner/change_streams/src/spanner_change_streams/create_change_stream.py new file mode 100644 index 00000000000..dbfac61eed7 --- /dev/null +++ b/spanner/change_streams/src/spanner_change_streams/create_change_stream.py @@ -0,0 +1,102 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START spanner_create_change_stream] +import sys + +from google.api_core import exceptions +from google.cloud import spanner_admin_database_v1 + + +def create_change_stream(project_id: str, instance_id: str, database_id: str) -> None: + """Creates a Spanner change stream watching Singers and Albums. + + Enabling allow_txn_exclusion = true allows individual read-write + transactions to opt out of change-stream capture when needed. + + Args: + project_id: The Google Cloud project ID. + instance_id: The Spanner instance ID. + database_id: The Spanner database ID containing Singers and Albums. + """ + client = spanner_admin_database_v1.DatabaseAdminClient() + database_path = client.database_path(project_id, instance_id, database_id) + stream_name = "NamesAndAlbumsStream" + ddl_statement = ( + f"CREATE CHANGE STREAM {stream_name} " + "FOR Singers(FirstName, LastName), Albums " + "OPTIONS (retention_period = '36h', allow_txn_exclusion = true)" + ) + request = spanner_admin_database_v1.UpdateDatabaseDdlRequest( + database=database_path, + statements=[ddl_statement], + ) + + try: + operation = client.update_database_ddl(request=request) + print(f"Waiting for change stream '{stream_name}' creation...") + operation.result(timeout=300) + except (exceptions.AlreadyExists, exceptions.FailedPrecondition) as err: + print( + f"error: change stream '{stream_name}' already exists or failed " + f"preconditions on database '{database_id}' in instance " + f"'{instance_id}' for project '{project_id}': {err.message}", + file=sys.stderr, + ) + except exceptions.GoogleAPICallError as err: + print( + f"error: failed to create change stream '{stream_name}' on " + f"database '{database_id}' in instance '{instance_id}' for project " + f"'{project_id}': {err.message}", + file=sys.stderr, + ) + else: + print(f"Resource ID: {database_path}") + print(f"Change Stream: {stream_name}") + print("Retention Period: 36h") + print("Transaction Exclusion Allowed: True") + + +# [END spanner_create_change_stream] + + +if __name__ == "__main__": + import argparse + import os + + parser = argparse.ArgumentParser( + description="Create a Cloud Spanner change stream via DDL." + ) + parser.add_argument( + "--project-id", + default=os.environ.get("GOOGLE_CLOUD_PROJECT", "example-project-id"), + help="Google Cloud project ID (default: $GOOGLE_CLOUD_PROJECT)", + ) + parser.add_argument( + "--instance-id", + default=os.environ.get("SPANNER_INSTANCE_ID", "example-instance"), + help="Spanner instance ID (default: $SPANNER_INSTANCE_ID)", + ) + parser.add_argument( + "--database-id", + default=os.environ.get("SPANNER_DATABASE_ID", "example-db"), + help="Spanner database ID (default: $SPANNER_DATABASE_ID)", + ) + args = parser.parse_args() + + create_change_stream( + project_id=args.project_id, + instance_id=args.instance_id, + database_id=args.database_id, + ) diff --git a/spanner/change_streams/tests/__init__.py b/spanner/change_streams/tests/__init__.py new file mode 100644 index 00000000000..58d482ea386 --- /dev/null +++ b/spanner/change_streams/tests/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/spanner/change_streams/tests/conftest.py b/spanner/change_streams/tests/conftest.py new file mode 100644 index 00000000000..b29a7e6dc7d --- /dev/null +++ b/spanner/change_streams/tests/conftest.py @@ -0,0 +1,38 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Shared live-test configuration for Cloud Spanner Change Streams Samples.""" + +import os + +import pytest + +LIVE_PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "") +LIVE_INSTANCE_ID = os.environ.get("SPANNER_INSTANCE_ID", "") +LIVE_DATABASE_ID = os.environ.get( + "SPANNER_DATABASE_ID", os.environ.get("SPANNER_DATABASE_ID", "") +) + +if os.environ.get("RUN_INTEGRATION_TESTS") == "true" and ( + not LIVE_PROJECT_ID or not LIVE_INSTANCE_ID or not LIVE_DATABASE_ID +): + raise RuntimeError( + "Set GOOGLE_CLOUD_PROJECT, SPANNER_INSTANCE_ID, and " + "SPANNER_DATABASE_ID when RUN_INTEGRATION_TESTS=true." + ) + +live_only = pytest.mark.skipif( + os.environ.get("RUN_INTEGRATION_TESTS") != "true", + reason="Set RUN_INTEGRATION_TESTS=true to run integration tests.", +) diff --git a/spanner/change_streams/tests/create_change_stream_test.py b/spanner/change_streams/tests/create_change_stream_test.py new file mode 100644 index 00000000000..a8bd525c318 --- /dev/null +++ b/spanner/change_streams/tests/create_change_stream_test.py @@ -0,0 +1,143 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import runpy +from unittest import mock + +from google.api_core import exceptions +from google.cloud import spanner_admin_database_v1 +import pytest +from spanner_change_streams import create_change_stream + +from . import conftest + +PROJECT_ID = "example-project-id" +INSTANCE_ID = "example-instance" +DATABASE_ID = "example-db" +DATABASE_PATH = f"projects/{PROJECT_ID}/instances/{INSTANCE_ID}/databases/{DATABASE_ID}" +ADMIN_CLIENT = "google.cloud.spanner_admin_database_v1.DatabaseAdminClient" + + +@mock.patch(ADMIN_CLIENT) +def test_create_change_stream_success( + mock_client_class: mock.MagicMock, capsys: pytest.CaptureFixture[str] +) -> None: + client = mock_client_class.return_value + client.database_path.return_value = DATABASE_PATH + + create_change_stream.create_change_stream(PROJECT_ID, INSTANCE_ID, DATABASE_ID) + + req = client.update_database_ddl.call_args.kwargs["request"] + assert "CREATE CHANGE STREAM NamesAndAlbumsStream" in req.statements[0] + assert "allow_txn_exclusion = true" in req.statements[0] + out, err = capsys.readouterr() + assert f"Resource ID: {DATABASE_PATH}" in out + assert "Change Stream: NamesAndAlbumsStream" in out + assert "Retention Period: 36h" in out + assert "Transaction Exclusion Allowed: True" in out + assert err == "" + + +@pytest.mark.parametrize( + "error, expected_prefix", + [ + ( + exceptions.FailedPrecondition("duplicate"), + "error: change stream 'NamesAndAlbumsStream' already exists", + ), + ( + exceptions.PermissionDenied("denied"), + "error: failed to create change stream 'NamesAndAlbumsStream'", + ), + ], +) +@mock.patch(ADMIN_CLIENT) +def test_create_change_stream_errors( + mock_client_class: mock.MagicMock, + capsys: pytest.CaptureFixture[str], + error: Exception, + expected_prefix: str, +) -> None: + client = mock_client_class.return_value + client.database_path.return_value = DATABASE_PATH + client.update_database_ddl.side_effect = error + + create_change_stream.create_change_stream(PROJECT_ID, INSTANCE_ID, DATABASE_ID) + + _out, err = capsys.readouterr() + assert err.startswith(expected_prefix) + + +@mock.patch(ADMIN_CLIENT) +def test_create_change_stream_cli_entrypoint( + mock_client_class: mock.MagicMock, + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + mock_client_class.return_value.database_path.return_value = DATABASE_PATH + monkeypatch.setattr( + "sys.argv", + [ + "create_change_stream.py", + "--project-id", + PROJECT_ID, + "--instance-id", + INSTANCE_ID, + "--database-id", + DATABASE_ID, + ], + ) + + runpy.run_module("spanner_change_streams.create_change_stream", run_name="__main__") + + assert "Change Stream: NamesAndAlbumsStream" in capsys.readouterr().out + + +@conftest.live_only +def test_create_change_stream_live(capsys: pytest.CaptureFixture[str]) -> None: + admin = spanner_admin_database_v1.DatabaseAdminClient() + db_path = admin.database_path( + conftest.LIVE_PROJECT_ID, + conftest.LIVE_INSTANCE_ID, + conftest.LIVE_DATABASE_ID, + ) + try: + admin.update_database_ddl( + request={ + "database": db_path, + "statements": ["DROP CHANGE STREAM NamesAndAlbumsStream"], + } + ).result(timeout=180) + except (exceptions.NotFound, exceptions.FailedPrecondition): + pass + + try: + create_change_stream.create_change_stream( + conftest.LIVE_PROJECT_ID, + conftest.LIVE_INSTANCE_ID, + conftest.LIVE_DATABASE_ID, + ) + out, err = capsys.readouterr() + assert err == "" + assert "Change Stream: NamesAndAlbumsStream" in out + finally: + try: + admin.update_database_ddl( + request={ + "database": db_path, + "statements": ["DROP CHANGE STREAM NamesAndAlbumsStream"], + } + ).result(timeout=180) + except (exceptions.NotFound, exceptions.FailedPrecondition): + pass From 6792e9bc37850d8c5646172f119d83d32e600191 Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Wed, 18 Nov 2026 00:38:22 +0000 Subject: [PATCH 2/5] fix(spanner): Omit project_id from change stream DDL sample output Avoids printing project identifiers in stdout or stderr and simplifies duplicate environment variable lookup in test configuration. --- .../src/spanner_change_streams/create_change_stream.py | 8 ++++---- spanner/change_streams/tests/conftest.py | 4 +--- spanner/change_streams/tests/create_change_stream_test.py | 5 ++++- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/spanner/change_streams/src/spanner_change_streams/create_change_stream.py b/spanner/change_streams/src/spanner_change_streams/create_change_stream.py index dbfac61eed7..18fcec7868e 100644 --- a/spanner/change_streams/src/spanner_change_streams/create_change_stream.py +++ b/spanner/change_streams/src/spanner_change_streams/create_change_stream.py @@ -51,18 +51,18 @@ def create_change_stream(project_id: str, instance_id: str, database_id: str) -> print( f"error: change stream '{stream_name}' already exists or failed " f"preconditions on database '{database_id}' in instance " - f"'{instance_id}' for project '{project_id}': {err.message}", + f"'{instance_id}': {err.message}", file=sys.stderr, ) except exceptions.GoogleAPICallError as err: print( f"error: failed to create change stream '{stream_name}' on " - f"database '{database_id}' in instance '{instance_id}' for project " - f"'{project_id}': {err.message}", + f"database '{database_id}' in instance '{instance_id}'" + f": {err.message}", file=sys.stderr, ) else: - print(f"Resource ID: {database_path}") + print(f"Resource ID: {database_id}") print(f"Change Stream: {stream_name}") print("Retention Period: 36h") print("Transaction Exclusion Allowed: True") diff --git a/spanner/change_streams/tests/conftest.py b/spanner/change_streams/tests/conftest.py index b29a7e6dc7d..cbdf15b1a6a 100644 --- a/spanner/change_streams/tests/conftest.py +++ b/spanner/change_streams/tests/conftest.py @@ -20,9 +20,7 @@ LIVE_PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "") LIVE_INSTANCE_ID = os.environ.get("SPANNER_INSTANCE_ID", "") -LIVE_DATABASE_ID = os.environ.get( - "SPANNER_DATABASE_ID", os.environ.get("SPANNER_DATABASE_ID", "") -) +LIVE_DATABASE_ID = os.environ.get("SPANNER_DATABASE_ID", "") if os.environ.get("RUN_INTEGRATION_TESTS") == "true" and ( not LIVE_PROJECT_ID or not LIVE_INSTANCE_ID or not LIVE_DATABASE_ID diff --git a/spanner/change_streams/tests/create_change_stream_test.py b/spanner/change_streams/tests/create_change_stream_test.py index a8bd525c318..06186d0b21e 100644 --- a/spanner/change_streams/tests/create_change_stream_test.py +++ b/spanner/change_streams/tests/create_change_stream_test.py @@ -42,7 +42,8 @@ def test_create_change_stream_success( assert "CREATE CHANGE STREAM NamesAndAlbumsStream" in req.statements[0] assert "allow_txn_exclusion = true" in req.statements[0] out, err = capsys.readouterr() - assert f"Resource ID: {DATABASE_PATH}" in out + assert f"Resource ID: {DATABASE_ID}" in out + assert PROJECT_ID not in out assert "Change Stream: NamesAndAlbumsStream" in out assert "Retention Period: 36h" in out assert "Transaction Exclusion Allowed: True" in out @@ -77,6 +78,8 @@ def test_create_change_stream_errors( _out, err = capsys.readouterr() assert err.startswith(expected_prefix) + assert PROJECT_ID not in err + assert PROJECT_ID not in err @mock.patch(ADMIN_CLIENT) From 69b50f5a30405e947bd7e6da120f6fd4c879e9ec Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Wed, 18 Nov 2026 02:42:53 +0000 Subject: [PATCH 3/5] fix(spanner): Remove hardcoded static stdout lines from sample output Prints only runtime resource and query results instead of hardcoded string literals. --- .../src/spanner_change_streams/create_change_stream.py | 2 -- spanner/change_streams/tests/create_change_stream_test.py | 2 -- 2 files changed, 4 deletions(-) diff --git a/spanner/change_streams/src/spanner_change_streams/create_change_stream.py b/spanner/change_streams/src/spanner_change_streams/create_change_stream.py index 18fcec7868e..378ed4767ac 100644 --- a/spanner/change_streams/src/spanner_change_streams/create_change_stream.py +++ b/spanner/change_streams/src/spanner_change_streams/create_change_stream.py @@ -64,8 +64,6 @@ def create_change_stream(project_id: str, instance_id: str, database_id: str) -> else: print(f"Resource ID: {database_id}") print(f"Change Stream: {stream_name}") - print("Retention Period: 36h") - print("Transaction Exclusion Allowed: True") # [END spanner_create_change_stream] diff --git a/spanner/change_streams/tests/create_change_stream_test.py b/spanner/change_streams/tests/create_change_stream_test.py index 06186d0b21e..851e0fd6095 100644 --- a/spanner/change_streams/tests/create_change_stream_test.py +++ b/spanner/change_streams/tests/create_change_stream_test.py @@ -45,8 +45,6 @@ def test_create_change_stream_success( assert f"Resource ID: {DATABASE_ID}" in out assert PROJECT_ID not in out assert "Change Stream: NamesAndAlbumsStream" in out - assert "Retention Period: 36h" in out - assert "Transaction Exclusion Allowed: True" in out assert err == "" From 99f59f9b0d7c3be70184c0855f3e29f4de60bfda Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Wed, 18 Nov 2026 21:41:11 +0000 Subject: [PATCH 4/5] fix(spanner): report operation wait timeouts in create_change_stream Problem: the sample waits on a long-running operation with a client-side timeout. When that wait expires the client raises TimeoutError, which is not an API call error, so the sample ended with an unhandled traceback. Solution: catch TimeoutError and print a clear message to stderr noting that only the wait timed out and the operation may still be running (the timeout does not cancel it). Add a unit test covering the new handler. --- .../src/spanner_change_streams/create_change_stream.py | 6 ++++++ spanner/change_streams/tests/create_change_stream_test.py | 1 + 2 files changed, 7 insertions(+) diff --git a/spanner/change_streams/src/spanner_change_streams/create_change_stream.py b/spanner/change_streams/src/spanner_change_streams/create_change_stream.py index 378ed4767ac..710a2a3ebee 100644 --- a/spanner/change_streams/src/spanner_change_streams/create_change_stream.py +++ b/spanner/change_streams/src/spanner_change_streams/create_change_stream.py @@ -54,6 +54,12 @@ def create_change_stream(project_id: str, instance_id: str, database_id: str) -> f"'{instance_id}': {err.message}", file=sys.stderr, ) + except TimeoutError as err: + print( + f"error: timed out waiting for the operation on database '{database_id}' " + f"to complete; the operation may still be running: {err}", + file=sys.stderr, + ) except exceptions.GoogleAPICallError as err: print( f"error: failed to create change stream '{stream_name}' on " diff --git a/spanner/change_streams/tests/create_change_stream_test.py b/spanner/change_streams/tests/create_change_stream_test.py index 851e0fd6095..ce8d2231590 100644 --- a/spanner/change_streams/tests/create_change_stream_test.py +++ b/spanner/change_streams/tests/create_change_stream_test.py @@ -51,6 +51,7 @@ def test_create_change_stream_success( @pytest.mark.parametrize( "error, expected_prefix", [ + (TimeoutError("deadline"), "error: timed out waiting for the operation"), ( exceptions.FailedPrecondition("duplicate"), "error: change stream 'NamesAndAlbumsStream' already exists", From 080a3539223ec7631368001a5d1a831de3177a84 Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Thu, 19 Nov 2026 04:41:46 +0000 Subject: [PATCH 5/5] style(spanner): use triple-quoted SQL strings in create_change_stream Problem: multi-line SQL and DDL statements were written as adjacent string fragments. That style is easy to get wrong (a missing trailing space joins two SQL tokens) and is harder to read than the statement itself, and the samples in this set mixed it with triple-quoted SQL. Solution: write each multi-line statement as a single triple-quoted string, matching the layout already used by the other samples. Only whitespace between SQL tokens changes; the statements are otherwise identical. Files updated: - spanner/change_streams/src/spanner_change_streams/create_change_stream.py --- .../src/spanner_change_streams/create_change_stream.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spanner/change_streams/src/spanner_change_streams/create_change_stream.py b/spanner/change_streams/src/spanner_change_streams/create_change_stream.py index 710a2a3ebee..555f31482db 100644 --- a/spanner/change_streams/src/spanner_change_streams/create_change_stream.py +++ b/spanner/change_streams/src/spanner_change_streams/create_change_stream.py @@ -33,11 +33,11 @@ def create_change_stream(project_id: str, instance_id: str, database_id: str) -> client = spanner_admin_database_v1.DatabaseAdminClient() database_path = client.database_path(project_id, instance_id, database_id) stream_name = "NamesAndAlbumsStream" - ddl_statement = ( - f"CREATE CHANGE STREAM {stream_name} " - "FOR Singers(FirstName, LastName), Albums " - "OPTIONS (retention_period = '36h', allow_txn_exclusion = true)" - ) + ddl_statement = f""" + CREATE CHANGE STREAM {stream_name} + FOR Singers(FirstName, LastName), Albums + OPTIONS (retention_period = '36h', allow_txn_exclusion = true) + """ request = spanner_admin_database_v1.UpdateDatabaseDdlRequest( database=database_path, statements=[ddl_statement],