]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Fix compat issue with older SQLAlchemy versions.
authorFederico Caselli <cfederico87@gmail.com>
Mon, 8 May 2023 21:05:40 +0000 (23:05 +0200)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 9 May 2023 22:11:13 +0000 (18:11 -0400)
Added placeholder classes for ``Computed`` and ``Identity`` when older 1.x
SQLAlchemy versions are in use, namely prior to SQLAlchemy 1.3.11 when the
``Computed`` construct was introduced. Previously these were set to None,
however this could cause issues with certain codepaths that were using
``isinstance()`` such as one within "batch mode".

Fixes: #1237
Change-Id: I033712158baa68cca6a56bd41d0636109e156b86

alembic/testing/fixtures.py
alembic/testing/requirements.py
alembic/testing/util.py
alembic/util/__init__.py
alembic/util/sqla_compat.py
docs/build/unreleased/1109.rst
docs/build/unreleased/1237.rst [new file with mode: 0644]

index ef1c3bbaf6aa8fd7c7743e88cd7232bd3aeb1ec5..65f3a0a9ad952fb859cf62fbb60b2c6e6fe5a95b 100644 (file)
@@ -28,7 +28,7 @@ from ..operations import Operations
 from ..util import sqla_compat
 from ..util.sqla_compat import create_mock_engine
 from ..util.sqla_compat import sqla_14
-from ..util.sqla_compat import sqla_1x
+from ..util.sqla_compat import sqla_2
 
 
 testing_config = configparser.ConfigParser()
@@ -36,10 +36,7 @@ testing_config.read(["test.cfg"])
 
 
 class TestBase(SQLAlchemyTestBase):
-    if sqla_1x:
-        is_sqlalchemy_future = False
-    else:
-        is_sqlalchemy_future = True
+    is_sqlalchemy_future = sqla_2
 
     @testing.fixture()
     def ops_context(self, migration_context):
index a4a6045e88c8eed0cbca5721d365507893d6f19b..40de4cd218a53206c47bda737b678f2ca2cd262c 100644 (file)
@@ -84,7 +84,7 @@ class SuiteRequirements(Requirements):
     @property
     def sqlalchemy_1x(self):
         return exclusions.skip_if(
-            lambda config: not util.sqla_1x,
+            lambda config: util.sqla_2,
             "SQLAlchemy 1.x test",
         )
 
index e65597d9ac7579be3bdf16de63d7697c98510ad0..4517a69f6b5c4ebdc34702005074e83178cc9d95 100644 (file)
@@ -6,12 +6,13 @@
 # the MIT License: http://www.opensource.org/licenses/mit-license.php
 from __future__ import annotations
 
-import re
 import types
 from typing import Union
 
 from sqlalchemy.util import inspect_getfullargspec
 
+from ..util import sqla_2
+
 
 def flag_combinations(*combinations):
     """A facade around @testing.combinations() oriented towards boolean
@@ -114,17 +115,11 @@ def _safe_int(value: str) -> Union[int, str]:
 def testing_engine(url=None, options=None, future=False):
     from sqlalchemy.testing import config
     from sqlalchemy.testing.engines import testing_engine
-    from sqlalchemy import __version__
-
-    _vers = tuple(
-        [_safe_int(x) for x in re.findall(r"(\d+|[abc]\d)", __version__)]
-    )
-    sqla_1x = _vers < (2,)
 
     if not future:
         future = getattr(config._current.options, "future_engine", False)
 
-    if sqla_1x:
+    if not sqla_2:
         kw = {"future": future} if future else {}
     else:
         kw = {}
index 8f684abcff8fe6a5db058640ac7c47d02c195d4a..3c1e27ca4a3158aa388ee47caaf49233c1d7007e 100644 (file)
@@ -28,7 +28,6 @@ from .pyfiles import template_to_file
 from .sqla_compat import has_computed
 from .sqla_compat import sqla_13
 from .sqla_compat import sqla_14
-from .sqla_compat import sqla_1x
 from .sqla_compat import sqla_2
 
 
index e2725d6c5c9ea718504315841440c659457ed64f..00703376407ea212d687e0271e3d0182309a448f 100644 (file)
@@ -71,18 +71,17 @@ except ImportError:
     from sqlalchemy.sql.elements import _NONE_NAME as _NONE_NAME  # type: ignore  # noqa: E501
 
 
-if sqla_14:
-    # when future engine merges, this can be again based on version string
-    from sqlalchemy.engine import Connection as legacy_connection
+class _Unsupported:
+    "Placeholder for unsupported SQLAlchemy classes"
 
-    sqla_1x = not hasattr(legacy_connection, "commit")
-else:
-    sqla_1x = True
 
 try:
-    from sqlalchemy import Computed  # noqa
+    from sqlalchemy import Computed
 except ImportError:
-    Computed = type(None)  # type: ignore
+
+    class Computed(_Unsupported):  # type: ignore
+        pass
+
     has_computed = False
     has_computed_reflection = False
 else:
@@ -90,12 +89,15 @@ else:
     has_computed_reflection = _vers >= (1, 3, 16)
 
 try:
-    from sqlalchemy import Identity  # noqa
+    from sqlalchemy import Identity
 except ImportError:
-    Identity = type(None)  # type: ignore
+
+    class Identity(_Unsupported):  # type: ignore
+        pass
+
     has_identity = False
 else:
-    # attributes common to Indentity and Sequence
+    # attributes common to Identity and Sequence
     _identity_options_attrs = (
         "start",
         "increment",
@@ -107,7 +109,7 @@ else:
         "cache",
         "order",
     )
-    # attributes of Indentity
+    # attributes of Identity
     _identity_attrs = _identity_options_attrs + ("on_null",)
     has_identity = True
 
index b6c07183386ce468f472d9867a4dcdf32b9dea35..62be487a1b7d9093c668b78500ac8688036ae597 100644 (file)
@@ -1,5 +1,6 @@
 .. change::
     :tags: usecase, commands
+    :tickets: 1109
 
     Added quiet option to the command line, using the ``-q/--quiet``
     option. This flag will prevent alembic from logging anything
diff --git a/docs/build/unreleased/1237.rst b/docs/build/unreleased/1237.rst
new file mode 100644 (file)
index 0000000..9717064
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, batch
+    :tickets: 1237
+
+    Added placeholder classes for ``Computed`` and ``Identity`` when older 1.x
+    SQLAlchemy versions are in use, namely prior to SQLAlchemy 1.3.11 when the
+    ``Computed`` construct was introduced. Previously these were set to None,
+    however this could cause issues with certain codepaths that were using
+    ``isinstance()`` such as one within "batch mode".