-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Added samples for using Cloud SQL with App Engine Python 3.7 Standard #1672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
63c0f7e
Added samples for using Cloud SQL with App Engine Python 3.7 Standard
michaelawyu 93c382d
Minor fix
michaelawyu 4dfb600
Minor fixes.
michaelawyu e3f09e3
Minor fixes.
michaelawyu 2cf1fd8
Minor fix.
e1836ca
Updated sample to use PyMySQL + SQLAlchemy.
6828bb0
Merge branch 'master' into michaelawyu-patch-appengine-py37-cloudsql
76a8742
Minor fix.
5d05163
Minor fix: remove F-strings
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # [START gae_python37_cloudsql_config] | ||
| runtime: python37 | ||
|
|
||
| env_variables: | ||
| CLOUDSQL_USERNAME: YOUR-USERNAME | ||
| CLOUDSQL_PASSWORD: YOUR-PASSWORD | ||
| CLOUDSQL_DATABASE_NAME: YOUR-DATABASE | ||
| CLOUDSQL_CONNECTION_NAME: YOUR-CONNECTION-NAME | ||
| # [END gae_python37_cloudsql_config] |
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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # Copyright 2018 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # [START gae_python37_cloudsql_mysql] | ||
| import os | ||
|
|
||
| from flask import Flask | ||
| import pymysql | ||
|
|
||
| db_user = os.environ.get('CLOUD_SQL_USERNAME') | ||
| db_password = os.environ.get('CLOUD_SQL_PASSWORD') | ||
| db_name = os.environ.get('CLOUD_SQL_DATABASE_NAME') | ||
| db_connection_name = os.environ.get('CLOUD_SQL_CONNECTION_NAME') | ||
|
|
||
| app = Flask(__name__) | ||
|
|
||
|
|
||
| @app.route('/') | ||
| def main(): | ||
| # When deployed to App Engine, the `GAE_ENV` environment variable will be | ||
| # set to `standard` | ||
| if os.environ.get('GAE_ENV'): | ||
| # If deployed, use the local socket interface for accessing Cloud SQL | ||
| host = '/cloudsql/{}'.format(db_connection_name) | ||
| else: | ||
| # If running locally, use the TCP connections instead | ||
| # Set up Cloud SQL Proxy (cloud.google.com/sql/docs/mysql/sql-proxy) | ||
| # so that your application can use 127.0.0.1:3306 to connect to your | ||
| # Cloud SQL instance | ||
| host = '127.0.0.1' | ||
|
|
||
| cnx = pymysql.connect(user=db_user, password=db_password, | ||
| host=host, db=db_name) | ||
| with cnx.cursor() as cursor: | ||
| cursor.execute('SELECT NOW() as now;') | ||
| result = cursor.fetchall() | ||
| current_time = result[0][0] | ||
| cnx.close() | ||
|
|
||
| return str(current_time) | ||
| # [END gae_python37_cloudsql_mysql] | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| app.run(host='127.0.0.1', port=8080, debug=True) |
63 changes: 63 additions & 0 deletions
63
appengine/standard_python37/cloudsql/main_mysql_pooling.py
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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Copyright 2018 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # [START gae_python37_cloudsql_mysql_pooling] | ||
| import os | ||
|
|
||
| from flask import Flask | ||
| import sqlalchemy | ||
|
|
||
| db_user = os.environ.get('CLOUD_SQL_USERNAME') | ||
| db_password = os.environ.get('CLOUD_SQL_PASSWORD') | ||
| db_name = os.environ.get('CLOUD_SQL_DATABASE_NAME') | ||
| db_connection_name = os.environ.get('CLOUD_SQL_CONNECTION_NAME') | ||
|
|
||
| # When deployed to App Engine, the `GAE_ENV` environment variable will be | ||
| # set to `standard` | ||
| if os.environ.get('GAE_ENV'): | ||
| # If deployed, use the local socket interface for accessing Cloud SQL | ||
| host = '/cloudsql/{}'.format(db_connection_name) | ||
| else: | ||
| # If running locally, use the TCP connections instead | ||
| # Set up Cloud SQL Proxy (cloud.google.com/sql/docs/mysql/sql-proxy) | ||
| # so that your application can use 127.0.0.1:3306 to connect to your | ||
| # Cloud SQL instance | ||
| host = '127.0.0.1' | ||
|
|
||
| # The Engine object returned by create_engine() has a QueuePool integrated | ||
| # See https://docs.sqlalchemy.org/en/latest/core/pooling.html for more | ||
| # information | ||
| engine = sqlalchemy.create_engine('mysql+pymysql://{}:{}@{}/{}'.format( | ||
| db_user, db_password, host, db_name | ||
| ), pool_size=3) | ||
|
|
||
| app = Flask(__name__) | ||
|
|
||
|
|
||
| @app.route('/') | ||
| def main(): | ||
| cnx = engine.connect() | ||
| cursor = cnx.execute('SELECT NOW() as now;') | ||
| result = cursor.fetchall() | ||
| current_time = result[0][0] | ||
| # If the connection comes from a pool, close() will send the connection | ||
| # back to the pool instead of closing it | ||
| cnx.close() | ||
|
|
||
| return str(current_time) | ||
| # [END gae_python37_cloudsql_mysql_pooling] | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| app.run(host='127.0.0.1', port=8080, debug=True) | ||
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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # Copyright 2018 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # [START gae_python37_cloudsql_psql] | ||
| import os | ||
|
|
||
| from flask import Flask | ||
| import psycopg2 | ||
|
|
||
| db_user = os.environ.get('CLOUD_SQL_USERNAME') | ||
| db_password = os.environ.get('CLOUD_SQL_PASSWORD') | ||
| db_name = os.environ.get('CLOUD_SQL_DATABASE_NAME') | ||
| db_connection_name = os.environ.get('CLOUD_SQL_CONNECTION_NAME') | ||
|
|
||
| app = Flask(__name__) | ||
|
|
||
|
|
||
| @app.route('/') | ||
| def main(): | ||
| # When deployed to App Engine, the `GAE_ENV` environment variable will be | ||
| # set to `standard` | ||
| if os.environ.get('GAE_ENV'): | ||
| # If deployed, use the local socket interface for accessing Cloud SQL | ||
| host = '/cloudsql/{}'.format(db_connection_name) | ||
| else: | ||
| # If running locally, use the TCP connections instead | ||
| # Set up Cloud SQL Proxy (cloud.google.com/sql/docs/mysql/sql-proxy) | ||
| # so that your application can use 127.0.0.1:3306 to connect to your | ||
| # Cloud SQL instance | ||
| host = '127.0.0.1' | ||
|
|
||
| cnx = psycopg2.connect(dbname=db_name, user=db_user, | ||
| password=db_password, host=host) | ||
| with cnx.cursor() as cursor: | ||
| cursor.execute('SELECT NOW() as now;') | ||
| result = cursor.fetchall() | ||
| current_time = result[0][0] | ||
| cnx.commit() | ||
| cnx.close() | ||
|
|
||
| return str(current_time) | ||
| # [END gae_python37_cloudsql_psql] | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| app.run(host='127.0.0.1', port=8080, debug=True) |
66 changes: 66 additions & 0 deletions
66
appengine/standard_python37/cloudsql/main_postgres_pooling.py
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 |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # Copyright 2018 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # [START gae_python37_cloudsql_psql_pooling] | ||
| import os | ||
|
|
||
| from flask import Flask | ||
| import psycopg2.pool | ||
|
|
||
| db_user = os.environ.get('CLOUD_SQL_USERNAME') | ||
| db_password = os.environ.get('CLOUD_SQL_PASSWORD') | ||
| db_name = os.environ.get('CLOUD_SQL_DATABASE_NAME') | ||
| db_connection_name = os.environ.get('CLOUD_SQL_CONNECTION_NAME') | ||
|
|
||
| # When deployed to App Engine, the `GAE_ENV` environment variable will be | ||
| # set to `standard` | ||
| if os.environ.get('GAE_ENV'): | ||
| # If deployed, use the local socket interface for accessing Cloud SQL | ||
| host = '/cloudsql/{}'.format(db_connection_name) | ||
| else: | ||
| # If running locally, use the TCP connections instead | ||
| # Set up Cloud SQL Proxy (cloud.google.com/sql/docs/mysql/sql-proxy) | ||
| # so that your application can use 127.0.0.1:3306 to connect to your | ||
| # Cloud SQL instance | ||
| host = '127.0.0.1' | ||
|
|
||
| db_config = { | ||
| 'user': db_user, | ||
| 'password': db_password, | ||
| 'database': db_name, | ||
| 'host': host | ||
| } | ||
|
|
||
| cnxpool = psycopg2.pool.ThreadedConnectionPool(minconn=1, maxconn=3, | ||
| **db_config) | ||
|
|
||
| app = Flask(__name__) | ||
|
|
||
|
|
||
| @app.route('/') | ||
| def main(): | ||
| cnx = cnxpool.getconn() | ||
| with cnx.cursor() as cursor: | ||
| cursor.execute('SELECT NOW() as now;') | ||
| result = cursor.fetchall() | ||
| current_time = result[0][0] | ||
| cnx.commit() | ||
| cnxpool.putconn(cnx) | ||
|
|
||
| return str(current_time) | ||
| # [END gae_python37_cloudsql_psql_pooling] | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| app.run(host='127.0.0.1', port=8080, debug=True) |
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 |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # Copyright 2018 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from unittest.mock import MagicMock | ||
|
|
||
| import psycopg2.pool | ||
| import sqlalchemy | ||
|
|
||
|
|
||
| def test_main(): | ||
| import main_mysql | ||
| main_mysql.pymysql = MagicMock() | ||
| fetchall_mock = main_mysql.pymysql.connect().cursor().__enter__().fetchall | ||
| fetchall_mock.return_value = [['0']] | ||
|
|
||
| main_mysql.app.testing = True | ||
| client = main_mysql.app.test_client() | ||
|
|
||
| r = client.get('/') | ||
| assert r.status_code == 200 | ||
| assert '0' in r.data.decode('utf-8') | ||
|
|
||
|
|
||
| def test_main_pooling(): | ||
| sqlalchemy.create_engine = MagicMock() | ||
|
|
||
| import main_mysql_pooling | ||
|
|
||
| cnx_mock = main_mysql_pooling.sqlalchemy.create_engine().connect() | ||
| cnx_mock.execute().fetchall.return_value = [['0']] | ||
|
|
||
| main_mysql_pooling.app.testing = True | ||
| client = main_mysql_pooling.app.test_client() | ||
|
|
||
| r = client.get('/') | ||
| assert r.status_code == 200 | ||
| assert '0' in r.data.decode('utf-8') | ||
|
|
||
|
|
||
| def test_main_postgressql(): | ||
| import main_postgres | ||
| main_postgres.psycopg2.connect = MagicMock() | ||
| mock_cursor = main_postgres.psycopg2.connect().cursor() | ||
| mock_cursor.__enter__().fetchall.return_value = [['0']] | ||
|
|
||
| main_postgres.app.testing = True | ||
| client = main_postgres.app.test_client() | ||
|
|
||
| r = client.get('/') | ||
| assert r.status_code == 200 | ||
| assert '0' in r.data.decode('utf-8') | ||
|
|
||
|
|
||
| def test_main_postgressql_pooling(): | ||
| psycopg2.pool.ThreadedConnectionPool = MagicMock() | ||
|
|
||
| import main_postgres_pooling | ||
|
|
||
| mock_pool = main_postgres_pooling.psycopg2.pool.ThreadedConnectionPool() | ||
| mock_pool.getconn().cursor().__enter__().fetchall.return_value = [['0']] | ||
|
|
||
| main_postgres_pooling.app.testing = True | ||
| client = main_postgres_pooling.app.test_client() | ||
|
|
||
| r = client.get('/') | ||
| assert r.status_code == 200 | ||
| assert '0' in r.data.decode('utf-8') |
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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| psycopg2==2.7.5 | ||
| psycopg2-binary==2.7.5 | ||
| PyMySQL==0.9.2 | ||
| SQLAlchemy==1.2.12 |
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.
Good comment here!
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.
Thanks :)