]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add support for prefixes on CREATE INDEX statements in MySQL
authorJoseph Schorr <josephschorr@users.noreply.github.com>
Tue, 17 Jan 2017 15:02:17 +0000 (10:02 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 17 Jan 2017 15:21:47 +0000 (10:21 -0500)
Added a new parameter ``mysql_prefix`` supported by the :class:`.Index`
construct, allows specification of MySQL-specific prefixes such as
"FULLTEXT". Pull request courtesy Joseph Schorr.

Change-Id: I5a21fa466fdfd4d9e39e1fb4ecec1eab93b92c36
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/339

doc/build/changelog/changelog_11.rst
lib/sqlalchemy/dialects/mysql/base.py
test/dialect/mysql/test_compiler.py

index ead292570654037152fac41145923fe01dfab9a0..31df8720f226d7842f2e796b95a7bcca392f2e72 100644 (file)
 .. changelog::
     :version: 1.1.5
 
+    .. change:: mysql_index_prefix
+        :tags: feature, mysql
+
+        Added a new parameter ``mysql_prefix`` supported by the :class:`.Index`
+        construct, allows specification of MySQL-specific prefixes such as
+        "FULLTEXT". Pull request courtesy Joseph Schorr.
+
     .. change:: 3854
         :tags: bug, orm
         :tickets: 3854
index 1654ff3278610f9c7a880f5232567d9d8f713c75..8b0d00a63d39b4ddf22746f5e9d36ed10e25ea63 100644 (file)
@@ -391,6 +391,25 @@ BLOB.
 .. versionadded:: 0.8.2 ``mysql_length`` may now be specified as a dictionary
    for use with composite indexes.
 
+Index Prefixes
+~~~~~~~~~~~~~~
+
+MySQL storage engines permit you to specify an index prefix when creating
+an index. SQLAlchemy provides this feature via the
+``mysql_prefix`` parameter on :class:`.Index`::
+
+    Index('my_index', my_table.c.data, mysql_prefix='FULLTEXT')
+
+The value passed to the keyword argument will be simply passed through to the
+underlying CREATE INDEX, so it *must* be a valid index prefix for your MySQL
+storage engine.
+
+.. versionadded:: 1.1.5
+
+.. seealso::
+
+    `CREATE INDEX <http://dev.mysql.com/doc/refman/5.0/en/create-index.html>`_ - MySQL documentation
+
 Index Types
 ~~~~~~~~~~~~~
 
@@ -1039,6 +1058,11 @@ class MySQLDDLCompiler(compiler.DDLCompiler):
         text = "CREATE "
         if index.unique:
             text += "UNIQUE "
+
+        index_prefix = index.kwargs.get('mysql_prefix', None)
+        if index_prefix:
+          text += index_prefix + ' '
+
         text += "INDEX %s ON %s " % (name, table)
 
         length = index.dialect_options['mysql']['length']
@@ -1468,6 +1492,7 @@ class MySQLDialect(default.DefaultDialect):
         (sa_schema.Index, {
             "using": None,
             "length": None,
+            "prefix": None,
         })
     ]
 
index 88f9235e11ad9af54eee63c198f5826c97ac1cd3..b72507104fb92c3a8d63ed1a40751d480df358a4 100644 (file)
@@ -40,6 +40,14 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
         self.assert_compile(schema.CreateIndex(idx),
                             'CREATE INDEX test_idx1 ON testtbl (data)')
 
+    def test_create_index_with_prefix(self):
+        m = MetaData()
+        tbl = Table('testtbl', m, Column('data', String(255)))
+        idx = Index('test_idx1', tbl.c.data, mysql_length=10, mysql_prefix='FULLTEXT')
+
+        self.assert_compile(schema.CreateIndex(idx),
+                            'CREATE FULLTEXT INDEX test_idx1 ON testtbl (data(10))')
+
     def test_create_index_with_length(self):
         m = MetaData()
         tbl = Table('testtbl', m, Column('data', String(255)))