File tree Expand file tree Collapse file tree 6 files changed +42
-19
lines changed Expand file tree Collapse file tree 6 files changed +42
-19
lines changed Original file line number Diff line number Diff line change @@ -36,11 +36,11 @@ def getconn() -> pymysql.connections.Connection:
3636 )
3737 return conn
3838
39- engine = sqlalchemy .create_engine (
39+ pool = sqlalchemy .create_engine (
4040 "mysql+pymysql://" ,
4141 creator = getconn ,
4242 )
43- return engine
43+ return pool
4444
4545
4646def test_connector_with_credentials () -> None :
Original file line number Diff line number Diff line change @@ -38,11 +38,11 @@ def getconn() -> pymysql.connections.Connection:
3838 )
3939 return conn
4040
41- engine = sqlalchemy .create_engine (
41+ pool = sqlalchemy .create_engine (
4242 "mysql+pymysql://" ,
4343 creator = getconn ,
4444 )
45- return engine
45+ return pool
4646
4747
4848def test_public_ip () -> None :
Original file line number Diff line number Diff line change 1717import uuid
1818from typing import Generator
1919
20- import pg8000
2120import pytest
21+
22+ # [START cloud_sql_connector_postgres_pg8000]
23+ import pg8000
2224import sqlalchemy
2325from google .cloud .sql .connector import Connector
2426
27+ # [END cloud_sql_connector_postgres_pg8000]
28+
2529table_name = f"books_{ uuid .uuid4 ().hex } "
2630
2731
3034# 'creator' argument to 'create_engine'
3135def init_connection_engine () -> sqlalchemy .engine .Engine :
3236 def getconn () -> pg8000 .dbapi .Connection :
37+ # initialize Connector object for connections to Cloud SQL
3338 with Connector () as connector :
3439 conn : pg8000 .dbapi .Connection = connector .connect (
3540 os .environ ["POSTGRES_CONNECTION_NAME" ],
@@ -40,12 +45,13 @@ def getconn() -> pg8000.dbapi.Connection:
4045 )
4146 return conn
4247
43- engine = sqlalchemy .create_engine (
48+ # create SQLAlchemy connection pool
49+ pool = sqlalchemy .create_engine (
4450 "postgresql+pg8000://" ,
4551 creator = getconn ,
4652 )
47- engine .dialect .description_encoding = None
48- return engine
53+ pool .dialect .description_encoding = None
54+ return pool
4955
5056
5157# [END cloud_sql_connector_postgres_pg8000]
Original file line number Diff line number Diff line change 1717import uuid
1818from typing import Generator
1919
20- import pg8000
20+ # [START cloud_sql_connector_postgres_pg8000_iam_auth]
2121import pytest
22+ import pg8000
2223import sqlalchemy
2324from google .cloud .sql .connector import Connector
2425
26+ # [END cloud_sql_connector_postgres_pg8000_iam_auth]
27+
2528table_name = f"books_{ uuid .uuid4 ().hex } "
2629
2730
2831# [START cloud_sql_connector_postgres_pg8000_iam_auth]
2932# The Cloud SQL Python Connector can be used along with SQLAlchemy using the
3033# 'creator' argument to 'create_engine'
3134def init_connection_engine () -> sqlalchemy .engine .Engine :
35+ # initialize Connector object for connections to Cloud SQL
3236 def getconn () -> pg8000 .dbapi .Connection :
3337 with Connector () as connector :
3438 conn : pg8000 .dbapi .Connection = connector .connect (
@@ -40,12 +44,13 @@ def getconn() -> pg8000.dbapi.Connection:
4044 )
4145 return conn
4246
43- engine = sqlalchemy .create_engine (
47+ # create SQLAlchemy connection pool
48+ pool = sqlalchemy .create_engine (
4449 "postgresql+pg8000://" ,
4550 creator = getconn ,
4651 )
47- engine .dialect .description_encoding = None
48- return engine
52+ pool .dialect .description_encoding = None
53+ return pool
4954
5055
5156# [END cloud_sql_connector_postgres_pg8000_iam_auth]
Original file line number Diff line number Diff line change 1717import uuid
1818from typing import Generator
1919
20- import pymysql
2120import pytest
21+
22+ # [START cloud_sql_connector_mysql_pymysql]
23+ import pymysql
2224import sqlalchemy
2325from google .cloud .sql .connector import Connector
2426
27+ # [END cloud_sql_connector_mysql_pymysql]
28+
2529table_name = f"books_{ uuid .uuid4 ().hex } "
2630
2731
3034# 'creator' argument to 'create_engine'
3135def init_connection_engine () -> sqlalchemy .engine .Engine :
3236 def getconn () -> pymysql .connections .Connection :
37+ # initialize Connector object for connections to Cloud SQL
3338 with Connector () as connector :
3439 conn : pymysql .connections .Connection = connector .connect (
3540 os .environ ["MYSQL_CONNECTION_NAME" ],
@@ -40,11 +45,12 @@ def getconn() -> pymysql.connections.Connection:
4045 )
4146 return conn
4247
43- engine = sqlalchemy .create_engine (
48+ # create SQLAlchemy connection pool
49+ pool = sqlalchemy .create_engine (
4450 "mysql+pymysql://" ,
4551 creator = getconn ,
4652 )
47- return engine
53+ return pool
4854
4955
5056# [END cloud_sql_connector_mysql_pymysql]
Original file line number Diff line number Diff line change 1717import uuid
1818from typing import Generator
1919
20- import pytds
2120import pytest
21+
22+ # [START cloud_sql_connector_mysql_pytds]
23+ import pytds
2224import sqlalchemy
2325from google .cloud .sql .connector import Connector
2426
27+ # [END cloud_sql_connector_mysql_pytds]
28+
2529table_name = f"books_{ uuid .uuid4 ().hex } "
2630
2731
3135# 'sqlalchemy-pytds` in your dependencies
3236def init_connection_engine () -> sqlalchemy .engine .Engine :
3337 def getconn () -> pytds .Connection :
38+ # initialize Connector object for connections to Cloud SQL
3439 with Connector () as connector :
3540 conn = connector .connect (
3641 os .environ ["SQLSERVER_CONNECTION_NAME" ],
@@ -41,12 +46,13 @@ def getconn() -> pytds.Connection:
4146 )
4247 return conn
4348
44- engine = sqlalchemy .create_engine (
49+ # create SQLAlchemy connection pool
50+ pool = sqlalchemy .create_engine (
4551 "mssql+pytds://localhost" ,
4652 creator = getconn ,
4753 )
48- engine .dialect .description_encoding = None
49- return engine
54+ pool .dialect .description_encoding = None
55+ return pool
5056
5157
5258# [END cloud_sql_connector_mysql_pytds]
You can’t perform that action at this time.
0 commit comments