Skip to content

Commit

Permalink
Merge pull request #881 from Mause/comments
Browse files Browse the repository at this point in the history
feat: comment support
  • Loading branch information
Mause authored Feb 4, 2024
2 parents 991ca6e + 6a00ec0 commit 5c7ca9b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
3 changes: 2 additions & 1 deletion duckdb_engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.selectable import Select

from ._supports import has_comment_support
from .config import apply_config, get_core_config
from .datatypes import ISCHEMA_NAMES, register_extension_types

Expand Down Expand Up @@ -218,7 +219,7 @@ class Dialect(PGDialect_psycopg2):
driver = "duckdb_engine"
_has_events = False
supports_statement_cache = False
supports_comments = False
supports_comments = has_comment_support()
supports_sane_rowcount = False
supports_server_side_cursors = False
inspector = DuckDBInspector
Expand Down
14 changes: 14 additions & 0 deletions duckdb_engine/_supports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import duckdb


def has_comment_support() -> bool:
"""
See https://github.com/duckdb/duckdb/pull/10372
"""
try:
with duckdb.connect(":memory:") as con:
con.execute("CREATE TABLE t (i INTEGER);")
con.execute("COMMENT ON TABLE t IS 'test';")
except duckdb.ParserException:
return False
return True
36 changes: 32 additions & 4 deletions duckdb_engine/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session, relationship, sessionmaker

from .. import DBAPI, Dialect, supports_attach
from .. import Dialect, supports_attach
from .._supports import has_comment_support

try:
# sqlalchemy 2
Expand Down Expand Up @@ -394,10 +395,37 @@ def test_binary(session: Session) -> None:
assert b.text == "Hello World!"


def test_comment_support() -> None:
@mark.skipif(
not has_comment_support(), reason="comments not supported by duckdb version"
)
def test_comment_support(engine: Engine) -> None:
"comments not yet supported by duckdb"
with raises(DBAPI.ParserException, match="syntax error"):
duckdb.default_connection.execute('comment on sqlite_master is "hello world";')

class Notice(Base):
__tablename__ = "tb_notice"
__table_args__ = {"comment": "Notice table"}

seqno = Column(
Integer,
Sequence("seqno_sequence"),
primary_key=True,
comment="Integer representing the sequence number",
)
title = Column(
String(200),
nullable=False,
comment="Title of the notice, represented as a string",
)

Base.metadata.create_all(bind=engine)

inspector = inspect(engine)

assert inspector.get_table_comment("tb_notice")["text"] == "Notice table"

columns = inspector.get_columns("tb_notice", None)
assert columns[0]["comment"] == "Integer representing the sequence number"
assert columns[1]["comment"] == "Title of the notice, represented as a string"


def test_rowcount() -> None:
Expand Down

0 comments on commit 5c7ca9b

Please sign in to comment.