]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Added "postgresql_using" argument to Index(), produces
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 2 Oct 2011 21:17:46 +0000 (17:17 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 2 Oct 2011 21:17:46 +0000 (17:17 -0400)
    USING clause to specify index implementation for
    PG.  [ticket:2290].  Thanks to Ryan P. Kelly for
    the patch.

CHANGES
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/test_postgresql.py

diff --git a/CHANGES b/CHANGES
index 16333d494b9e94df887cce7b33f988b00d0df58e..f4f846ff9ff72c37f9e20dac77b3f84364462012 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -214,6 +214,11 @@ CHANGES
     extensions are in use or not.
 
 - postgresql
+  - Added "postgresql_using" argument to Index(), produces
+    USING clause to specify index implementation for
+    PG.  [ticket:2290].  Thanks to Ryan P. Kelly for
+    the patch.
+
   - Reflection functions for Table, Sequence no longer
     case insensitive.  Names can be differ only in case
     and will be correctly distinguished.  [ticket:2256]
index 8c3babd313a7794cc6c69782481f7996365c710d..b3be7bc998bc18bf5b001972df9666d69b7b6a4e 100644 (file)
@@ -141,6 +141,20 @@ the :class:`.Column`, i.e. the name used to access it from the ``.c`` collection
 of :class:`.Table`, which can be configured to be different than the actual
 name of the column as expressed in the database.
 
+Index Types
+^^^^^^^^^^^^
+
+PostgreSQL provides several index types: B-Tree, Hash, GiST, and GIN, as well as
+the ability for users to create their own (see
+http://www.postgresql.org/docs/8.3/static/indexes-types.html). These can be
+specified on :class:`.Index` using the ``postgresql_using`` keyword argument::
+
+    Index('my_index', my_table.c.data, postgresql_using='gin')
+
+The value passed to the keyword argument will be simply passed through to the
+underlying CREATE INDEX command, so it *must* be a valid index type for your
+version of PostgreSQL.
+
 """
 
 import re
@@ -629,11 +643,18 @@ class PGDDLCompiler(compiler.DDLCompiler):
         if index.unique:
             text += "UNIQUE "
         ops = index.kwargs.get('postgresql_ops', {})
-        text += "INDEX %s ON %s (%s)" \
-                % (
+        text += "INDEX %s ON %s " % (
                     preparer.quote(
                         self._index_identifier(index.name), index.quote),
-                    preparer.format_table(index.table),
+                    preparer.format_table(index.table)
+                )
+
+        if 'postgresql_using' in index.kwargs:
+            using = index.kwargs['postgresql_using']
+            text += "USING %s " % preparer.quote(using, index.quote)
+
+        text += "(%s)" \
+                % (
                     ', '.join([
                         preparer.format_column(c) + 
                         (c.key in ops and (' ' + ops[c.key]) or '')
index adf292999da8f1adc60ac8fafc0743e6d9d211c8..8f90becd59541a0bd6ab09a4f8dba577b1c738af 100644 (file)
@@ -174,6 +174,27 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
                             '(data text_pattern_ops, data2 int4_ops)',
                             dialect=postgresql.dialect())
 
+    def test_create_index_with_using(self):
+        m = MetaData()
+        tbl = Table('testtbl', m, Column('data', String))
+
+        idx1 = Index('test_idx1', tbl.c.data)
+        idx2 = Index('test_idx2', tbl.c.data, postgresql_using='btree')
+        idx3 = Index('test_idx3', tbl.c.data, postgresql_using='hash')
+
+        self.assert_compile(schema.CreateIndex(idx1),
+                            'CREATE INDEX test_idx1 ON testtbl '
+                            '(data)',
+                            dialect=postgresql.dialect())
+        self.assert_compile(schema.CreateIndex(idx2),
+                            'CREATE INDEX test_idx2 ON testtbl '
+                            'USING btree (data)',
+                            dialect=postgresql.dialect())
+        self.assert_compile(schema.CreateIndex(idx3),
+                            'CREATE INDEX test_idx3 ON testtbl '
+                            'USING hash (data)',
+                            dialect=postgresql.dialect())
+
     @testing.uses_deprecated(r".*'postgres_where' argument has been "
                              "renamed.*")
     def test_old_create_partial_index(self):