]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Enable compare type by default
authorFederico Caselli <cfederico87@gmail.com>
Fri, 11 Aug 2023 21:05:50 +0000 (23:05 +0200)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 22 Aug 2023 18:36:28 +0000 (14:36 -0400)
Change the default value of :paramref:`.EnvironmentContext.configure.compare_type`
to ``True``.

Fixes: #1248
Change-Id: I6fd93de73f3b239dffddec98008083c10e154334

alembic/__init__.py
alembic/context.pyi
alembic/runtime/environment.py
alembic/runtime/migration.py
docs/build/autogenerate.rst
docs/build/unreleased/1248.rst [new file with mode: 0644]
tests/test_autogen_diffs.py

index e9698de53e300375aadf85bdcca99433de9d27ee..f391826a5871770c37893a8e4bf07aef600e3fc5 100644 (file)
@@ -3,4 +3,4 @@ import sys
 from . import context
 from . import op
 
-__version__ = "1.11.4"
+__version__ = "1.12.0"
index 265d723ce0d285e3c48b333819e26701d918be6b..469797631bc5735d440459a39ce3ea8c320b968e 100644 (file)
@@ -26,6 +26,7 @@ if TYPE_CHECKING:
     from sqlalchemy.sql.schema import FetchedValue
     from sqlalchemy.sql.schema import MetaData
     from sqlalchemy.sql.schema import SchemaItem
+    from sqlalchemy.sql.type_api import TypeEngine
 
     from .autogenerate.api import AutogenContext
     from .config import Config
@@ -145,7 +146,19 @@ def configure(
             [MigrationContext, Tuple[str, str], List[MigrateOperation]], None
         ]
     ] = None,
