Skip to content

Commit

Permalink
SQLAlchemy: Adjust DDL compiler improvements to emit warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Sep 28, 2023
1 parent 9eaf38d commit 47ad022
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 30 deletions.
5 changes: 5 additions & 0 deletions src/crate/client/sqlalchemy/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# software solely pursuant to the terms of the relevant commercial agreement.

import string
import warnings
from collections import defaultdict

import sqlalchemy as sa
Expand Down Expand Up @@ -182,12 +183,16 @@ def visit_foreign_key_constraint(self, constraint, **kw):
"""
CrateDB does not support foreign key constraints.
"""
warnings.warn("CrateDB does not support foreign key constraints, "
"they will be omitted when generating DDL statements.")
return None

def visit_unique_constraint(self, constraint, **kw):
"""
CrateDB does not support unique key constraints.
"""
warnings.warn("CrateDB does not support unique constraints, "
"they will be omitted when generating DDL statements.")
return None


Expand Down
89 changes: 59 additions & 30 deletions src/crate/client/sqlalchemy/tests/compiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# However, if you have executed another commercial license agreement
# with Crate these terms will supersede the license and you may use the
# software solely pursuant to the terms of the relevant commercial agreement.
import warnings
from textwrap import dedent
from unittest import mock, skipIf, TestCase
from unittest.mock import MagicMock, patch
Expand All @@ -27,6 +28,9 @@

import sqlalchemy as sa
from sqlalchemy.sql import text, Update

from crate.testing.util import ExtraAssertions

try:
from sqlalchemy.orm import declarative_base
except ImportError:
Expand Down Expand Up @@ -288,7 +292,7 @@ def execute_wrapper(self, query, *args, **kwargs):


@patch('crate.client.connection.Cursor', FakeCursor)
class SqlAlchemyDDLCompilerTest(CompilerTestCase):
class SqlAlchemyDDLCompilerTest(CompilerTestCase, ExtraAssertions):
"""
Verify a few scenarios regarding the DDL compiler.
"""
Expand Down Expand Up @@ -330,26 +334,39 @@ class ItemStore(Base):
)
root = sa.orm.relationship(RootStore, back_populates="items")

self.metadata.create_all(self.engine, tables=[RootStore.__table__], checkfirst=False)
self.assertEqual(self.executed_statement, dedent("""
CREATE TABLE testdrive.root (
\tid INT NOT NULL,
\tname STRING,
\tPRIMARY KEY (id)
)
""")) # noqa: W291

self.metadata.create_all(self.engine, tables=[ItemStore.__table__], checkfirst=False)
self.assertEqual(self.executed_statement, dedent("""
CREATE TABLE testdrive.item (
\tid INT NOT NULL,
\tname STRING,
\troot_id INT,
\tPRIMARY KEY (id)
)
""")) # noqa: W291, W293
with warnings.catch_warnings(record=True) as w:

# Cause all warnings to always be triggered.
warnings.simplefilter("always")

# Verify SQL DDL statement.
self.metadata.create_all(self.engine, tables=[RootStore.__table__], checkfirst=False)
self.assertEqual(self.executed_statement, dedent("""
CREATE TABLE testdrive.root (
\tid INT NOT NULL,
\tname STRING,
\tPRIMARY KEY (id)
)
""")) # noqa: W291, W293

# Verify SQL DDL statement.
self.metadata.create_all(self.engine, tables=[ItemStore.__table__], checkfirst=False)
self.assertEqual(self.executed_statement, dedent("""
CREATE TABLE testdrive.item (
\tid INT NOT NULL,
\tname STRING,
\troot_id INT,
\tPRIMARY KEY (id)
)
""")) # noqa: W291, W293

# Verify if corresponding warning is emitted.
self.assertEqual(len(w), 1)
self.assertIsSubclass(w[-1].category, UserWarning)
self.assertIn("CrateDB does not support foreign key constraints, "
"they will be omitted when generating DDL statements.", str(w[-1].message))

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

self.metadata.create_all(self.engine, tables=[FooBar.__table__], checkfirst=False)
self.assertEqual(self.executed_statement, dedent("""
CREATE TABLE testdrive.foobar (
\tid INT NOT NULL,
\tname STRING,
\tPRIMARY KEY (id)
)
""")) # noqa: W291
with warnings.catch_warnings(record=True) as w:

# Cause all warnings to always be triggered.
warnings.simplefilter("always")

# Verify SQL DDL statement.
self.metadata.create_all(self.engine, tables=[FooBar.__table__], checkfirst=False)
self.assertEqual(self.executed_statement, dedent("""
CREATE TABLE testdrive.foobar (
\tid INT NOT NULL,
\tname STRING,
\tPRIMARY KEY (id)
)
""")) # noqa: W291, W293

# Verify if corresponding warning is emitted.
self.assertEqual(len(w), 1)
self.assertIsSubclass(w[-1].category, UserWarning)
self.assertIn("CrateDB does not support unique constraints, "
"they will be omitted when generating DDL statements.", str(w[-1].message))

0 comments on commit 47ad022

Please sign in to comment.