This repository was archived by the owner on Mar 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
bug: handle SQLAlchemy connection deprecation #717
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,9 +12,8 @@ | |
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import base64 | ||
|
|
||
| import re | ||
|
|
||
| import sqlalchemy | ||
| from alembic.ddl.base import ( | ||
| ColumnNullable, | ||
| ColumnType, | ||
|
|
@@ -25,14 +24,16 @@ | |
| from google.api_core.client_options import ClientOptions | ||
| from google.auth.credentials import AnonymousCredentials | ||
| from google.cloud.spanner_v1 import Client, TransactionOptions | ||
| from sqlalchemy.exc import NoSuchTableError | ||
| from sqlalchemy.sql import elements | ||
| from google.cloud.spanner_v1.data_types import JsonObject | ||
| from sqlalchemy import ForeignKeyConstraint, types, TypeDecorator, PickleType | ||
| from sqlalchemy.engine.base import Engine | ||
| from sqlalchemy.engine.default import DefaultDialect, DefaultExecutionContext | ||
| from sqlalchemy.event import listens_for | ||
| from sqlalchemy.exc import NoSuchTableError | ||
| from sqlalchemy.ext.compiler import compiles | ||
| from sqlalchemy.pool import Pool | ||
| from sqlalchemy.sql import elements | ||
| from sqlalchemy.sql import expression | ||
| from sqlalchemy.sql.compiler import ( | ||
| selectable, | ||
| DDLCompiler, | ||
|
|
@@ -44,13 +45,10 @@ | |
| ) | ||
| from sqlalchemy.sql.default_comparator import operator_lookup | ||
| from sqlalchemy.sql.operators import json_getitem_op | ||
| from sqlalchemy.sql import expression | ||
|
|
||
| from google.cloud.spanner_v1.data_types import JsonObject | ||
| from google.cloud import spanner_dbapi | ||
| from google.cloud.sqlalchemy_spanner._opentelemetry_tracing import trace_call | ||
| from google.cloud.sqlalchemy_spanner import version as sqlalchemy_spanner_version | ||
| import sqlalchemy | ||
| from google.cloud.sqlalchemy_spanner._opentelemetry_tracing import trace_call | ||
|
|
||
| USING_SQLACLCHEMY_20 = False | ||
| if sqlalchemy.__version__.split(".")[0] == "2": | ||
|
|
@@ -63,12 +61,18 @@ | |
| @listens_for(Pool, "reset") | ||
| def reset_connection(dbapi_conn, connection_record, reset_state=None): | ||
| """An event of returning a connection back to a pool.""" | ||
| if hasattr(dbapi_conn, "driver_connection"): | ||
| dbapi_conn = dbapi_conn.driver_connection | ||
| if hasattr(dbapi_conn, "connection"): | ||
| dbapi_conn = dbapi_conn.connection | ||
| if isinstance(dbapi_conn, spanner_dbapi.Connection): | ||
| if dbapi_conn.inside_transaction: | ||
| transaction_started = getattr( | ||
| dbapi_conn, | ||
| "spanner_transaction_started", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: The name of the property is But I don't think it is wise to make this change at all, or at least not in the way that it is done here. |
||
| getattr(dbapi_conn, "inside_transaction", False), | ||
| ) | ||
| if transaction_started: | ||
| dbapi_conn.rollback() | ||
|
|
||
| dbapi_conn.staleness = None | ||
| dbapi_conn.read_only = False | ||
|
|
||
|
|
@@ -1709,7 +1713,7 @@ def set_isolation_level(self, conn_proxy, level): | |
| conn_proxy ( | ||
| Union[ | ||
| sqlalchemy.pool._ConnectionFairy, | ||
| spanner_dbapi.connection.Connection, | ||
| spanner_dbapi.driver_connection.Connection, | ||
| ] | ||
| ): | ||
| Database connection proxy object or the connection itself. | ||
|
|
@@ -1718,7 +1722,7 @@ def set_isolation_level(self, conn_proxy, level): | |
| if isinstance(conn_proxy, spanner_dbapi.Connection): | ||
| conn = conn_proxy | ||
| else: | ||
| conn = conn_proxy.connection | ||
| conn = conn_proxy.driver_connection | ||
|
|
||
| if level == "AUTOCOMMIT": | ||
| conn.autocommit = True | ||
|
|
@@ -1735,7 +1739,7 @@ def get_isolation_level(self, conn_proxy): | |
| conn_proxy ( | ||
| Union[ | ||
| sqlalchemy.pool._ConnectionFairy, | ||
| spanner_dbapi.connection.Connection, | ||
| spanner_dbapi.driver_connection.Connection, | ||
| ] | ||
| ): | ||
| Database connection proxy object or the connection itself. | ||
|
|
@@ -1746,7 +1750,7 @@ def get_isolation_level(self, conn_proxy): | |
| if isinstance(conn_proxy, spanner_dbapi.Connection): | ||
| conn = conn_proxy | ||
| else: | ||
| conn = conn_proxy.connection | ||
| conn = conn_proxy.driver_connection | ||
|
|
||
| if conn.autocommit: | ||
| return "AUTOCOMMIT" | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: can you remove the unrelated changes from this pull request (and potentially open a separate pull request for formatting issues, if any)