]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
check for variants (recursion branch) first in all cases
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 24 Dec 2024 15:30:10 +0000 (10:30 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 24 Dec 2024 15:30:10 +0000 (10:30 -0500)
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

alembic/autogenerate/render.py
docs/build/unreleased/1585.rst [new file with mode: 0644]
tests/test_autogen_render.py

index 38bdbfca26e3d87623d9807e884498e9f7e3fcff..6ebfbf9f3ffb0979be1ea0a8f6ae9a0cdf523587 100644 (file)
@@ -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)
@@ -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__]
diff --git a/docs/build/unreleased/1585.rst b/docs/build/unreleased/1585.rst
new file mode 100644 (file)
index 0000000..d6d6de0
--- /dev/null
@@ -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.
+
index 14a33194fdead34fe29d82b1f02f35c33c9d4943..6c78a8b436522d27fdcdffa8612cc8153468a07b 100644 (file)
@@ -26,6 +26,7 @@ from sqlalchemy import types
 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
@@ -1842,7 +1843,6 @@ class AutogenRenderTest(TestBase):
             .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)."
@@ -1850,6 +1850,18 @@ class AutogenRenderTest(TestBase):
             "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):