-
Notifications
You must be signed in to change notification settings - Fork 85
feat: validate driver with database engine #1108
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,8 +30,10 @@ | |
|
|
||
| import google.cloud.sql.connector.asyncpg as asyncpg | ||
| from google.cloud.sql.connector.client import CloudSQLClient | ||
| from google.cloud.sql.connector.enums import DriverMapping | ||
| from google.cloud.sql.connector.exceptions import ConnectorLoopError | ||
| from google.cloud.sql.connector.exceptions import DnsNameResolutionError | ||
| from google.cloud.sql.connector.exceptions import IncompatibleDriverError | ||
| from google.cloud.sql.connector.instance import IPTypes | ||
| from google.cloud.sql.connector.instance import RefreshAheadCache | ||
| from google.cloud.sql.connector.instance import RefreshStrategy | ||
|
|
@@ -321,6 +323,14 @@ async def connect_async( | |
| # attempt to make connection to Cloud SQL instance | ||
| try: | ||
| conn_info = await cache.connect_info() | ||
| # validate driver matches intended database engine | ||
| mapping = DriverMapping[driver.upper()] | ||
| if not conn_info.database_version.startswith(mapping.value): | ||
| raise IncompatibleDriverError( | ||
| f"Database driver '{driver}' is incompatible with database " | ||
|
||
| f"version '{conn_info.database_version}'. Given driver can " | ||
| f"only be used with Cloud SQL {mapping.value} databases." | ||
| ) | ||
| ip_address = conn_info.get_preferred_ip(ip_type) | ||
| # resolve DNS name into IP address for PSC | ||
| if ip_type.value == "PSC": | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Copyright 2024 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 enum import Enum | ||
|
|
||
|
|
||
| class DriverMapping(Enum): | ||
| """Maps a given database driver to it's corresponding database engine.""" | ||
|
|
||
| ASYNCPG = "POSTGRES" | ||
| PG8000 = "POSTGRES" | ||
| PYMYSQL = "MYSQL" | ||
| PYTDS = "SQLSERVER" | ||
|
Comment on lines
+20
to
+26
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am going to have a follow-up refactor PR that moves the other shared enum types ( |
||
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.
What if we encapsulated more details and did something like this?
where the DriverMapping method looks like:
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.
this would mean i no longer have access to the proper compatible database engine
mapping.valuein the error message without doing a separate call toDriverMapping[driver.upper()].valuewhich is more work...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.
Just an idea -- feel free to handle it however you like. A good error message is most important.