Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

danielroseman
Copy link
Contributor

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.

@danielroseman danielroseman requested a review from a team as a code owner July 29, 2023 13:14
@google-cla
Copy link

google-cla bot commented Jul 29, 2023

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.

@jackwotherspoon
Copy link
Collaborator

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)
Copy link
Collaborator

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

Copy link
Collaborator

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants