]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
add variant render step for user-defined types
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 7 Feb 2023 04:30:58 +0000 (23:30 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 7 Feb 2023 13:58:33 +0000 (08:58 -0500)
due to SQLA 2.0's variant being integrated into types,
the variant rendering conditional would no longer take effect
as the type was not under the "sqlalchemy" module namespace.

Fixed issue where rendering of user-defined types that then went onto use
the ``.with_variant()`` method would fail to render, if using SQLAlchemy
2.0's version of variants.

Change-Id: I3c6f14325d6dffb2ddc1bf955753ee5a2de2cedd
Fixes: #1167
alembic/autogenerate/render.py
docs/build/unreleased/1167.rst [new file with mode: 0644]
tests/test_autogen_render.py

index 41903d81ec8444f29b426c058e978eed365c199e..4a144db74a7c881368ccc106aef265820a464544 100644 (file)
@@ -827,9 +827,9 @@ 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 not _skip_variants and sqla_compat._type_has_variants(type_):
-            return _render_Variant_type(type_, autogen_context)
         if "_render_%s_type" % type_.__visit_name__ in globals():
             fn = globals()["_render_%s_type" % type_.__visit_name__]
             return fn(type_, autogen_context)
diff --git a/docs/build/unreleased/1167.rst b/docs/build/unreleased/1167.rst
new file mode 100644 (file)
index 0000000..9c81253
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, autogenerate
+    :tickets: 1167
+
+    Fixed issue where rendering of user-defined types that then went onto use
+    the ``.with_variant()`` method would fail to render, if using SQLAlchemy
+    2.0's version of variants.
+
index 0a2fc87662541058cf0849b3b7a31beee79bbec2..f138df5fcc3d881287373a46ad9aa5574decad16 100644 (file)
@@ -1757,19 +1757,38 @@ class AutogenRenderTest(TestBase):
             "    # ### end Alembic commands ###",
         )
 
-    def test_repr_custom_type_w_sqla_prefix(self):
+    @testing.combinations("sqlaname", "nonsqlaname", argnames="modname")
+    @testing.combinations("usevariant", "plain", argnames="construct")
+    def test_repr_custom_type(self, modname, construct):
+        """test #1167 as well as other user defined type variations"""
+
         self.autogen_context.opts["user_module_prefix"] = None
 
         class MyType(UserDefinedType):
             pass
 
-        MyType.__module__ = "sqlalchemy_util.types"
+        if modname == "sqlaname":
+            MyType.__module__ = mod = "sqlalchemy_util.types"
+        elif modname == "nonsqlaname":
+            MyType.__module__ = mod = "mymodule"
+        else:
+            assert False
 
-        type_ = MyType()
+        if construct == "usevariant":
+            type_ = MyType().with_variant(String(), "mysql")
+        elif construct == "plain":
+            type_ = MyType()
+        else:
+            assert False
 
         eq_ignore_whitespace(
             autogenerate.render._repr_type(type_, self.autogen_context),
-            "sqlalchemy_util.types.MyType()",
+            f"{mod}.MyType()"
+            + (
+                ".with_variant(sa.String(), 'mysql')"
+                if construct == "usevariant"
+                else ""
+            ),
         )
 
     def test_render_variant(self):