-    compare_type: bool = False,
+    compare_type: Union[
+        bool,
+        Callable[
+            [
+                MigrationContext,
+                Column[Any],
+                Column[Any],
+                TypeEngine,
+                TypeEngine,
+            ],
+            Optional[bool],
+        ],
+    ] = True,
     compare_server_default: Union[
         bool,
         Callable[
@@ -308,12 +321,16 @@ def configure(
      to produce candidate upgrade/downgrade operations.
     :param compare_type: Indicates type comparison behavior during
      an autogenerate
-     operation.  Defaults to ``False`` which disables type
-     comparison.  Set to
-     ``True`` to turn on default type comparison, which has varied
-     accuracy depending on backend.   See :ref:`compare_types`
+     operation.  Defaults to ``True`` turning on type comparison, which
+     has good accuracy on most backends.   See :ref:`compare_types`
      for an example as well as information on other type
-     comparison options.
+     comparison options. Set to ``False`` which disables type
+     comparison. A callable can also be passed to provide custom type
+     comparison, see :ref:`compare_types` for additional details.
+
+     .. versionchanged:: 1.12.0 The default value of
+        :paramref:`.EnvironmentContext.configure.compare_type` has been
+        changed to ``True``.
 
      .. seealso::
 
index 7efa0f02ffb37a2f64b634bb687b4cbdb474e173..d729da1990f599cd88f8a5d522abc2a404655e0c 100644 (file)
@@ -30,6 +30,7 @@ if TYPE_CHECKING:
     from sqlalchemy.sql.elements import ClauseElement
     from sqlalchemy.sql.schema import MetaData
     from sqlalchemy.sql.schema import SchemaItem
+    from sqlalchemy.sql.type_api import TypeEngine
 
     from .migration import MigrationInfo
     from ..autogenerate.api import AutogenContext
@@ -92,6 +93,17 @@ CompareServerDefault = Callable[
     Optional[bool],
 ]
 
+CompareType = Callable[
+    [
+        MigrationContext,
+        "Column[Any]",
+        "Column[Any]",
+        "TypeEngine[Any]",
+        "TypeEngine[Any]",
+    ],
+    Optional[bool],
+]
+
 
 class EnvironmentContext(util.ModuleClsProxy):
 
@@ -410,7 +422,7 @@ class EnvironmentContext(util.ModuleClsProxy):
         process_revision_directives: Optional[
             ProcessRevisionDirectiveFn
         ] = None,
-        compare_type: bool = False,
+        compare_type: Union[bool, CompareType] = True,
         compare_server_default: Union[bool, CompareServerDefault] = False,
         render_item: Optional[RenderItemFn] = None,
         literal_binds: bool = False,
@@ -548,12 +560,16 @@ class EnvironmentContext(util.ModuleClsProxy):
          to produce candidate upgrade/downgrade operations.
         :param compare_type: Indicates type comparison behavior during
          an autogenerate
-         operation.  Defaults to ``False`` which disables type
-         comparison.  Set to
-         ``True`` to turn on default type comparison, which has varied
-         accuracy depending on backend.   See :ref:`compare_types`
+         operation.  Defaults to ``True`` turning on type comparison, which
+         has good accuracy on most backends.   See :ref:`compare_types`
          for an example as well as information on other type
-         comparison options.
+         comparison options. Set to ``False`` which disables type
+         comparison. A callable can also be passed to provide custom type
+         comparison, see :ref:`compare_types` for additional details.
+
+         .. versionchanged:: 1.12.0 The default value of
+            :paramref:`.EnvironmentContext.configure.compare_type` has been
+            changed to ``True``.
 
          .. seealso::
 
@@ -880,8 +896,7 @@ class EnvironmentContext(util.ModuleClsProxy):
 
         if render_item is not None:
             opts["render_item"] = render_item
-        if compare_type is not None:
-            opts["compare_type"] = compare_type
+        opts["compare_type"] = compare_type
         if compare_server_default is not None:
             opts["compare_server_default"] = compare_server_default
         opts["script"] = self.script
index 76742294e12962b04665fb464d41814067ea9bba..50518ffee2717ea6426f2013aec707211404081f 100644 (file)
@@ -178,7 +178,7 @@ class MigrationContext:
         else:
             self.output_buffer = opts.get("output_buffer", sys.stdout)
 
-        self._user_compare_type = opts.get("compare_type", False)
+        self._user_compare_type = opts.get("compare_type", True)
         self._user_compare_server_default = opts.get(
             "compare_server_default", False
         )
index 5f34c0a064efef4971f97efdcefebf94042c1d26..7a03c9050f33f113e255dfdecc3aa9f285e403a8 100644 (file)
@@ -126,9 +126,9 @@ Autogenerate **will detect**:
 
 Autogenerate can **optionally detect**:
 
-* Change of column type.  This will occur if you set
-  the :paramref:`.EnvironmentContext.configure.compare_type` parameter to
-  ``True``.   The default implementation will reliably detect major changes,
+* Change of column type.  This will occur by default unless the parameter
+  :paramref:`.EnvironmentContext.configure.compare_type` is set to ``False``.
+  The default implementation will reliably detect major changes,
   such as between :class:`.Numeric` and :class:`.String`, as well as
   accommodate for the types generated by SQLAlchemy's "generic" types such as
   :class:`.Boolean`.   Arguments that are shared between both types, such as
@@ -587,15 +587,19 @@ Comparing Types
 ^^^^^^^^^^^^^^^^
 
 The default type comparison logic will work for SQLAlchemy built in types as
-well as basic user defined types.   This logic is only enabled if the
-:paramref:`.EnvironmentContext.configure.compare_type` parameter
-is set to True::
+well as basic user defined types.   This logic is enabled by default.
+It can be disabled by setting the
+:paramref:`.EnvironmentContext.configure.compare_type` to ``False``::
 
     context.configure(
         # ...
-        compare_type = True
+        compare_type = False
     )
 
+.. versionchanged:: 1.12.0 The default value of
+   :paramref:`.EnvironmentContext.configure.compare_type` has been changed
+   to ``True``.
+
 .. note::
 
    The default type comparison logic (which is end-user extensible) currently
@@ -604,7 +608,7 @@ is set to True::
 
    * First, it compares the outer type of each column such as ``VARCHAR``
      or ``TEXT``. Dialect implementations can have synonyms that are considered
-     equivalent- this is because some databases support types by converting them
+     equivalent, this is because some databases support types by converting them
      to another type. For example, NUMERIC and DECIMAL are considered equivalent
      on all backends, while on the Oracle backend the additional synonyms
      BIGINT, INTEGER, NUMBER, SMALLINT are added to this list of equivalents
@@ -670,10 +674,10 @@ will proceed::
 
     The boolean return values for the above
     ``compare_against_backend`` method, which is part of SQLAlchemy and not
-    Alembic,are **the opposite** of that of the
+    Alembic, are **the opposite** of that of the
     :paramref:`.EnvironmentContext.configure.compare_type` callable, returning
     ``True`` for types that are the same vs. ``False`` for types that are
-    different.The :paramref:`.EnvironmentContext.configure.compare_type`
+    different. The :paramref:`.EnvironmentContext.configure.compare_type`
     callable on the other hand should return ``True`` for types that are
     **different**.
 
@@ -683,7 +687,7 @@ type itself implementing ``compare_against_backend`` is that the
 :paramref:`.EnvironmentContext.configure.compare_type` callable is favored
 first; if it returns ``None``, then the ``compare_against_backend`` method
 will be used, if present on the metadata type.  If that returns ``None``,
-then a basic check for type equivalence is run.
+then the default check for type equivalence is run.
 
 .. _post_write_hooks:
 
diff --git a/docs/build/unreleased/1248.rst b/docs/build/unreleased/1248.rst
new file mode 100644 (file)
index 0000000..5e9984e
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: usecase, autogenerate
+    :tickets: 1248
+
+    Change the default value of
+    :paramref:`.EnvironmentContext.configure.compare_type` to ``True``.
+    As Alembic's autogenerate for types was dramatically improved in
+    version 1.4 released in 2020, the type comparison feature is now much
+    more reliable so is now enabled by default.
index ebba04bbc3bfd63c1868f435264b964eb979a227..d03771ee8f341f78d86c5227d7dde3d3586797ba 100644 (file)
@@ -1755,9 +1755,14 @@ class PGCompareMetaData(ModelOne, AutogenTest, TestBase):
             diffs[4][3],
         )
 
-        eq_(diffs[5][0][0], "modify_nullable")
-        eq_(diffs[5][0][5], False)
-        eq_(diffs[5][0][6], True)
+        eq_(diffs[5][0][0], "modify_type")
+        eq_(diffs[5][0][1:4], ("test_schema", "order", "amount"))
+        eq_(diffs[5][0][5].precision, 8)
+        eq_(diffs[5][0][6].precision, 10)
+
+        eq_(diffs[5][1][0], "modify_nullable")
+        eq_(diffs[5][1][5], False)
+        eq_(diffs[5][1][6], True)
 
 
 class OrigObjectTest(TestBase):