Skip to content

Commit 50b81fb

Browse files
feat!: remove deprecated global 'connector.connect' function (GoogleCloudPlatform#394)
1 parent 40425f9 commit 50b81fb

File tree

3 files changed

+2
-76
lines changed

3 files changed

+2
-76
lines changed

google/cloud/sql/connector/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16-
from .connector import connect, Connector
16+
from .connector import Connector
1717
from .instance import IPTypes
1818

1919

20-
__ALL__ = [connect, Connector, IPTypes]
20+
__ALL__ = [Connector, IPTypes]
2121

2222
try:
2323
import pkg_resources

google/cloud/sql/connector/connector.py

-40
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232

3333
logger = logging.getLogger(name=__name__)
3434

35-
_default_connector = None
36-
3735

3836
class Connector:
3937
"""A class to configure and create connections to Cloud SQL instances.
@@ -236,41 +234,3 @@ async def _close(self) -> None:
236234
await asyncio.gather(
237235
*[instance.close() for instance in self._instances.values()]
238236
)
239-
240-
241-
def connect(instance_connection_string: str, driver: str, **kwargs: Any) -> Any:
242-
"""Uses a Connector object with default settings and returns a database
243-
connection object with a background thread to refresh the certificates and metadata.
244-
For more advanced configurations, callers should instantiate Connector on their own.
245-
246-
:type instance_connection_string: str
247-
:param instance_connection_string:
248-
A string containing the GCP project name, region name, and instance
249-
name separated by colons.
250-
251-
Example: example-proj:example-region-us6:example-instance
252-
253-
:type driver: str
254-
:param: driver:
255-
A string representing the driver to connect with. Supported drivers are
256-
pymysql, pg8000, and pytds.
257-
258-
:param kwargs:
259-
Pass in any driver-specific arguments needed to connect to the Cloud
260-
SQL instance.
261-
262-
:rtype: Connection
263-
:returns:
264-
A DB-API connection to the specified Cloud SQL instance.
265-
"""
266-
# deprecation warning
267-
logger.warning(
268-
"The global `connect` method is deprecated and may be removed in a later "
269-
"version. Please initialize a `Connector` object and call it's `connect` "
270-
"method directly. \n"
271-
"See https://github.com/GoogleCloudPlatform/cloud-sql-python-connector/blob/main/README.md#how-to-use-this-connector for examples.",
272-
)
273-
global _default_connector
274-
if _default_connector is None:
275-
_default_connector = Connector()
276-
return _default_connector.connect(instance_connection_string, driver, **kwargs)

tests/unit/test_connector.py

-34
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import asyncio
1919

2020
from google.cloud.sql.connector import Connector, IPTypes
21-
from google.cloud.sql.connector import connector as global_connector
22-
from google.cloud.sql.connector.connector import _default_connector
2321

2422
from mock import patch
2523
from typing import Any
@@ -100,35 +98,3 @@ def test_Connector_connect(connector: Connector) -> None:
10098
)
10199
# verify connector made connection call
102100
assert connection is True
103-
104-
105-
def test_global_connect(connector: Connector) -> None:
106-
"""Test that global connect properly make connection call to default connector."""
107-
connect_string = "my-project:my-region:my-instance"
108-
# verify default_connector is not set
109-
assert _default_connector is None
110-
# set global connector
111-
global_connector._default_connector = connector
112-
# patch db connection creation
113-
with patch("pg8000.dbapi.connect") as mock_connect:
114-
mock_connect.return_value = True
115-
# connect using global connector
116-
connection = global_connector.connect(
117-
connect_string, "pg8000", user="my-user", password="my-pass", db="my-db"
118-
)
119-
120-
# verify default_connector is now set
121-
from google.cloud.sql.connector.connector import (
122-
_default_connector as default_connector,
123-
)
124-
125-
assert isinstance(default_connector, Connector)
126-
127-
# verify attributes of default connector
128-
assert default_connector._ip_type == IPTypes.PUBLIC
129-
assert default_connector._enable_iam_auth is False
130-
assert default_connector._timeout == 30
131-
assert default_connector._credentials is None
132-
133-
# verify global connector made connection call
134-
assert connection is True

0 commit comments

Comments
 (0)