-
Notifications
You must be signed in to change notification settings - Fork 188
SQLAlchemy 2.0 Compatibility #307
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
Conversation
Thank you for your pull request and welcome to our community. We could not parse the GitHub identity of the following contributors: mritter31.
|
5838031
to
f820dcd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add an integration test on the sqlalchemy beta version?
See
- { python: "3.11", trino: "latest", sqlalchemy: "~=1.3.0" } |
f820dcd
to
063cc51
Compare
Thanks @mdesmet, that was what I was looking for. Unfortunately, the tests for 2.0 are failing because of an issue in |
063cc51
to
3b659f4
Compare
We're using that library only in tests if I remember correctly - can we remove that dependency with some alternative code? It's used in two tests - just to verify whether Would those tests still work if we directly executed |
3b659f4
to
704dae3
Compare
@hashhar They have just released a new version of |
b9b17b0
to
5934726
Compare
5e3fc8c
to
d5a33aa
Compare
@@ -156,7 +156,7 @@ def _get_columns(self, connection: Connection, table_name: str, schema: str = No | |||
ORDER BY "ordinal_position" ASC | |||
""" | |||
).strip() | |||
res = connection.execute(sql.text(query), schema=schema, table=table_name) | |||
res = connection.execute(sql.text(query), {"schema": schema, "table": table_name}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change required for sqlalchemy 2.0.0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -351,7 +355,8 @@ def test_get_table_comment(trino_connection): | |||
engine, conn = trino_connection | |||
|
|||
if not engine.dialect.has_schema(conn, "test"): | |||
engine.execute(sqla.schema.CreateSchema("test")) | |||
with engine.begin() as connection: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change required for sqlalchemy 2.0.0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1a3f263
to
701cc72
Compare
@hashhar : PTAL |
@@ -421,10 +428,10 @@ def test_get_view_names(trino_connection, schema): | |||
) | |||
metadata.create_all(engine) | |||
view_name = schema_name + ".test_get_view_names" | |||
conn.execute(f"CREATE VIEW {view_name} AS SELECT * FROM test_table") | |||
conn.execute(sqla.text(f"CREATE VIEW {view_name} AS SELECT * FROM test_table")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This conn
object is a dbapi connection (see fixture trino_connection
) so why do we need this sqla.text here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The conn
object is a SQLAlchemy Connection instance:
trino-python-client/tests/integration/test_sqlalchemy_integration.py
Lines 20 to 25 in 701cc72
@pytest.fixture | |
def trino_connection(run_trino, request): | |
_, host, port = run_trino | |
engine = sqla.create_engine(f"trino://test@{host}:{port}/{request.param}", | |
connect_args={"source": "test", "max_attempts": 1}) | |
yield engine, engine.connect() |
SQLAlchemy 2.0 requires this raw statement to be wrapped into text
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, 🤦 - should've seen what class the fixture is coming from. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM % comments
701cc72
to
a4ed553
Compare
Thank you for your pull request and welcome to our community. We could not parse the GitHub identity of the following contributors: Mathias Ritter.
|
The following changes are for compatibility with SQLAlchemy 2.0, while mainting compatibility with versions >= 1.3. In the Connection.execute function, query parameters may no longer be passed as keyword arguments. Parameters can be passed in a dicionary instead. Engine.execute was removed. Statements can only be executed on the Connection object, which can be obtained via Engine.begin() or Engine.connect(). RowProxy is no longer a “proxy”; is now called Row and behaves like an enhanced named tuple. In order to access rows, just use row.column instead of row["column"]. Raw SQL statements must be wrapped into a TextClause by calling text(...), imported from sqlalchemy.
In the do_execute function of SQLAlchemy dialect, an if statement was accessing context.should_autocommit. This property has been removed in SQLAlchemy 2.0. To fix, the if statement can simply be removed as it is no longer needed.
a4ed553
to
5d80068
Compare
LGTM. @mathiasritter I assume you've sent the CLA already (and it matches the email you use in your commits)? If not please do. If sent already I'll take care of it. |
@hashhar Yes I’ve already sent it |
Description
In the do_execute function of SQLAlchemy dialect, an if statement was accessing context.should_autocommit. This property has been removed in SQLAlchemy 2.0.
To fix, the if statement can simply be removed as it is no longer needed. See #291 for more details.
Resolves #291
Non-technical explanation
The current dialect is incompatible with SQLAlchemy version 2.0. This change makes the dialect compatible.
Release notes
( ) This is not user-visible or docs only and no release notes are required.
( ) Release notes are required, please propose a release note for me.
(x) Release notes are required, with the following suggested text: