Skip to content

Commit

Permalink
check for variants (recursion branch) first in all cases
Browse files Browse the repository at this point in the history
Fixed bug where autogen render of a "variant" type would fail to catch the
variants if the leading type were a dialect-specific type, rather than a
generic type.

Fixes: #1585
Change-Id: I189e9ab3674b09700f2c774b600f6ff3abb52cd0
  • Loading branch information
zzzeek committed Dec 24, 2024
1 parent de3958b commit 0e0c141
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
7 changes: 4 additions & 3 deletions alembic/autogenerate/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,10 @@ def _repr_type(

mod = type(type_).__module__
imports = autogen_context.imports
if mod.startswith("sqlalchemy.dialects"):

if not _skip_variants and sqla_compat._type_has_variants(type_):
return _render_Variant_type(type_, autogen_context)
elif mod.startswith("sqlalchemy.dialects"):
match = re.match(r"sqlalchemy\.dialects\.(\w+)", mod)
assert match is not None
dname = match.group(1)
Expand All @@ -843,8 +846,6 @@ def _repr_type(
return "%s.%r" % (dname, type_)
elif impl_rt:
return impl_rt
elif not _skip_variants and sqla_compat._type_has_variants(type_):
return _render_Variant_type(type_, autogen_context)
elif mod.startswith("sqlalchemy."):
if "_render_%s_type" % type_.__visit_name__ in globals():
fn = globals()["_render_%s_type" % type_.__visit_name__]
Expand Down
8 changes: 8 additions & 0 deletions docs/build/unreleased/1585.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.. change::
:tags: bug, autogenerate
:tickets: 1585

Fixed bug where autogen render of a "variant" type would fail to catch the
variants if the leading type were a dialect-specific type, rather than a
generic type.

14 changes: 13 additions & 1 deletion tests/test_autogen_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from sqlalchemy import Unicode
from sqlalchemy import UniqueConstraint
from sqlalchemy import VARCHAR
from sqlalchemy.dialects.mysql import LONGTEXT
from sqlalchemy.engine.default import DefaultDialect
from sqlalchemy.sql import and_
from sqlalchemy.sql import column
Expand Down Expand Up @@ -1842,14 +1843,25 @@ def test_render_variant(self):
.with_variant(CHAR(15), "oracle")
)

# the new Black formatting will help a lot with this
eq_ignore_whitespace(
autogenerate.render._repr_type(type_, self.autogen_context),
"sa.String(length=5)."
"with_variant(sa.VARCHAR(length=10), 'mysql')."
"with_variant(sa.CHAR(length=15), 'oracle')",
)

def test_render_reverse_variant(self):
"""test #1585"""

self.autogen_context.opts["user_module_prefix"] = None

type_ = LONGTEXT().with_variant(String(10), "oracle")

eq_ignore_whitespace(
autogenerate.render._repr_type(type_, self.autogen_context),
"mysql.LONGTEXT()." "with_variant(sa.String(length=10), 'oracle')",
)

def test_repr_user_type_user_prefix_None(self):
class MyType(UserDefinedType):
def get_col_spec(self):
Expand Down

0 comments on commit 0e0c141

Please sign in to comment.