Skip to content

Commit

Permalink
Merge "Fix compat issue with older SQLAlchemy versions." into main
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzeek authored and Gerrit Code Review committed May 9, 2023
2 parents 2aba0ad + 2866272 commit 92e54a0
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 27 deletions.
7 changes: 2 additions & 5 deletions alembic/testing/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,15 @@
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()
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):
Expand Down
2 changes: 1 addition & 1 deletion alembic/testing/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def sqlalchemy_14(self):
@property
def sqlalchemy_1x(self):
return exclusions.skip_if(
lambda config: not util.sqla_1x,
lambda config: util.sqla_2,
"SQLAlchemy 1.x test",
)

Expand Down
11 changes: 3 additions & 8 deletions alembic/testing/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {}
Expand Down
1 change: 0 additions & 1 deletion alembic/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
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


Expand Down
26 changes: 14 additions & 12 deletions alembic/util/sqla_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,33 @@ def _safe_int(value: str) -> Union[int, str]:
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:
has_computed = True
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",
Expand All @@ -107,7 +109,7 @@ def _safe_int(value: str) -> Union[int, str]:
"cache",
"order",
)
# attributes of Indentity
# attributes of Identity
_identity_attrs = _identity_options_attrs + ("on_null",)
has_identity = True

Expand Down
1 change: 1 addition & 0 deletions docs/build/unreleased/1109.rst
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 9 additions & 0 deletions docs/build/unreleased/1237.rst
Original file line number Diff line number Diff line change
@@ -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".

0 comments on commit 92e54a0

Please sign in to comment.