Skip to content

Commit

Permalink
fix(reflection bug): Fixed bug causing TypeError when using metadata.…
Browse files Browse the repository at this point in the history
…reflect()
  • Loading branch information
Aarya2004 committed Oct 28, 2024
1 parent 4190fbb commit b96c1d2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
29 changes: 28 additions & 1 deletion duckdb_engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

import duckdb
import sqlalchemy
from sqlalchemy import pool, text, util
from sqlalchemy import pool, text, util, select, sql
from sqlalchemy import types as sqltypes
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.dialects.postgresql.base import (
PGDialect,
PGIdentifierPreparer,
PGInspector,
PGTypeCompiler,
pg_catalog,
)
from sqlalchemy.dialects.postgresql.psycopg2 import PGDialect_psycopg2
from sqlalchemy.engine.default import DefaultDialect
Expand All @@ -34,6 +35,7 @@
from sqlalchemy.exc import NoSuchTableError
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.selectable import Select
from sqlalchemy.sql import bindparam

from ._supports import has_comment_support
from .config import apply_config, get_core_config
Expand Down Expand Up @@ -601,6 +603,31 @@ def get_multi_columns(

return columns.items()

def _comment_query(self, schema, has_filter_names, scope, kind):
relkinds = self._kind_to_relkinds(kind)
query = (
select(
pg_catalog.pg_class.c.relname,
pg_catalog.pg_description.c.description,
)
.select_from(pg_catalog.pg_class)
.outerjoin(
pg_catalog.pg_description,
sql.and_(
pg_catalog.pg_class.c.oid
== pg_catalog.pg_description.c.objoid,
pg_catalog.pg_description.c.objsubid == 0,
),
)
.where(self._pg_class_relkind_condition(relkinds))
)
query = self._pg_class_filter_scope_schema(query, schema, scope)
if has_filter_names:
query = query.where(
pg_catalog.pg_class.c.relname.in_(bindparam("filter_names"))
)
return query


if sqlalchemy.__version__ >= "2.0.14":
from sqlalchemy import TryCast # type: ignore[attr-defined]
Expand Down
12 changes: 12 additions & 0 deletions duckdb_engine/tests/test_reflection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from sqlalchemy import MetaData, create_engine, text

engine = create_engine("duckdb:///:memory:")
metadata = MetaData()
with engine.connect() as conn:
conn.execute(text("CREATE TABLE tbl(col1 INTEGER)"))
conn.commit()
try:
metadata.reflect(engine)
except Exception as e:
print("Exception: " + str(e))
raise AssertionError

0 comments on commit b96c1d2

Please sign in to comment.