25
25
db_user = os .environ .get ("DB_USER" )
26
26
db_pass = os .environ .get ("DB_PASS" )
27
27
db_name = os .environ .get ("DB_NAME" )
28
- cloud_sql_instance_name = os .environ .get ("CLOUD_SQL_INSTANCE_NAME " )
28
+ cloud_sql_connection_name = os .environ .get ("CLOUD_SQL_CONNECTION_NAME " )
29
29
30
30
app = Flask (__name__ )
31
31
32
32
logger = logging .getLogger ()
33
33
34
- # [START cloud_sql_postgres_connection_pool ]
34
+ # [START cloud_sql_postgres_sqlalchemy_create ]
35
35
# The SQLAlchemy engine will help manage interactions, including automatically
36
36
# managing a pool of connections to your database
37
37
db = sqlalchemy .create_engine (
43
43
password = db_pass ,
44
44
database = db_name ,
45
45
query = {
46
- 'unix_sock' : '/cloudsql/{}' .format (cloud_sql_instance_name )
46
+ 'unix_sock' : '/cloudsql/{}' .format (cloud_sql_connection_name )
47
47
}
48
48
),
49
49
# ... Specify additional properties here.
50
50
# [START_EXCLUDE]
51
51
52
- # [START cloud_sql_postgres_limit_connections ]
52
+ # [START cloud_sql_postgres_sqlalchemy_limit ]
53
53
# Pool size is the maximum number of permanent connections to keep.
54
54
pool_size = 5 ,
55
55
# Temporarily exceeds the set pool_size if no connections are available.
56
56
max_overflow = 2 ,
57
57
# The total number of concurrent connections for your application will be
58
58
# a total of pool_size and max_overflow.
59
- # [END cloud_sql_postgres_limit_connections ]
59
+ # [END cloud_sql_postgres_sqlalchemy_limit ]
60
60
61
- # [START cloud_sql_postgres_connection_backoff ]
61
+ # [START cloud_sql_postgres_sqlalchemy_backoff ]
62
62
# SQLAlchemy automatically uses delays between failed connection attempts,
63
63
# but provides no arguments for configuration.
64
- # [END cloud_sql_postgres_connection_backoff ]
64
+ # [END cloud_sql_postgres_sqlalchemy_backoff ]
65
65
66
- # [START cloud_sql_postgres_connection_timeout ]
66
+ # [START cloud_sql_postgres_sqlalchemy_timeout ]
67
67
# 'pool_timeout' is the maximum number of seconds to wait when retrieving a
68
68
# new connection from the pool. After the specified amount of time, an
69
69
# exception will be thrown.
70
70
pool_timeout = 30 , # 30 seconds
71
- # [END cloud_sql_postgres_connection_timeout ]
71
+ # [END cloud_sql_postgres_sqlalchemy_timeout ]
72
72
73
- # [START cloud_sql_postgres_connection_lifetime ]
73
+ # [START cloud_sql_postgres_sqlalchemy_lifetime ]
74
74
# 'pool_recycle' is the maximum number of seconds a connection can persist.
75
75
# Connections that live longer than the specified amount of time will be
76
76
# reestablished
77
77
pool_recycle = 1800 , # 30 minutes
78
- # [END cloud_sql_postgres_connection_lifetime ]
78
+ # [END cloud_sql_postgres_sqlalchemy_lifetime ]
79
79
80
80
# [END_EXCLUDE]
81
81
)
82
- # [END cloud_sql_postgres_connection_pool ]
82
+ # [END cloud_sql_postgres_sqlalchemy_create ]
83
83
84
84
85
85
@app .before_first_request
@@ -139,7 +139,7 @@ def save_vote():
139
139
status = 400
140
140
)
141
141
142
- # [START cloud_sql_postgres_example_statement ]
142
+ # [START cloud_sql_postgres_sqlalchemy_connection ]
143
143
# Preparing a statement before hand can help protect against injections.
144
144
stmt = sqlalchemy .text (
145
145
"INSERT INTO votes (time_cast, candidate)"
@@ -161,7 +161,7 @@ def save_vote():
161
161
"application logs for more details."
162
162
)
163
163
# [END_EXCLUDE]
164
- # [END cloud_sql_postgres_example_statement ]
164
+ # [END cloud_sql_postgres_sqlalchemy_connection ]
165
165
166
166
return Response (
167
167
status = 200 ,
0 commit comments