|
1 | 1 | from typing import Any, ClassVar, Dict, Union, Type |
2 | 2 |
|
3 | 3 | import attrs |
| 4 | +from packaging.version import parse as parse_version |
4 | 5 |
|
5 | 6 | from data_diff.utils import match_regexps |
6 | 7 | from data_diff.abcs.database_types import ( |
|
27 | 28 | CHECKSUM_OFFSET, |
28 | 29 | ) |
29 | 30 | from data_diff.databases.base import MD5_HEXDIGITS, CHECKSUM_HEXDIGITS |
| 31 | +from data_diff.version import __version__ |
30 | 32 |
|
31 | 33 |
|
32 | 34 | @import_helper("duckdb") |
@@ -148,9 +150,21 @@ def close(self): |
148 | 150 | def create_connection(self): |
149 | 151 | ddb = import_duckdb() |
150 | 152 | try: |
151 | | - return ddb.connect(self._args["filepath"]) |
| 153 | + # custom_user_agent is only available in duckdb >= 0.9.2 |
| 154 | + if parse_version(ddb.__version__) >= parse_version("0.9.2"): |
| 155 | + custom_user_agent = f"data-diff/v{__version__}" |
| 156 | + config = {"custom_user_agent": custom_user_agent} |
| 157 | + connection = ddb.connect(database=self._args["filepath"], config=config) |
| 158 | + custom_user_agent_results = connection.sql("PRAGMA USER_AGENT;").fetchall() |
| 159 | + custom_user_agent_filtered = custom_user_agent_results[0][0] |
| 160 | + assert custom_user_agent in custom_user_agent_filtered |
| 161 | + else: |
| 162 | + connection = ddb.connect(database=self._args["filepath"]) |
| 163 | + return connection |
152 | 164 | except ddb.OperationalError as e: |
153 | 165 | raise ConnectError(*e.args) from e |
| 166 | + except AssertionError: |
| 167 | + raise ConnectError("Assertion failed: Custom user agent is invalid.") from None |
154 | 168 |
|
155 | 169 | def select_table_schema(self, path: DbPath) -> str: |
156 | 170 | database, schema, table = self._normalize_table_path(path) |
|
0 commit comments