Skip to content

SQLAlchemy: Improve DDL compiler to ignore unsupported UNIQUE and FOREIGN KEY constraints #582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
SQLAlchemy: Improve DDL compiler to ignore foreign key constraints
  • Loading branch information
amotl committed Sep 28, 2023
commit 2f8e185bdd4176b0583b98c8b59a24cc4b00a579
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Unreleased
certificate details are immanent, like no longer accepting the long
deprecated ``commonName`` attribute. Instead, going forward, only the
``subjectAltName`` attribute will be used.
- SQLAlchemy: Improve DDL compiler to ignore foreign key constraints

.. _urllib3 v2.0 migration guide: https://urllib3.readthedocs.io/en/latest/v2-migration-guide.html
.. _urllib3 v2.0 roadmap: https://urllib3.readthedocs.io/en/stable/v2-roadmap.html
Expand Down
5 changes: 5 additions & 0 deletions src/crate/client/sqlalchemy/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ def post_create_table(self, table):
', '.join(sorted(table_opts)))
return special_options

def visit_foreign_key_constraint(self, constraint, **kw):
"""
CrateDB does not support foreign key constraints.
"""
return None

class CrateTypeCompiler(compiler.GenericTypeCompiler):

Expand Down
3 changes: 2 additions & 1 deletion src/crate/client/sqlalchemy/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .connection_test import SqlAlchemyConnectionTest
from .dict_test import SqlAlchemyDictTypeTest
from .datetime_test import SqlAlchemyDateAndDateTimeTest
from .compiler_test import SqlAlchemyCompilerTest
from .compiler_test import SqlAlchemyCompilerTest, SqlAlchemyDDLCompilerTest
from .update_test import SqlAlchemyUpdateTest
from .match_test import SqlAlchemyMatchTest
from .bulk_test import SqlAlchemyBulkTest
Expand All @@ -36,6 +36,7 @@ def test_suite_unit():
tests.addTest(makeSuite(SqlAlchemyDictTypeTest))
tests.addTest(makeSuite(SqlAlchemyDateAndDateTimeTest))
tests.addTest(makeSuite(SqlAlchemyCompilerTest))
tests.addTest(makeSuite(SqlAlchemyDDLCompilerTest))
tests.addTest(ParametrizedTestCase.parametrize(SqlAlchemyCompilerTest, param={"server_version_info": None}))
tests.addTest(ParametrizedTestCase.parametrize(SqlAlchemyCompilerTest, param={"server_version_info": (4, 0, 12)}))
tests.addTest(ParametrizedTestCase.parametrize(SqlAlchemyCompilerTest, param={"server_version_info": (4, 1, 10)}))
Expand Down
104 changes: 103 additions & 1 deletion src/crate/client/sqlalchemy/tests/compiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
# with Crate these terms will supersede the license and you may use the
# software solely pursuant to the terms of the relevant commercial agreement.
from textwrap import dedent
from unittest import mock, skipIf
from unittest import mock, skipIf, TestCase
from unittest.mock import MagicMock, patch

from crate.client.cursor import Cursor
from crate.client.sqlalchemy.compiler import crate_before_execute

import sqlalchemy as sa
Expand All @@ -30,6 +32,8 @@
from crate.client.sqlalchemy.types import ObjectType
from crate.client.test_util import ParametrizedTestCase

from crate.testing.settings import crate_host


class SqlAlchemyCompilerTest(ParametrizedTestCase):

Expand Down Expand Up @@ -244,3 +248,101 @@ def test_insert_manyvalues(self):
mock.call(mock.ANY, 'INSERT INTO mytable (name) VALUES (?), (?)', ('foo_2', 'foo_3'), None),
mock.call(mock.ANY, 'INSERT INTO mytable (name) VALUES (?)', ('foo_4', ), None),
])


FakeCursor = MagicMock(name='FakeCursor', spec=Cursor)


class CompilerTestCase(TestCase):
"""
A base class for providing mocking infrastructure to validate the DDL compiler.
"""

def setUp(self):
self.engine = sa.create_engine(f"crate://{crate_host}")
self.metadata = sa.MetaData(schema="testdrive")
self.session = sa.orm.Session(bind=self.engine)
self.setup_mock()

def setup_mock(self):
"""
Set up a fake cursor, in order to intercept query execution.
"""

self.fake_cursor = MagicMock(name="fake_cursor")
FakeCursor.return_value = self.fake_cursor

self.executed_statement = None
self.fake_cursor.execute = self.execute_wrapper

def execute_wrapper(self, query, *args, **kwargs):
"""
Receive the SQL query expression, and store it.
"""
self.executed_statement = query
return self.fake_cursor


@patch('crate.client.connection.Cursor', FakeCursor)
class SqlAlchemyDDLCompilerTest(CompilerTestCase):
"""
Verify a few scenarios regarding the DDL compiler.
"""

def test_ddl_with_foreign_keys(self):
"""
Verify the CrateDB dialect properly ignores foreign key constraints.
"""

Base = sa.orm.declarative_base(metadata=self.metadata)

class RootStore(Base):
"""The main store."""

__tablename__ = "root"

id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String)

items = sa.orm.relationship(
"ItemStore",
back_populates="root",
passive_deletes=True,
)

class ItemStore(Base):
"""The auxiliary store."""

__tablename__ = "item"

id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String)
root_id = sa.Column(
sa.Integer,
sa.ForeignKey(
f"{RootStore.__tablename__}.id",
ondelete="CASCADE",
),
)
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy