-
Notifications
You must be signed in to change notification settings - Fork 67
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
Add docs for connecting to MySQL with Django. #806
base: main
Are you sure you want to change the base?
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 this failed 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. |
e205bc8
to
9095437
Compare
Will test out this sample sometime this week and merge it if all goes smoothly 😄 |
|
||
class DatabaseWrapper(base.DatabaseWrapper): | ||
def get_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 to connect()
after a new Connector()
is initialized it will cache the Cloud SQL instance data such as instance IP and certs used for SSL connection. Initializing a new Connector()
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 the Connector()
to be initialized outside the DatabaseWrapper
class so that it is only called once and can properly cache instances and re-use them.
Something that looks like this...
from django.db.backends.mysql import base
from google.cloud.sql.connector import Connector
connector = Connector()
class DatabaseWrapper(base.DatabaseWrapper):
def get_new_connection(self, conn_params):
return connector.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_mysqldb
call; I included a note explaining this.