Skip to content

Commit cbd2049

Browse files
chore(deps): update sqlalchemy to v2 (#605)
1 parent dc60cc5 commit cbd2049

9 files changed

+59
-39
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ insert_stmt = sqlalchemy.text(
138138

139139
with pool.connect() as db_conn:
140140
# insert into database
141-
db_conn.execute(insert_stmt, id="book1", title="Book One")
141+
db_conn.execute(insert_stmt, parameters={"id": "book1", "title": "Book One"})
142142

143143
# query database
144-
result = db_conn.execute("SELECT * from my_table").fetchall()
144+
result = db_conn.execute(sqlalchemy.text("SELECT * from my_table")).fetchall()
145145

146146
# Do something with the results
147147
for row in result:
@@ -186,6 +186,7 @@ Connector as a context manager:
186186

187187
```python
188188
from google.cloud.sql.connector import Connector
189+
import sqlalchemy
189190

190191
# build connection
191192
def getconn() -> pymysql.connections.Connection:
@@ -213,10 +214,10 @@ insert_stmt = sqlalchemy.text(
213214
# interact with Cloud SQL database using connection pool
214215
with pool.connect() as db_conn:
215216
# insert into database
216-
db_conn.execute(insert_stmt, id="book1", title="Book One")
217+
db_conn.execute(insert_stmt, parameters={"id": "book1", "title": "Book One"})
217218

218219
# query database
219-
result = db_conn.execute("SELECT * from my_table").fetchall()
220+
result = db_conn.execute(sqlalchemy.text("SELECT * from my_table")).fetchall()
220221

221222
# Do something with the results
222223
for row in result:

requirements-test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pytest==7.2.1
22
mock==5.0.1
33
pytest-cov==4.0.0
44
pytest-asyncio==0.20.3
5-
SQLAlchemy==1.4.46
5+
SQLAlchemy==2.0.2
66
sqlalchemy-pytds==0.3.4
77
flake8==5.0.4
88
flake8-annotations==2.9.1

tests/system/test_connector_object.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,22 @@ def getconn() -> pymysql.connections.Connection:
4545
pool = sqlalchemy.create_engine(
4646
"mysql+pymysql://",
4747
creator=getconn,
48+
execution_options={"isolation_level": "AUTOCOMMIT"},
4849
)
4950
return pool
5051

5152

5253
def test_connector_with_credentials() -> None:
5354
"""Test Connector object connection with credentials loaded from file."""
54-
credentials, project = google.auth.load_credentials_from_file(
55+
credentials, _ = google.auth.load_credentials_from_file(
5556
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
5657
)
5758
custom_connector = Connector(credentials=credentials)
5859
try:
5960
pool = init_connection_engine(custom_connector)
6061

6162
with pool.connect() as conn:
62-
conn.execute("SELECT 1")
63+
conn.execute(sqlalchemy.text("SELECT 1"))
6364

6465
except Exception as e:
6566
logging.exception("Failed to connect with credentials from file!", e)
@@ -76,10 +77,10 @@ def test_multiple_connectors() -> None:
7677
pool2 = init_connection_engine(second_connector)
7778

7879
with pool.connect() as conn:
79-
conn.execute("SELECT 1")
80+
conn.execute(sqlalchemy.text("SELECT 1"))
8081

8182
with pool2.connect() as conn:
82-
conn.execute("SELECT 1")
83+
conn.execute(sqlalchemy.text("SELECT 1"))
8384

8485
instance_connection_string = os.environ["MYSQL_CONNECTION_NAME"]
8586
assert instance_connection_string in first_connector._instances
@@ -108,7 +109,7 @@ def get_time() -> datetime.datetime:
108109

109110
# connect to database and get current time
110111
with pool.connect() as conn:
111-
current_time = conn.execute("SELECT NOW()").fetchone()
112+
current_time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone()
112113

113114
# close connector
114115
default_connector.close()
@@ -127,7 +128,7 @@ def test_connector_as_context_manager() -> None:
127128
pool = init_connection_engine(connector)
128129

129130
with pool.connect() as conn:
130-
conn.execute("SELECT 1")
131+
conn.execute(sqlalchemy.text("SELECT 1"))
131132

132133

133134
def test_connector_with_custom_loop() -> None:
@@ -141,7 +142,7 @@ def test_connector_with_custom_loop() -> None:
141142
pool = init_connection_engine(connector)
142143

143144
with pool.connect() as conn:
144-
result = conn.execute("SELECT 1").fetchone()
145+
result = conn.execute(sqlalchemy.text("SELECT 1")).fetchone()
145146
assert result[0] == 1
146147
# assert that Connector does not start its own thread
147148
assert connector._thread is None

tests/system/test_ip_types.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def getconn() -> pymysql.connections.Connection:
4141
pool = sqlalchemy.create_engine(
4242
"mysql+pymysql://",
4343
creator=getconn,
44+
execution_options={"isolation_level": "AUTOCOMMIT"},
4445
)
4546
return pool
4647

@@ -49,12 +50,12 @@ def test_public_ip() -> None:
4950
with Connector() as connector:
5051
pool = init_connection_engine(connector, IPTypes.PUBLIC)
5152
with pool.connect() as conn:
52-
conn.execute("SELECT 1")
53+
conn.execute(sqlalchemy.text("SELECT 1"))
5354

5455

5556
@pytest.mark.private_ip
5657
def test_private_ip() -> None:
5758
with Connector() as connector:
5859
pool = init_connection_engine(connector, IPTypes.PRIVATE)
5960
with pool.connect() as conn:
60-
conn.execute("SELECT 1")
61+
conn.execute(sqlalchemy.text("SELECT 1"))

tests/system/test_pg8000_connection.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def getconn() -> pg8000.dbapi.Connection:
4949
pool = sqlalchemy.create_engine(
5050
"postgresql+pg8000://",
5151
creator=getconn,
52+
execution_options={"isolation_level": "AUTOCOMMIT"},
5253
)
5354
pool.dialect.description_encoding = None
5455
return pool
@@ -63,23 +64,25 @@ def setup() -> Generator:
6364

6465
with pool.connect() as conn:
6566
conn.execute(
66-
f"CREATE TABLE IF NOT EXISTS {table_name}"
67-
" ( id CHAR(20) NOT NULL, title TEXT NOT NULL );"
67+
sqlalchemy.text(
68+
f"CREATE TABLE IF NOT EXISTS {table_name}"
69+
" ( id CHAR(20) NOT NULL, title TEXT NOT NULL );"
70+
)
6871
)
6972

7073
yield pool
7174

7275
with pool.connect() as conn:
73-
conn.execute(f"DROP TABLE IF EXISTS {table_name}")
76+
conn.execute(sqlalchemy.text(f"DROP TABLE IF EXISTS {table_name}"))
7477

7578

7679
def test_pooled_connection_with_pg8000(pool: sqlalchemy.engine.Engine) -> None:
7780
insert_stmt = sqlalchemy.text(
7881
f"INSERT INTO {table_name} (id, title) VALUES (:id, :title)",
7982
)
8083
with pool.connect() as conn:
81-
conn.execute(insert_stmt, id="book1", title="Book One")
82-
conn.execute(insert_stmt, id="book2", title="Book Two")
84+
conn.execute(insert_stmt, parameters={"id": "book1", "title": "Book One"})
85+
conn.execute(insert_stmt, parameters={"id": "book2", "title": "Book Two"})
8386

8487
select_stmt = sqlalchemy.text(f"SELECT title FROM {table_name} ORDER BY ID;")
8588
with pool.connect() as conn:

tests/system/test_pg8000_iam_auth.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def getconn() -> pg8000.dbapi.Connection:
4848
pool = sqlalchemy.create_engine(
4949
"postgresql+pg8000://",
5050
creator=getconn,
51+
execution_options={"isolation_level": "AUTOCOMMIT"},
5152
)
5253
pool.dialect.description_encoding = None
5354
return pool
@@ -62,23 +63,25 @@ def setup() -> Generator:
6263

6364
with pool.connect() as conn:
6465
conn.execute(
65-
f"CREATE TABLE IF NOT EXISTS {table_name}"
66-
" ( id CHAR(20) NOT NULL, title TEXT NOT NULL );"
66+
sqlalchemy.text(
67+
f"CREATE TABLE IF NOT EXISTS {table_name}"
68+
" ( id CHAR(20) NOT NULL, title TEXT NOT NULL );"
69+
)
6770
)
6871

6972
yield pool
7073

7174
with pool.connect() as conn:
72-
conn.execute(f"DROP TABLE IF EXISTS {table_name}")
75+
conn.execute(sqlalchemy.text(f"DROP TABLE IF EXISTS {table_name}"))
7376

7477

7578
def test_pooled_connection_with_pg8000_iam_auth(pool: sqlalchemy.engine.Engine) -> None:
7679
insert_stmt = sqlalchemy.text(
7780
f"INSERT INTO {table_name} (id, title) VALUES (:id, :title)",
7881
)
7982
with pool.connect() as conn:
80-
conn.execute(insert_stmt, id="book1", title="Book One")
81-
conn.execute(insert_stmt, id="book2", title="Book Two")
83+
conn.execute(insert_stmt, parameters={"id": "book1", "title": "Book One"})
84+
conn.execute(insert_stmt, parameters={"id": "book2", "title": "Book Two"})
8285

8386
select_stmt = sqlalchemy.text(f"SELECT title FROM {table_name} ORDER BY ID;")
8487
with pool.connect() as conn:

tests/system/test_pymysql_connection.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def getconn() -> pymysql.connections.Connection:
4949
pool = sqlalchemy.create_engine(
5050
"mysql+pymysql://",
5151
creator=getconn,
52+
execution_options={"isolation_level": "AUTOCOMMIT"},
5253
)
5354
return pool
5455

@@ -62,23 +63,25 @@ def setup() -> Generator:
6263

6364
with pool.connect() as conn:
6465
conn.execute(
65-
f"CREATE TABLE IF NOT EXISTS `{table_name}`"
66-
" ( id CHAR(20) NOT NULL, title TEXT NOT NULL );"
66+
sqlalchemy.text(
67+
f"CREATE TABLE IF NOT EXISTS `{table_name}`"
68+
" ( id CHAR(20) NOT NULL, title TEXT NOT NULL );"
69+
)
6770
)
6871

6972
yield pool
7073

7174
with pool.connect() as conn:
72-
conn.execute(f"DROP TABLE IF EXISTS `{table_name}`")
75+
conn.execute(sqlalchemy.text(f"DROP TABLE IF EXISTS `{table_name}`"))
7376

7477

7578
def test_pooled_connection_with_pymysql(pool: sqlalchemy.engine.Engine) -> None:
7679
insert_stmt = sqlalchemy.text(
7780
f"INSERT INTO {table_name} (id, title) VALUES (:id, :title)",
7881
)
7982
with pool.connect() as conn:
80-
conn.execute(insert_stmt, id="book1", title="Book One")
81-
conn.execute(insert_stmt, id="book2", title="Book Two")
83+
conn.execute(insert_stmt, parameters={"id": "book1", "title": "Book One"})
84+
conn.execute(insert_stmt, parameters={"id": "book2", "title": "Book Two"})
8285

8386
select_stmt = sqlalchemy.text(f"SELECT title FROM {table_name} ORDER BY ID;")
8487
with pool.connect() as conn:

tests/system/test_pymysql_iam_auth.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def getconn() -> pymysql.connections.Connection:
4545
pool = sqlalchemy.create_engine(
4646
"mysql+pymysql://",
4747
creator=getconn,
48+
execution_options={"isolation_level": "AUTOCOMMIT"},
4849
)
4950
return pool
5051

@@ -58,14 +59,16 @@ def setup() -> Generator:
5859

5960
with pool.connect() as conn:
6061
conn.execute(
61-
f"CREATE TABLE IF NOT EXISTS {table_name}"
62-
" ( id CHAR(20) NOT NULL, title TEXT NOT NULL );"
62+
sqlalchemy.text(
63+
f"CREATE TABLE IF NOT EXISTS {table_name}"
64+
" ( id CHAR(20) NOT NULL, title TEXT NOT NULL );"
65+
)
6366
)
6467

6568
yield pool
6669

6770
with pool.connect() as conn:
68-
conn.execute(f"DROP TABLE IF EXISTS {table_name}")
71+
conn.execute(sqlalchemy.text(f"DROP TABLE IF EXISTS {table_name}"))
6972

7073

7174
def test_pooled_connection_with_pymysql_iam_auth(
@@ -75,8 +78,8 @@ def test_pooled_connection_with_pymysql_iam_auth(
7578
f"INSERT INTO {table_name} (id, title) VALUES (:id, :title)",
7679
)
7780
with pool.connect() as conn:
78-
conn.execute(insert_stmt, id="book1", title="Book One")
79-
conn.execute(insert_stmt, id="book2", title="Book Two")
81+
conn.execute(insert_stmt, parameters={"id": "book1", "title": "Book One"})
82+
conn.execute(insert_stmt, parameters={"id": "book2", "title": "Book Two"})
8083

8184
select_stmt = sqlalchemy.text(f"SELECT title FROM {table_name} ORDER BY ID;")
8285
with pool.connect() as conn:

tests/system/test_pytds_connection.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,28 @@ def setup() -> Generator:
6464

6565
with pool.connect() as conn:
6666
conn.execute(
67-
f"CREATE TABLE {table_name}"
68-
" ( id CHAR(20) NOT NULL, title TEXT NOT NULL );"
67+
sqlalchemy.text(
68+
f"CREATE TABLE {table_name}"
69+
" ( id CHAR(20) NOT NULL, title TEXT NOT NULL );"
70+
)
6971
)
72+
conn.commit()
7073

7174
yield pool
7275

7376
with pool.connect() as conn:
74-
conn.execute(f"DROP TABLE {table_name}")
77+
conn.execute(sqlalchemy.text(f"DROP TABLE {table_name}"))
78+
conn.commit()
7579

7680

7781
def test_pooled_connection_with_pytds(pool: sqlalchemy.engine.Engine) -> None:
7882
insert_stmt = sqlalchemy.text(
7983
f"INSERT INTO {table_name} (id, title) VALUES (:id, :title)",
8084
)
8185
with pool.connect() as conn:
82-
conn.execute(insert_stmt, id="book1", title="Book One")
83-
conn.execute(insert_stmt, id="book2", title="Book Two")
86+
conn.execute(insert_stmt, parameters={"id": "book1", "title": "Book One"})
87+
conn.execute(insert_stmt, parameters={"id": "book2", "title": "Book Two"})
88+
conn.commit()
8489

8590
select_stmt = sqlalchemy.text(f"SELECT title FROM {table_name} ORDER BY ID;")
8691
with pool.connect() as conn:

0 commit comments

Comments
 (0)