8000 SQLAlchemy: Adjust DDL compiler improvements to emit warnings · crate/crate-python@47ad022 · GitHub
[go: up one dir, main page]

Skip to content

Commit 47ad022

Browse files
committed
SQLAlchemy: Adjust DDL compiler improvements to emit warnings
1 parent 9eaf38d commit 47ad022

File tree

2 files changed

+64
-30
lines changed

2 files changed

+64
-30
lines changed

src/crate/client/sqlalchemy/compiler.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# software solely pursuant to the terms of the relevant commercial agreement.
2121

2222
import string
23+
import warnings
2324
from collections import defaultdict
2425

2526
import sqlalchemy as sa
@@ -182,12 +183,16 @@ def visit_foreign_key_constraint(self, constraint, **kw):
182183
"""
183184
CrateDB does not support foreign key constraints.
184185
"""
186+
warnings.warn("CrateDB does not support foreign key constraints, "
187+
"they will be omitted when generating DDL statements.")
185188
return None
186189

187190
def visit_unique_constraint(self, constraint, **kw):
188191
"""
189192
CrateDB does not support unique key constraints.
190193
"""
194+
warnings.warn("CrateDB does not support unique constraints, "
195+
"they will be omitted when generating DDL statements.")
191196
return None
192197

193198

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

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# However, if you have executed another commercial license agreement
1919
# with Crate these terms will supersede the license and you may use the
2020
# software solely pursuant to the terms of the relevant commercial agreement.
21+
import warnings
2122
from textwrap import dedent
2223
from unittest import mock, skipIf, TestCase
2324
from unittest.mock import MagicMock, patch
@@ -27,6 +28,9 @@
2728

2829
import sqlalchemy as sa
2930
from sqlalchemy.sql import text, Update
31+
32+
from crate.testing.util import ExtraAssertions
33+
3034
try:
3135
from sqlalchemy.orm import declarative_base
3236
except ImportError:
@@ -288,7 +292,7 @@ def execute_wrapper(self, query, *args, **kwargs):
288292

289293

290294
@patch('crate.client.connection.Cursor', FakeCursor)
291-
class SqlAlchemyDDLCompilerTest(CompilerTestCase):
295+
class SqlAlchemyDDLCompilerTest(CompilerTestCase, ExtraAssertions):
292296
"""
293297
Verify a few scenarios regarding the DDL compiler.
294298
"""
@@ -330,26 +334,39 @@ class ItemStore(Base):
330334
)
331335
root = sa.orm.relationship(RootStore, back_populates="items")
332336

333-
self.metadata.create_all(self.engine, tables=[RootStore.__table__], checkfirst=False)
334-
self.assertEqual(self.executed_statement, dedent("""
335-
CREATE TABLE testdrive.root (
336-
\tid INT NOT NULL,
337-
\tname STRING,
338-
\tPRIMARY KEY (id)
339-
)
340-
341-
""")) # noqa: W291
342-
343-
self.metadata.create_all(self.engine, tables=[ItemStore.__table__], checkfirst=False)
344-
self.assertEqual(self.executed_statement, dedent("""
345-
CREATE TABLE testdrive.item (
346-
\tid INT NOT NULL,
347-
\tname STRING,
348-
\troot_id INT,
349-
\tPRIMARY KEY (id)
350-
)
351-
352-
""")) # noqa: W291, W293
337+
with warnings.catch_warnings(record=True) as w:
338+
339+
# Cause all warnings to always be triggered.
340+
warnings.simplefilter("always")
341+
342+
# Verify SQL DDL statement.
343+
self.metadata.create_all(self.engine, tables=[RootStore.__table__], checkfirst=False)
344+
self.assertEqual(self.executed_statement, dedent("""
345+
CREATE TABLE testdrive.root (
346+
\tid INT NOT NULL,
347+
\tname STRING,
348+
\tPRIMARY KEY (id)
349+
)
350+
351+
""")) # noqa: W291, W293
352+
353+
# Verify SQL DDL statement.
354+
self.metadata.create_all(self.engine, tables=[ItemStore.__table__], checkfirst=False)
355+
self.assertEqual(self.executed_statement, dedent("""
356+
CREATE TABLE testdrive.item (
357+
\tid INT NOT NULL,
358+
\tname STRING,
359+
\troot_id INT,
360+
\tPRIMARY KEY (id)
361+
)
362+
363+
""")) # noqa: W291, W293
364+
365+
# Verify if corresponding warning is emitted.
366+
self.assertEqual(len(w), 1)
367+
self.assertIsSubclass(w[-1].category, UserWarning)
368+
self.assertIn("CrateDB does not support foreign key constraints, "
369+
"they will be omitted when generating DDL statements.", str(w[-1].message))
353370

354371
def test_ddl_with_unique_key(self):
355372
"""
@@ -366,12 +383,24 @@ class FooBar(Base):
366383
id = sa.Column(sa.Integer, primary_key=True)
367384
name = sa.Column(sa.String, unique=True)
368385

369-
self.metadata.create_all(self.engine, tables=[FooBar.__table__], checkfirst=False)
370-
self.assertEqual(self.executed_statement, dedent("""
371-
CREATE TABLE testdrive.foobar (
372-
\tid INT NOT NULL,
373-
\tname STRING,
374-
\tPRIMARY KEY (id)
375-
)
376-
377-
""")) # noqa: W291
386+
with warnings.catch_warnings(record=True) as w:
387+
388+
# Cause all warnings to always be triggered.
389+
warnings.simplefilter("always")
390+
391+
# Verify SQL DDL statement.
392+
self.metadata.create_all(self.engine, tables=[FooBar.__table__], checkfirst=False)
393+
self.assertEqual(self.executed_statement, dedent("""
394+
CREATE TABLE testdrive.foobar (
395+
\tid INT NOT NULL,
396+
\tname STRING,
397+
\tPRIMARY KEY (id)
398+
)
399+
400+
""")) # noqa: W291, W293
401+
402+
# Verify if corresponding warning is emitted.
403+
self.assertEqual(len(w), 1)
404+
self.assertIsSubclass(w[-1].category, UserWarning)
405+
self.assertIn("CrateDB does not support unique constraints, "
406+
"they will be omitted when generating DDL statements.", str(w[-1].message))

0 commit comments

Comments
 (0)
0