]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Partial index support with sqlite dialects.
authorKai Groner <kai@gronr.com>
Mon, 26 Jan 2015 19:49:40 +0000 (14:49 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 10 Mar 2015 18:19:18 +0000 (14:19 -0400)
From https://www.sqlite.org/partialindex.html
> Partial indexes have been supported in SQLite since version 3.8.0.

Reflection does not expose the predicate of partial indexes.  The
postgresql dialect does detect such indexes and issue a warning.  I
looked into matching this level of support, but the sqlite pragma
index_info does not expose the predicate.  Getting this data would
probably require parsing the CREATE INDEX statement from sqlite_master.

(cherry picked from commit 30ce7e93f4068b00ced0db785fdd578dc8a45975)

lib/sqlalchemy/dialects/sqlite/base.py

index a30de3a978a0b25abd504b166384227a62ed0ed4..9881eea64bcb55f28141d58fa9f9431760e4e036 100644 (file)
@@ -664,9 +664,20 @@ class SQLiteDDLCompiler(compiler.DDLCompiler):
         return preparer.format_table(table, use_schema=False)
 
     def visit_create_index(self, create):
-        return super(SQLiteDDLCompiler, self).visit_create_index(
+        index = create.element
+
+        text = super(SQLiteDDLCompiler, self).visit_create_index(
             create, include_table_schema=False)
 
+        whereclause = index.dialect_options["sqlite"]["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
+
 
 class SQLiteTypeCompiler(compiler.GenericTypeCompiler):
     def visit_large_binary(self, type_):
@@ -752,7 +763,10 @@ class SQLiteDialect(default.DefaultDialect):
     construct_arguments = [
         (sa_schema.Table, {
             "autoincrement": False
-        })
+        }),
+        (sa_schema.Index, {
+            "where": None,
+        }),
     ]
 
     _broken_fk_pragma_quotes = False