|
29 | 29 |
|
30 | 30 | logger = logging.getLogger()
|
31 | 31 |
|
32 |
| -# [START cloud_sql_server_sqlalchemy_create] |
33 |
| -# Remember - storing secrets in plaintext is potentially unsafe. Consider using |
34 |
| -# something like https://cloud.google.com/kms/ to help keep secrets secret. |
35 |
| -db_user = os.environ.get("DB_USER") |
36 |
| -db_pass = os.environ.get("DB_PASS") |
37 |
| -db_name = os.environ.get("DB_NAME") |
38 |
| - |
39 |
| -# When deployed to GAE Flex for TCP, use "172.17.0.1" to connect |
40 |
| -host = "172.17.0.1" if os.environ.get("DEPLOYED") else "127.0.0.1" |
41 |
| - |
42 |
| -# The SQLAlchemy engine will help manage interactions, including automatically |
43 |
| -# managing a pool of connections to your database |
44 |
| -db = sqlalchemy.create_engine( |
45 |
| - # Equivalent URL: |
46 |
| - # mssql+pyodbc://<db_user>:<db_pass>@/<host>:<port>/<db_name>?driver=ODBC+Driver+17+for+SQL+Server |
47 |
| - sqlalchemy.engine.url.URL( |
48 |
| - "mssql+pyodbc", |
49 |
| - username=db_user, |
50 |
| - password=db_pass, |
51 |
| - database=db_name, |
52 |
| - host=host, |
53 |
| - port=1433, |
54 |
| - query={"driver": "ODBC Driver 17 for SQL Server"}, |
55 |
| - ), |
56 |
| - # ... Specify additional properties here. |
57 |
| - # [START_EXCLUDE] |
58 |
| - # [START cloud_sql_server_sqlalchemy_limit] |
59 |
| - # Pool size is the maximum number of permanent connections to keep. |
60 |
| - pool_size=5, |
61 |
| - # Temporarily exceeds the set pool_size if no connections are available. |
62 |
| - max_overflow=2, |
63 |
| - # The total number of concurrent connections for your application will be |
64 |
| - # a total of pool_size and max_overflow. |
65 |
| - # [END cloud_sql_server_sqlalchemy_limit] |
66 |
| - # [START cloud_sql_server_sqlalchemy_backoff] |
67 |
| - # SQLAlchemy automatically uses delays between failed connection attempts, |
68 |
| - # but provides no arguments for configuration. |
69 |
| - # [END cloud_sql_server_sqlalchemy_backoff] |
70 |
| - # [START cloud_sql_server_sqlalchemy_timeout] |
71 |
| - # 'pool_timeout' is the maximum number of seconds to wait when retrieving a |
72 |
| - # new connection from the pool. After the specified amount of time, an |
73 |
| - # exception will be thrown. |
74 |
| - pool_timeout=30, # 30 seconds |
75 |
| - # [END cloud_sql_server_sqlalchemy_timeout] |
76 |
| - # [START cloud_sql_server_sqlalchemy_lifetime] |
77 |
| - # 'pool_recycle' is the maximum number of seconds a connection can persist. |
78 |
| - # Connections that live longer than the specified amount of time will be |
79 |
| - # reestablished |
80 |
| - pool_recycle=1800, # 30 minutes |
81 |
| - # [END cloud_sql_server_sqlalchemy_lifetime] |
82 |
| - echo=True # debug |
83 |
| - # [END_EXCLUDE] |
84 |
| -) |
85 |
| -# [END cloud_sql_server_sqlalchemy_create] |
| 32 | + |
| 33 | +def init_tcp_connection_engine(): |
| 34 | + # [START cloud_sql_server_sqlalchemy_create_tcp] |
| 35 | + # Remember - storing secrets in plaintext is potentially unsafe. Consider using |
| 36 | + # something like https://cloud.google.com/kms/ to help keep secrets secret. |
| 37 | + db_user = os.environ["DB_USER"] |
| 38 | + db_pass = os.environ["DB_PASS"] |
| 39 | + db_name = os.environ["DB_NAME"] |
| 40 | + |
| 41 | + # Extract host and port from environment variable DB_HOST |
| 42 | + host_args = os.environ["DB_HOST"].split(":") |
| 43 | + host, port = host_args[0], int(host_args[1]) |
| 44 | + |
| 45 | + # The SQLAlchemy engine will help manage interactions, including automatically |
| 46 | + # managing a pool of connections to your database |
| 47 | + pool = sqlalchemy.create_engine( |
| 48 | + # Equivalent URL: |
| 49 | + # mssql+pyodbc://<db_user>:<db_pass>@/<host>:<port>/<db_name>?driver=ODBC+Driver+17+for+SQL+Server |
| 50 | + sqlalchemy.engine.url.URL( |
| 51 | + "mssql+pyodbc", |
| 52 | + username=db_user, |
| 53 | + password=db_pass, |
| 54 | + database=db_name, |
| 55 | + host=host, |
| 56 | + port=port, |
| 57 | + query={"driver": "ODBC Driver 17 for SQL Server"}, |
| 58 | + ), |
| 59 | + # ... Specify additional properties here. |
| 60 | + # [START_EXCLUDE] |
| 61 | + # [START cloud_sql_server_sqlalchemy_limit] |
| 62 | + # Pool size is the maximum number of permanent connections to keep. |
| 63 | + pool_size=5, |
| 64 | + # Temporarily exceeds the set pool_size if no connections are available. |
| 65 | + max_overflow=2, |
| 66 | + # The total number of concurrent connections for your application will be |
| 67 | + # a total of pool_size and max_overflow. |
| 68 | + # [END cloud_sql_server_sqlalchemy_limit] |
| 69 | + # [START cloud_sql_server_sqlalchemy_backoff] |
| 70 | + # SQLAlchemy automatically uses delays between failed connection attempts, |
| 71 | + # but provides no arguments for configuration. |
| 72 | + # [END cloud_sql_server_sqlalchemy_backoff] |
| 73 | + # [START cloud_sql_server_sqlalchemy_timeout] |
| 74 | + # 'pool_timeout' is the maximum number of seconds to wait when retrieving a |
| 75 | + # new connection from the pool. After the specified amount of time, an |
| 76 | + # exception will be thrown. |
| 77 | + pool_timeout=30, # 30 seconds |
| 78 | + # [END cloud_sql_server_sqlalchemy_timeout] |
| 79 | + # [START cloud_sql_server_sqlalchemy_lifetime] |
| 80 | + # 'pool_recycle' is the maximum number of seconds a connection can persist. |
| 81 | + # Connections that live longer than the specified amount of time will be |
| 82 | + # reestablished |
| 83 | + pool_recycle=1800, # 30 minutes |
| 84 | + # [END cloud_sql_server_sqlalchemy_lifetime] |
| 85 | + echo=True # debug |
| 86 | + # [END_EXCLUDE] |
| 87 | + ) |
| 88 | + # [END cloud_sql_server_sqlalchemy_create_tcp] |
| 89 | + |
| 90 | + return pool |
| 91 | + |
| 92 | + |
| 93 | +db = init_tcp_connection_engine() |
86 | 94 |
|
87 | 95 |
|
88 | 96 | @app.before_first_request
|
|
0 commit comments