]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fixed compile for mssql dialect
authorGord Thompson <gord@gordthompson.com>
Tue, 8 Dec 2020 20:23:02 +0000 (15:23 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 9 Dec 2020 14:37:01 +0000 (09:37 -0500)
Fixed string compilation when both mssql_include and mssql_where are used

Fixes: #5751
Closes: #5752
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5752
Pull-request-sha: aa57ad5d93cd69bf7728d864569c31c7e59b54fb

Change-Id: I1201170affd9911c252df5c9b841e538bb577085
(cherry picked from commit 6f8b035c7808fea13bae50995206c9b2ded91f51)

doc/build/changelog/unreleased_13/5751.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_13/5751.rst b/doc/build/changelog/unreleased_13/5751.rst
new file mode 100644 (file)
index 0000000..44306ce
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: bug, mssql
+    :tickets: 5751
+
+    Fixed bug where a CREATE INDEX statement was rendered incorrectly when
+    both ``mssql-include`` and ``mssql_where`` were specified. Pull request
+    courtesy @Adiorz.
index 6869e504ad5efb81c870c31218a1fbe74e9c494a..cb5d677a184a46be5bab9b2464e5ba21dfd565df 100644 (file)
@@ -2043,14 +2043,6 @@ class MSDDLCompiler(compiler.DDLCompiler):
             ),
         )
 
-        whereclause = index.dialect_options["mssql"]["where"]
-
-        if whereclause is not None:
-            where_compiled = self.sql_compiler.process(
-                whereclause, include_table=False, literal_binds=True
-            )
-            text += " WHERE " + where_compiled
-
         # handle other included columns
         if index.dialect_options["mssql"]["include"]:
             inclusions = [
@@ -2064,6 +2056,14 @@ class MSDDLCompiler(compiler.DDLCompiler):
                 [preparer.quote(c.name) for c in inclusions]
             )
 
+        whereclause = index.dialect_options["mssql"]["where"]
+
+        if whereclause is not None:
+            where_compiled = self.sql_compiler.process(
+                whereclause, include_table=False, literal_binds=True
+            )
+            text += " WHERE " + where_compiled
+
         return text
 
     def visit_drop_index(self, drop):
index 34fd20076db1e703cb7a9c8184c40ae08f978342..e0d606c771cec024b9405a1394e6ccec6870a02e 100644 (file)
@@ -18,6 +18,7 @@ from sqlalchemy import sql
 from sqlalchemy import String
 from sqlalchemy import Table
 from sqlalchemy import testing
+from sqlalchemy import text
 from sqlalchemy import union
 from sqlalchemy import UniqueConstraint
 from sqlalchemy import update
@@ -1255,6 +1256,31 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             schema.CreateIndex(idx), "CREATE INDEX foo ON test (x) INCLUDE (y)"
         )
 
+    def test_index_include_where(self):
+        metadata = MetaData()
+        tbl = Table(
+            "test",
+            metadata,
+            Column("x", Integer),
+            Column("y", Integer),
+            Column("z", Integer),
+        )
+        idx = Index(
+            "foo", tbl.c.x, mssql_include=[tbl.c.y], mssql_where=tbl.c.y > 1
+        )
+        self.assert_compile(
+            schema.CreateIndex(idx),
+            "CREATE INDEX foo ON test (x) INCLUDE (y) WHERE y > 1",
+        )
+
+        idx = Index(
+            "foo", tbl.c.x, mssql_include=[tbl.c.y], mssql_where=text("y > 1")
+        )
+        self.assert_compile(
+            schema.CreateIndex(idx),
+            "CREATE INDEX foo ON test (x) INCLUDE (y) WHERE y > 1",
+        )
+
     def test_try_cast(self):
         metadata = MetaData()
         t1 = Table("t1", metadata, Column("id", Integer, primary_key=True))