Skip to content

Commit 34c1b56

Browse files
committed
Improve SQLAlchemy's CrateDialect.get_pk_constraint for CrateDB>=5.1.0
Since CrateDB 3.0.0 already, it would have been correct to use the `information_schema.key_column_usage.table_schema` column instead of `table_catalog` (mostly contains value `doc`, the default schema of CrateDB). Now, with CrateDB >=5.1.0, `table_catalog` will always return `crate`, thus the need for a fix.
1 parent db1ec04 commit 34c1b56

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

CHANGES.txt

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Changes for crate
55
Unreleased
66
==========
77

8+
- Improved SQLAlchemy's ``CrateDialect.get_pk_constraint`` to be compatible
9+
with breaking changes in CrateDB >=5.1.0.
810

911
2022/07/04 0.27.1
1012
=================

src/crate/client/sqlalchemy/dialect.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,24 @@ def get_columns(self, connection, table_name, schema=None, **kw):
264264

265265
@reflection.cache
266266
def get_pk_constraint(self, engine, table_name, schema=None, **kw):
267-
if self.server_version_info >= (2, 3, 0):
267+
if self.server_version_info >= (3, 0, 0):
268+
query = """SELECT column_name
269+
FROM information_schema.key_column_usage
270+
WHERE table_name = ? AND table_schema = ?"""
271+
272+
def result_fun(result):
273+
rows = result.fetchall()
274+
return set(map(lambda el: el[0], rows))
275+
276+
elif self.server_version_info >= (2, 3, 0):
268277
query = """SELECT column_name
269278
FROM information_schema.key_column_usage
270279
WHERE table_name = ? AND table_catalog = ?"""
271280

272281
def result_fun(result):
273282
rows = result.fetchall()
274283
return set(map(lambda el: el[0], rows))
284+
275285
else:
276286
query = """SELECT constraint_name
277287
FROM information_schema.table_constraints

src/crate/client/sqlalchemy/tests/dialect_test.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Character(self.base):
6767
self.character = Character
6868
self.session = Session()
6969

70-
def test_primary_keys(self):
70+
def test_primary_keys_2_3_0(self):
7171
meta = self.character.metadata
7272
insp = inspect(meta.bind)
7373
self.engine.dialect.server_version_info = (2, 3, 0)
@@ -81,6 +81,23 @@ def test_primary_keys(self):
8181
eq_(insp.get_pk_constraint("characters")['constrained_columns'], {"id", "id2", "id3"})
8282
self.fake_cursor.fetchall.assert_called_once_with()
8383
in_("information_schema.key_column_usage", self.executed_statement)
84+
in_("table_catalog = ?", self.executed_statement)
85+
86+
def test_primary_keys_3_0_0(self):
87+
meta = self.character.metadata
88+
insp = inspect(meta.bind)
89+
self.engine.dialect.server_version_info = (3, 0, 0)
90+
91+
self.fake_cursor.rowcount = 3
92+
self.fake_cursor.description = (
93+
('foo', None, None, None, None, None, None),
94+
)
95+
self.fake_cursor.fetchall = MagicMock(return_value=[["id"], ["id2"], ["id3"]])
96+
97+
eq_(insp.get_pk_constraint("characters")['constrained_columns'], {"id", "id2", "id3"})
98+
self.fake_cursor.fetchall.assert_called_once_with()
99+
in_("information_schema.key_column_usage", self.executed_statement)
100+
in_("table_schema = ?", self.executed_statement)
84101

85102
def test_get_table_names(self):
86103
self.fake_cursor.rowcount = 1

0 commit comments

Comments
 (0)