Skip to content

Commit 47aa27c

Browse files
authored
feat: support database role in connect arguments (#667)
* feat: support database role in connect arguments * chore: update minimum Spanner version + add sample to nox
1 parent 1cc02bf commit 47aa27c

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

samples/database_role_sample.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2025 Google LLC All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import uuid
16+
17+
from sqlalchemy import create_engine
18+
from sqlalchemy.orm import Session
19+
20+
from sample_helper import run_sample
21+
from model import Singer
22+
23+
24+
# Shows how to set the database role for a connection.
25+
def database_role_sample():
26+
engine = create_engine(
27+
"spanner:///projects/sample-project/"
28+
"instances/sample-instance/"
29+
"databases/sample-database",
30+
# You can set the database role in the connect arguments.
31+
connect_args={"database_role": "my_role"},
32+
echo=True,
33+
)
34+
with Session(engine) as session:
35+
singer_id = str(uuid.uuid4())
36+
singer = Singer(id=singer_id, first_name="John", last_name="Doe")
37+
session.add(singer)
38+
session.commit()
39+
40+
41+
if __name__ == "__main__":
42+
run_sample(database_role_sample)

samples/noxfile.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ def read_only_transaction(session):
7777
_sample(session)
7878

7979

80+
@nox.session()
81+
def database_role(session):
82+
_sample(session)
83+
84+
8085
@nox.session()
8186
def _all_samples(session):
8287
_sample(session)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
description = "SQLAlchemy dialect integrated into Cloud Spanner database"
2626
dependencies = [
2727
"sqlalchemy>=1.1.13",
28-
"google-cloud-spanner>=3.54.0",
28+
"google-cloud-spanner>=3.55.0",
2929
"alembic",
3030
]
3131
extras = {

test/mockserver_tests/test_basics.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,25 @@ class Singer(Base):
267267
session.add(singer)
268268
session.commit()
269269

270+
def test_database_role(self):
271+
add_select1_result()
272+
engine = create_engine(
273+
"spanner:///projects/p/instances/i/databases/d",
274+
connect_args={
275+
"client": self.client,
276+
"pool": FixedSizePool(size=10),
277+
"database_role": "my_role",
278+
},
279+
)
280+
with Session(engine.execution_options(isolation_level="autocommit")) as session:
281+
session.execute(select(1))
282+
requests = self.spanner_service.requests
283+
eq_(2, len(requests))
284+
is_instance_of(requests[0], BatchCreateSessionsRequest)
285+
is_instance_of(requests[1], ExecuteSqlRequest)
286+
request: BatchCreateSessionsRequest = requests[0]
287+
eq_("my_role", request.session_template.creator_role)
288+
270289
def test_select_table_in_named_schema(self):
271290
class Base(DeclarativeBase):
272291
pass

0 commit comments

Comments
 (0)