Skip to content

Commit 31e3d76

Browse files
committed
feat: support database role in connect arguments
1 parent 54b8c63 commit 31e3d76

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

samples/database_role.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)

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)