]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Support for `IF EXISTS` in SQL Server 2016 (13.x) and later versions
authorEdgar Ramírez Mondragón <edgarrm358@gmail.com>
Thu, 18 Dec 2025 19:58:59 +0000 (14:58 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 19 Dec 2025 19:44:51 +0000 (14:44 -0500)
Added support for the ``IF EXISTS`` clause when dropping indexes on SQL
Server 2016 (13.x) and later versions. The :paramref:`.DropIndex.if_exists`
parameter is now honored by the SQL Server dialect, allowing conditional
index drops that will not raise an error if the index does not exist.
Pull request courtesy Edgar Ramírez Mondragón.

Fixes: #13045
Closes: #13046
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/13046
Pull-request-sha: 65dca2055cb403430730b5cf42f0c5f55b23bfb1

Change-Id: Iab95b1a46003b38709a791b8a7c4233dfda5e830
(cherry picked from commit 40fc3c90e1fee3f7a19184ab57cca3cbcdfa6da1)

doc/build/changelog/unreleased_20/13045.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/mssql/base.py
test/dialect/mssql/test_compiler.py

diff --git a/doc/build/changelog/unreleased_20/13045.rst b/doc/build/changelog/unreleased_20/13045.rst
new file mode 100644 (file)
index 0000000..9ac8257
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: usecase, mssql
+    :tickets: 13045
+
+    Added support for the ``IF EXISTS`` clause when dropping indexes on SQL
+    Server 2016 (13.x) and later versions. The :paramref:`.DropIndex.if_exists`
+    parameter is now honored by the SQL Server dialect, allowing conditional
+    index drops that will not raise an error if the index does not exist.
+    Pull request courtesy Edgar Ramírez Mondragón.
index d422ad8c3d930e04da4159ffaa3e8ed1e59aea6d..148a4f83e19649115ed39181e08c0a150e52729d 100644 (file)
@@ -984,6 +984,7 @@ import codecs
 import datetime
 import operator
 import re
+from typing import Any
 from typing import overload
 from typing import TYPE_CHECKING
 from uuid import UUID as _python_UUID
@@ -1034,6 +1035,7 @@ from ...util import update_wrapper
 from ...util.typing import Literal
 
 if TYPE_CHECKING:
+    from ...sql.ddl import DropIndex
     from ...sql.dml import DMLState
     from ...sql.selectable import TableClause
 
@@ -2700,11 +2702,13 @@ class MSDDLCompiler(compiler.DDLCompiler):
 
         return text
 
-    def visit_drop_index(self, drop, **kw):
-        return "\nDROP INDEX %s ON %s" % (
-            self._prepared_index_name(drop.element, include_schema=False),
-            self.preparer.format_table(drop.element.table),
+    def visit_drop_index(self, drop: DropIndex, **kw: Any) -> str:
+        index_name = self._prepared_index_name(
+            drop.element, include_schema=False
         )
+        table_name = self.preparer.format_table(drop.element.table)
+        if_exists = " IF EXISTS" if drop.if_exists else ""
+        return f"\nDROP INDEX{if_exists} {index_name} ON {table_name}"
 
     def visit_primary_key_constraint(self, constraint, **kw):
         if len(constraint) == 0:
index eb4dba0a079683ea06ed3f7a20fc7e395adf2aff..8de8a218540517725a4b35e28a29e8f16fa2f395 100644 (file)
@@ -1532,6 +1532,14 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "DROP INDEX idx_foo ON bar.foo",
         )
 
+    def test_drop_index_if_exists(self):
+        m = MetaData()
+        t1 = Table("foo", m, Column("x", Integer), schema="bar")
+        self.assert_compile(
+            schema.DropIndex(Index("idx_foo", t1.c.x), if_exists=True),
+            "DROP INDEX IF EXISTS idx_foo ON bar.foo",
+        )
+
     def test_index_extra_include_1(self):
         metadata = MetaData()
         tbl = Table(