Skip to content

Commit

Permalink
fix: add documentation and test for duckdb config
Browse files Browse the repository at this point in the history
  • Loading branch information
Mause committed Aug 6, 2022
1 parent 245744e commit f3e577a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ frank = session.query(FakeModel).one()
assert frank.name == "Frank"
```

## Configuration

You can configure DuckDB by passing `connect_args` to the create_engine function
```python
create_engine(
'duckdb:///:memory:',
connect_args={
'read_only': True,
'config': {
'memory_limit': '500mb'
}
}
)
```

The supported configuration parameters are listed in the [DuckDB docs](https://duckdb.org/docs/sql/configuration)

## How to register a pandas DataFrame

```python
Expand Down
6 changes: 4 additions & 2 deletions duckdb_engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
kwargs["use_native_hstore"] = False
super().__init__(*args, **kwargs)

def connect(self, *args: Any, **kwargs: Any) -> ConnectionWrapper:
return ConnectionWrapper(duckdb.connect(*args, **kwargs))
def connect(
self, database: str, read_only: bool = False, config: Dict = None
) -> ConnectionWrapper:
return ConnectionWrapper(duckdb.connect(database, read_only, config or {}))

def on_connect(self) -> None:
pass
Expand Down
22 changes: 20 additions & 2 deletions duckdb_engine/tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import zlib
from datetime import timedelta
from pathlib import Path
from typing import Any, Optional

import duckdb
from hypothesis import assume, given, settings
from hypothesis.strategies import text
from pytest import fixture, importorskip, mark, raises
Expand All @@ -22,6 +24,7 @@
from sqlalchemy.dialects import registry
from sqlalchemy.dialects.postgresql.base import PGInspector
from sqlalchemy.engine import Engine
from sqlalchemy.exc import DBAPIError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import RelationshipProperty, Session, relationship, sessionmaker

Expand Down Expand Up @@ -240,8 +243,6 @@ def test_binary(session: Session) -> None:

def test_comment_support() -> None:
"comments not yet supported by duckdb"
import duckdb

exc = getattr(duckdb, "StandardException", DBAPI.Error)

with raises(exc, match="syntax error"):
Expand Down Expand Up @@ -276,3 +277,20 @@ def test_inmemory() -> None:
res = shell.run_cell("""eng.execute("SHOW TABLES").fetchall()""")

assert res.result == [("t",)]


def test_config(tmp_path: Path) -> None:
db_path = tmp_path / "test.db"

with duckdb.connect(str(db_path)) as db:
db.execute("create table hello1 (i int)")

eng = create_engine(
f"duckdb:///{db_path}",
connect_args={"read_only": True, "config": {"memory_limit": "500mb"}},
)

with raises(
DBAPIError, match='Cannot execute statement of type "CREATE" in read-only mode!'
):
eng.execute("create table hello2 (i int)")

0 comments on commit f3e577a

Please sign in to comment.