Repository: iterative/dvc
Pull request #10151 (2023-12-09 05:41:18)

database: rename Adapter to Client


Patch:
diff --git a/dvc/commands/imp_db.py b/dvc/commands/imp_db.py
index 19e915be0..b87b7bd96 100644
--- a/dvc/commands/imp_db.py
+++ b/dvc/commands/imp_db.py
@@ -13,7 +13,7 @@ logger = logger.getChild(__name__)
 
 class CmdTestDb(CmdBase):
     def run(self):
-        from dvc.database import get_adapter
+        from dvc.database import get_client
         from dvc.database.dbt_utils import DBT_PROJECT_FILE, is_dbt_project
         from dvc.dependency.db import _get_dbt_config
         from dvc.exceptions import DvcException
@@ -47,7 +47,7 @@ class CmdTestDb(CmdBase):
                     "provide arguments or set a configuration"
                 )
 
-        adapter = get_adapter(conn_config, project_dir=project_dir, **dbt_config)
+        adapter = get_client(conn_config, project_dir=project_dir, **dbt_config)
         with adapter as db:
             ui.write(f"Testing with {db}", styled=True)
 
diff --git a/dvc/database/__init__.py b/dvc/database/__init__.py
index 4d1ad9f6a..19ca5749f 100644
--- a/dvc/database/__init__.py
+++ b/dvc/database/__init__.py
@@ -4,19 +4,19 @@ from . import dbt_query, sqla
 from .dbt_models import get_model
 from .serializer import export
 
-Adapter = Union[sqla.SQLAlchemyAdapter, dbt_query.DbtAdapter]
+Client = Union[sqla.SQLAlchemyClient, dbt_query.DbtClient]
 
 
-def get_adapter(
+def get_client(
     config,
     project_dir: Optional[str] = None,
     profile: Optional[str] = None,
     target: Optional[str] = None,
     **kwargs: Any,
-) -> "ContextManager[Adapter]":
+) -> "ContextManager[Client]":
     if config:
-        return sqla.adapter(config, **kwargs)
-    return dbt_query.adapter(project_dir=project_dir, profile=profile, target=target)
+        return sqla.client(config, **kwargs)
+    return dbt_query.client(project_dir=project_dir, profile=profile, target=target)
 
 
-__all__ = ["export", "get_adapter", "get_model", "Adapter"]
+__all__ = ["export", "get_client", "get_model", "Client"]
diff --git a/dvc/database/dbt_query.py b/dvc/database/dbt_query.py
index 19c8cf1ae..c37cf6014 100644
--- a/dvc/database/dbt_query.py
+++ b/dvc/database/dbt_query.py
@@ -17,7 +17,7 @@ logger = logger.getChild(__name__)
 
 
 @define
-class DbtAdapter:
+class DbtClient:
     adapter: "BaseAdapter" = field(repr=lambda o: type(o).__qualname__)
     creds: Dict[str, Any] = field(repr=False)
 
@@ -59,12 +59,12 @@ class DbtAdapter:
 
 @contextmanager
 @check_dbt("query")
-def adapter(
+def client(
     project_dir: Optional[str] = None,
     profiles_dir: Optional[str] = None,
     profile: Optional[str] = None,
     target: Optional[str] = None,
-) -> Iterator["DbtAdapter"]:
+) -> Iterator["DbtClient"]:
     from dbt.adapters import factory as adapters_factory
     from dbt.adapters.base.impl import BaseAdapter
 
@@ -85,4 +85,4 @@ def adapter(
         except:  # noqa: E722
             creds = {}
 
-        yield DbtAdapter(adapter, creds)
+        yield DbtClient(adapter, creds)
diff --git a/dvc/database/sqla.py b/dvc/database/sqla.py
index 9d61515e3..1487257aa 100644
--- a/dvc/database/sqla.py
+++ b/dvc/database/sqla.py
@@ -26,7 +26,7 @@ def url_from_config(config: Union[str, URL, Dict[str, str]]) -> URL:
 
 
 @dataclass
-class SQLAlchemyAdapter:
+class SQLAlchemyClient:
     engine: Engine
 
     @contextmanager
@@ -61,13 +61,13 @@ def handle_error(url: URL):
 
 
 @contextmanager
-def adapter(
+def client(
     url_or_config: Union[str, URL, Dict[str, str]], **engine_kwargs: Any
-) -> Iterator[SQLAlchemyAdapter]:
+) -> Iterator[SQLAlchemyClient]:
     url = url_from_config(url_or_config)
     with handle_error(url):
         engine = create_engine(url, **engine_kwargs)
     try:
-        yield SQLAlchemyAdapter(engine)
+        yield SQLAlchemyClient(engine)
     finally:
         engine.dispose()
diff --git a/dvc/dependency/db.py b/dvc/dependency/db.py
index 49d793278..2df04d8d3 100644
--- a/dvc/dependency/db.py
+++ b/dvc/dependency/db.py
@@ -122,7 +122,7 @@ class DbDependency(AbstractDependency):
         file_format: Optional[str] = None,
         **kwargs: Any,
     ) -> None:
-        from dvc.database import export, get_adapter
+        from dvc.database import export, get_client
 
         db_info = self.info.get(PARAM_DB, {})
         query = db_info.get(self.PARAM_QUERY)
@@ -140,7 +140,7 @@ class DbDependency(AbstractDependency):
             raise DvcException(f"connection {connection} not found in config")
 
         project_dir = self.repo.root_dir
-        with get_adapter(
+        with get_client(
             config, project_dir=project_dir, profile=profile, target=target
         ) as db:
             logger.debug("using adapter: %s", db)


Test patch:
diff --git a/tests/func/test_import_db.py b/tests/func/test_import_db.py
index e0d390be8..d4f82ec68 100644
--- a/tests/func/test_import_db.py
+++ b/tests/func/test_import_db.py
@@ -11,15 +11,15 @@ serializers = AgateSerializer(data1), AgateSerializer(data2)
 
 
 @pytest.fixture
-def adapter(mocker):
-    m = mocker.patch("dvc.database.get_adapter")
-    adapter = mocker.MagicMock()
-    adapter.query.return_value.__enter__.side_effect = serializers
-    m.return_value.__enter__.return_value = adapter
+def client(mocker):
+    m = mocker.patch("dvc.database.get_client")
+    client = mocker.MagicMock()
+    client.query.return_value.__enter__.side_effect = serializers
+    m.return_value.__enter__.return_value = client
     return m
 
 
-def test_sql(adapter, tmp_dir, dvc):
+def test_sql(client, tmp_dir, dvc):
     stage = dvc.imp_db(
         sql="select * from model", profile="profile", output_format="json"
     )
@@ -56,7 +56,7 @@ def test_sql(adapter, tmp_dir, dvc):
     }
 
 
-def test_sql_conn_string(adapter, tmp_dir, dvc):
+def test_sql_conn_string(client, tmp_dir, dvc):
     with dvc.config.edit(level="local") as conf:
         conf["db"] = {"conn": {"url": "conn"}}