Skip to content

Commit 32c571e

Browse files
authored
Move cloud_sql_binary_path from connection to Hook (#29499)
Specifying cloud_sql_binary_path in connection should never be needed, This is at most property of the Hook (and Operator by transition) if you want to override it, rather than extra in the connection.
1 parent 8e24387 commit 32c571e

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

β€Žairflow/providers/google/cloud/example_dags/example_cloud_sql_query.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ def get_absolute_path(path):
205205
"location={location}&"
206206
"instance={instance}&"
207207
"use_proxy=True&"
208-
"sql_proxy_binary_path={sql_proxy_binary_path}&"
209-
"sql_proxy_use_tcp=False".format(sql_proxy_binary_path=quote_plus(sql_proxy_binary_path), **mysql_kwargs)
208+
"sql_proxy_use_tcp=False".format(**mysql_kwargs)
210209
)
211210

212211
# MySQL: connect directly via TCP (non-SSL)
@@ -279,7 +278,10 @@ def get_absolute_path(path):
279278

280279
for connection_name in connection_names:
281280
task = CloudSQLExecuteQueryOperator(
282-
gcp_cloudsql_conn_id=connection_name, task_id="example_gcp_sql_task_" + connection_name, sql=SQL
281+
gcp_cloudsql_conn_id=connection_name,
282+
task_id="example_gcp_sql_task_" + connection_name,
283+
sql=SQL,
284+
sql_proxy_binary_path=sql_proxy_binary_path,
283285
)
284286
tasks.append(task)
285287
if prev_task:

β€Žairflow/providers/google/cloud/hooks/cloud_sql.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,8 @@ class CloudSQLDatabaseHook(BaseHook):
668668
* **public_ip** - IP to connect to for public connection (from host of the URI).
669669
* **public_port** - Port to connect to for public connection (from port of the URI).
670670
* **database** - Database to connect to (from schema of the URI).
671+
* **sql_proxy_binary_path** - Optional path to Cloud SQL Proxy binary. If the binary
672+
is not specified or the binary is not present, it is automatically downloaded.
671673
672674
Remaining parameters are retrieved from the extras (URI query parameters):
673675
@@ -682,8 +684,6 @@ class CloudSQLDatabaseHook(BaseHook):
682684
You cannot use proxy and SSL together.
683685
* **sql_proxy_use_tcp** - (default False) If set to true, TCP is used to connect via
684686
proxy, otherwise UNIX sockets are used.
685-
* **sql_proxy_binary_path** - Optional path to Cloud SQL Proxy binary. If the binary
686-
is not specified or the binary is not present, it is automatically downloaded.
687687
* **sql_proxy_version** - Specific version of the proxy to download (for example
688688
v1.13). If not specified, the latest version is downloaded.
689689
* **sslcert** - Path to client certificate to authenticate when SSL is used.
@@ -707,6 +707,7 @@ def __init__(
707707
gcp_cloudsql_conn_id: str = "google_cloud_sql_default",
708708
gcp_conn_id: str = "google_cloud_default",
709709
default_gcp_project_id: str | None = None,
710+
sql_proxy_binary_path: str | None = None,
710711
) -> None:
711712
super().__init__()
712713
self.gcp_conn_id = gcp_conn_id
@@ -722,7 +723,7 @@ def __init__(
722723
self.use_ssl = self._get_bool(self.extras.get("use_ssl", "False"))
723724
self.sql_proxy_use_tcp = self._get_bool(self.extras.get("sql_proxy_use_tcp", "False"))
724725
self.sql_proxy_version = self.extras.get("sql_proxy_version")
725-
self.sql_proxy_binary_path = self.extras.get("sql_proxy_binary_path")
726+
self.sql_proxy_binary_path = sql_proxy_binary_path
726727
self.user = self.cloudsql_connection.login
727728
self.password = self.cloudsql_connection.password
728729
self.public_ip = self.cloudsql_connection.host

β€Žairflow/providers/google/cloud/operators/cloud_sql.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,8 @@ class CloudSQLExecuteQueryOperator(BaseOperator):
10451045
its schema should be gcpcloudsql://.
10461046
See :class:`~airflow.providers.google.cloud.hooks.cloud_sql.CloudSQLDatabaseHook` for
10471047
details on how to define ``gcpcloudsql://`` connection.
1048+
:param sql_proxy_binary_path: (optional) Path to the cloud-sql-proxy binary.
1049+
is not specified or the binary is not present, it is automatically downloaded.
10481050
"""
10491051

10501052
# [START gcp_sql_query_template_fields]
@@ -1062,6 +1064,7 @@ def __init__(
10621064
parameters: Iterable | Mapping | None = None,
10631065
gcp_conn_id: str = "google_cloud_default",
10641066
gcp_cloudsql_conn_id: str = "google_cloud_sql_default",
1067+
sql_proxy_binary_path: str | None = None,
10651068
**kwargs,
10661069
) -> None:
10671070
super().__init__(**kwargs)
@@ -1071,6 +1074,7 @@ def __init__(
10711074
self.autocommit = autocommit
10721075
self.parameters = parameters
10731076
self.gcp_connection: Connection | None = None
1077+
self.sql_proxy_binary_path = sql_proxy_binary_path
10741078

10751079
def _execute_query(self, hook: CloudSQLDatabaseHook, database_hook: PostgresHook | MySqlHook) -> None:
10761080
cloud_sql_proxy_runner = None
@@ -1094,6 +1098,7 @@ def execute(self, context: Context):
10941098
gcp_cloudsql_conn_id=self.gcp_cloudsql_conn_id,
10951099
gcp_conn_id=self.gcp_conn_id,
10961100
default_gcp_project_id=get_field(self.gcp_connection.extra_dejson, "project"),
1101+
sql_proxy_binary_path=self.sql_proxy_binary_path,
10971102
)
10981103
hook.validate_ssl_certs()
10991104
connection = hook.create_connection()

0 commit comments

Comments
 (0)