Skip to content

Commit

Permalink
Merge pull request #1064 from motherduckdb/guenp/add-no-cache
Browse files Browse the repository at this point in the history
fix: Pass user config option via database name
  • Loading branch information
Mause authored Jul 29, 2024
2 parents f2abccc + 87bd585 commit cbb60fd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions duckdb_engine/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import re
import uuid
import warnings
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -476,6 +478,9 @@ def initialize(self, connection: "Connection") -> None:
def create_connect_args(self, url: URL) -> Tuple[tuple, dict]:
opts = url.translate_connect_args(database="database")
opts["url_config"] = dict(url.query)
user = opts["url_config"].pop("user", None)
if user is not None:
opts["database"] += f"?user={user}"
return (), opts

@classmethod
Expand Down
33 changes: 33 additions & 0 deletions duckdb_engine/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,36 @@ def test_close(engine: Engine) -> None:
with engine.connect() as conn:
res = conn.execute(text("select 1"))
res.close()


def test_with_cache(tmp_path: Path) -> None:
tmp_db_path = str(tmp_path / "db_cached")
engine1 = create_engine(f"duckdb:///{tmp_db_path}?threads=1")
engine2 = create_engine(f"duckdb:///{tmp_db_path}?threads=2")
with engine1.connect() as conn1:
with engine2.connect() as conn2:
res1 = conn1.execute(
text("select value from duckdb_settings() where name = 'threads'")
).fetchall()
res2 = conn2.execute(
text("select value from duckdb_settings() where name = 'threads'")
).fetchall()
assert res1 == res2
assert res1[0][0] == "1"


def test_no_cache(tmp_path: Path) -> None:
tmp_db_path = str(tmp_path / "db_no_cache")
engine1 = create_engine(f"duckdb:///{tmp_db_path}?threads=1&user=1")
engine2 = create_engine(f"duckdb:///{tmp_db_path}?threads=2&user=2")
with engine1.connect() as conn1:
with engine2.connect() as conn2:
res1 = conn1.execute(
text("select value from duckdb_settings() where name = 'threads'")
).fetchall()
res2 = conn2.execute(
text("select value from duckdb_settings() where name = 'threads'")
).fetchall()
assert res1 != res2
assert res1[0][0] == "1"
assert res2[0][0] == "2"

0 comments on commit cbb60fd

Please sign in to comment.