- Notifications
You must be signed in to change notification settings - Fork85
Add docs for connecting to MySQL with Django.#806
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View thisfailed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
jackwotherspoon commentedJul 31, 2023
Will test out this sample sometime this week and merge it if all goes smoothly 😄 |
| classDatabaseWrapper(base.DatabaseWrapper): | ||
| defget_new_connection(self,conn_params): | ||
| return Connector().connect(**conn_params) |
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.
Is this called on every new database connection? If so I'm not sure this is ideal as the first call toconnect() after a newConnector() is initialized it will cache the Cloud SQL instance data such as instance IP and certs used for SSL connection. Initializing a newConnector() on every database connection may cause SQL Admin API quotas to be exhausted and will not take advantage of the caching I mentioned. I will have to think about this a bit
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.
After looking at this a bit closer I think we would want theConnector() to be initialized outside theDatabaseWrapper class so that it is only called once and can properly cache instances and re-use them.
Something that looks like this...
fromdjango.db.backends.mysqlimportbasefromgoogle.cloud.sql.connectorimportConnectorconnector=Connector()classDatabaseWrapper(base.DatabaseWrapper):defget_new_connection(self,conn_params):returnconnector.connect(**conn_params)
Refs#437
Note, I realised while writing this that it won't work with Postgres as there's no equivalent of the
install_as_mysqldbcall; I included a note explaining this.