From 2f08b2a2d2b9de5869f4b3c1c23aa22a0e771656 Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Fri, 22 Dec 2023 01:39:58 +0100 Subject: [PATCH] Fix `get_table_names()` reflection method It did not respect the `schema` query argument in SQLAlchemy connection URLs. --- CHANGES.md | 3 +++ src/sqlalchemy_cratedb/dialect.py | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 65ac05b6..b92ae7e7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,9 @@ - Added support for CrateDB's [FLOAT_VECTOR] data type. For SQLAlchemy column definitions, you can use it like `FloatVector(dimensions=1024)`. +- Fixed `get_table_names()` reflection method, it did not respect the + `schema` query argument in SQLAlchemy connection URLs. + [FLOAT_VECTOR]: https://crate.io/docs/crate/reference/en/master/general/ddl/data-types.html#float-vector diff --git a/src/sqlalchemy_cratedb/dialect.py b/src/sqlalchemy_cratedb/dialect.py index 73f6c539..425da36f 100644 --- a/src/sqlalchemy_cratedb/dialect.py +++ b/src/sqlalchemy_cratedb/dialect.py @@ -229,6 +229,15 @@ def connect(self, host=None, port=None, *args, **kwargs): def _get_default_schema_name(self, connection): return 'doc' + def _get_effective_schema_name(self, connection): + schema_name_raw = connection.engine.url.query.get("schema") + schema_name = None + if isinstance(schema_name_raw, str): + schema_name = schema_name_raw + elif isinstance(schema_name_raw, tuple): + schema_name = schema_name_raw[0] + return schema_name + def _get_server_version_info(self, connection): return tuple(connection.connection.lowest_server_version.version) @@ -258,6 +267,8 @@ def get_schema_names(self, connection, **kw): @reflection.cache def get_table_names(self, connection, schema=None, **kw): + if schema is None: + schema = self._get_effective_schema_name(connection) cursor = connection.exec_driver_sql( "SELECT table_name FROM information_schema.tables " "WHERE {0} = ? "