]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Improve index autogenerare detection on PostgreSQL
authorFederico Caselli <cfederico87@gmail.com>
Mon, 17 Jul 2023 20:45:00 +0000 (22:45 +0200)
committerFederico Caselli <cfederico87@gmail.com>
Tue, 8 Aug 2023 20:02:15 +0000 (22:02 +0200)
Improved autogenerate compare of expression based indexes on PostgreSQL
to produce fewer wrong detections.

Fixes: #1270
Change-Id: I468e27c6311c5449adfb92d0da07180b1c11cdd6

alembic/ddl/postgresql.py
docs/build/changelog.rst
docs/build/unreleased/1270.rst [new file with mode: 0644]
tests/test_autogen_indexes.py

index f8ae97046690981510f5dc8b5ac1495d3bf73339..af0d156ea5632625b98495ff1a878dda4b6a269e 100644 (file)
@@ -256,8 +256,7 @@ class PostgresqlImpl(DefaultImpl):
         self, index: Index, expr: str, remove_suffix: str
     ) -> str:
         # start = expr
-        expr = expr.lower()
-        expr = expr.replace('"', "")
+        expr = expr.lower().replace('"', "").replace("'", "")
         if index.table is not None:
             # should not be needed, since include_table=False is in compile
             expr = expr.replace(f"{index.table.name.lower()}.", "")
index 3971c977f27f2d97f6b168f5e14c74f9158e4449..9b114e8507240c251a24873cb6bbd7da66ac897c 100644 (file)
@@ -215,7 +215,7 @@ Changelog
         Added support for autogenerate comparison of indexes on PostgreSQL which
         include SQL sort option, such as ``ASC`` or ``NULLS FIRST``.
         The sort options are correctly detected only when defined using the
-        sqlalchemy modifier functions, such as ``asc()`` or ``nulls_first``,
+        sqlalchemy modifier functions, such as ``asc()`` or ``nulls_first()``,
         or the equivalent methods.
         Passing sort options inside the ``postgresql_ops`` dict is not supported.
 
diff --git a/docs/build/unreleased/1270.rst b/docs/build/unreleased/1270.rst
new file mode 100644 (file)
index 0000000..9133323
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: but, autogenerate, postgresql
+    :tickets: 1248
+
+    Improved autogenerate compare of expression based indexes on PostgreSQL
+    to produce fewer wrong detections.
index 9ec33d0f46a0a6a60de8adcf6bbc57e8244000cf..01a19d59c6731263f55a740f3ab1987ef812d9a9 100644 (file)
@@ -13,6 +13,7 @@ from sqlalchemy import Numeric
 from sqlalchemy import PrimaryKeyConstraint
 from sqlalchemy import String
 from sqlalchemy import Table
+from sqlalchemy import text
 from sqlalchemy import UniqueConstraint
 from sqlalchemy.dialects.postgresql import DOUBLE_PRECISION
 from sqlalchemy.sql.expression import asc
@@ -1402,6 +1403,10 @@ def _lots_of_indexes(flatten: bool = False):
             lambda t: Index("SomeIndex", t.c.ff + 42),
             lambda t: Index("SomeIndex", 42 + t.c.ff),
         ),
+        (
+            lambda t: Index("SomeIndex", text("coalesce(z, -1)"), _table=t),
+            lambda t: Index("SomeIndex", text("coalesce(q, '-1')"), _table=t),
+        ),
     ]
 
     with_sort = [