Skip to content
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

Add custom_user_agent to connection config when connecting to MotherDuck #333

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = {})
Loading