Skip to content

Commit

Permalink
Merge pull request #333 from motherduckdb/guen/eco-49-add-a-custom_us…
Browse files Browse the repository at this point in the history
…er_agent-to-dbt-duckdb-integration

Add custom_user_agent to connection config when connecting to MotherDuck
  • Loading branch information
jwills authored Feb 9, 2024
2 parents 7a04c30 + c84f884 commit 7722b45
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dbt/adapters/duckdb/environments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from ..utils import TargetConfig
from dbt.contracts.connection import AdapterResponse
from dbt.exceptions import DbtRuntimeError
from dbt.version import __version__


def _ensure_event_loop():
Expand Down Expand Up @@ -114,6 +115,12 @@ def initialize_db(
cls, creds: DuckDBCredentials, plugins: Optional[Dict[str, BasePlugin]] = None
):
config = creds.config_options or {}
if creds.is_motherduck:
user_agent = f"dbt/{__version__}"
if "custom_user_agent" in config:
user_agent = f"{user_agent} {config['custom_user_agent']}"

config["custom_user_agent"] = user_agent

if creds.retries:
success, attempt, exc = False, 0, None
Expand Down
18 changes: 18 additions & 0 deletions tests/functional/plugins/test_motherduck.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import pytest
from unittest import mock
from dbt.tests.util import (
run_dbt,
)
from dbt.adapters.duckdb.environments import Environment
from dbt.adapters.duckdb.credentials import DuckDBCredentials
from dbt.version import __version__

random_logs_sql = """
{{ config(materialized='table', meta=dict(temp_schema_name='dbt_temp_test')) }}
Expand Down Expand Up @@ -100,3 +104,17 @@ def test_incremental(self, project):

res = project.run_sql("SELECT schema_name FROM information_schema.schemata WHERE catalog_name = 'test'", fetch="all")
assert "dbt_temp_test" in [_r for (_r,) in res]

def test_motherduck_user_agent(dbt_profile_target):
test_path = dbt_profile_target["path"]
creds = DuckDBCredentials(path=test_path)
with mock.patch("dbt.adapters.duckdb.environments.duckdb.connect") as mock_connect:
Environment.initialize_db(creds)
if creds.is_motherduck:
kwargs = {
'read_only': False,
'config': {'custom_user_agent': f'dbt/{__version__}'}
}
mock_connect.assert_called_with(test_path, **kwargs)
else:
mock_connect.assert_called_with(test_path, read_only=False, config = {})

0 comments on commit 7722b45

Please sign in to comment.