Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use readonly transaction #97

Closed
wusendong opened this issue Jul 16, 2021 · 6 comments · Fixed by #125
Closed

How to use readonly transaction #97

wusendong opened this issue Jul 16, 2021 · 6 comments · Fixed by #125
Assignees
Labels
api: spanner Issues related to the googleapis/python-spanner-sqlalchemy API.

Comments

@wusendong
Copy link

wusendong commented Jul 16, 2021

I want to execute the query from a database in which my service account has no beginOrRollbackReadWriteTransaction permission, and I use this library with pandas via codes like here:

from sqlalchemy import create_engine
import pandas as pd

engine = create_engine( "spanner:///projects/project_id/instances/instance_id/databases/demo")
pd.read_sql("SELECT * from plans", engine)

And I got a message returns: "Caller is missing IAM permission spanner.databases.beginOrRollbackReadWriteTransaction on resource projects/project_id/instances/instance_id/databases/demo"

So how to use this library to execute some read-only SQL? Because I want only a limited number of people to have write access

@skuruppu
Copy link
Contributor

@wusendong just letting you know that @IlyaFaer is looking into how to support this. I don't believe there's a way to trigger the implementation to use read-only SQL in the current implementation. But @IlyaFaer will get back to you with details.

@IlyaFaer
Copy link
Contributor

Yes, it looks like we need to do some changes in code first. Working on it.

@wusendong
Copy link
Author

@IlyaFaer glad to hear that 👍

@IlyaFaer
Copy link
Contributor

IlyaFaer commented Aug 2, 2021

@wusendong, by default a connection is in non-autocommit mode. However, if your case doesn't require to stay in non-autocommit mode, you probably can initiate a connection manually from the engine, turn it into autocommit mode by writing:
connection.autocommit = True
or by changing isolation level:
connection.set_isolation_level("AUTOCOMMIT")
and then call read_sql(). In this case a single-use ReadOnly transaction going to be used, which should work for your case.

@wusendong
Copy link
Author

Good to see your quick implementation, this will help us a lot 👏

@hadim
Copy link

hadim commented Sep 25, 2021

I am hitting the same issue here. I have a client application using spanner-sqlalchemy and when it's used by an IAM user with the roles/spanner.databaseReader role it raises an error related to a missing spanner.databases.beginOrRollbackReadWriteTransaction permission. It works well when the user has the roles/spanner.databaseUser role.

The query is quite a standard sqlalchemy one: just getting some rows by their primary indices and calling `query.all() at the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the googleapis/python-spanner-sqlalchemy API.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants