Add support for filtered indexes for mssql dialect

This commit is contained in:
mollardthomas
2019-05-03 14:43:12 +02:00
parent cdd01a06f7
commit cf609c19bc
2 changed files with 28 additions and 1 deletions
+19 -1
View File
@@ -540,6 +540,16 @@ names::
would render the index as ``CREATE INDEX my_index ON table (x) INCLUDE (y)``
Filtered Indexes
^^^^^^^^^^^^^^^^
The ``mssql_where`` option renders WHERE(condition) for the given string
names::
Index("my_index", table.c.x, mssql_where=table.c.x > 10)
would render the index as ``CREATE INDEX my_index ON table (x) WHERE x > 10``
Index ordering
^^^^^^^^^^^^^^
@@ -1950,6 +1960,14 @@ 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 = [
@@ -2182,7 +2200,7 @@ class MSDialect(default.DefaultDialect):
construct_arguments = [
(sa_schema.PrimaryKeyConstraint, {"clustered": None}),
(sa_schema.UniqueConstraint, {"clustered": None}),
(sa_schema.Index, {"clustered": None, "include": None}),
(sa_schema.Index, {"clustered": None, "include": None, "where": None}),
(sa_schema.Column, {"identity_start": 1, "identity_increment": 1}),
]
+9
View File
@@ -1129,6 +1129,15 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
schema.CreateIndex(idx), "CREATE CLUSTERED INDEX foo ON test (id)"
)
def test_index_where(self):
metadata = MetaData()
tbl = Table("test", metadata, Column("data", Integer))
idx = Index("test_idx_data_1", tbl.c.data, mssql_where=tbl.c.data > 1)
self.assert_compile(
schema.CreateIndex(idx),
"CREATE INDEX test_idx_data_1 ON test (data) WHERE data > 1"
)
def test_index_ordering(self):
metadata = MetaData()
tbl = Table(