]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- for #3455
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 19 Jun 2015 18:10:47 +0000 (14:10 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 19 Jun 2015 18:10:47 +0000 (14:10 -0400)
- changelog
- versionadded + reflink for new pg storage parameters doc
- pep8ing
- add additional tests to definitely check that the Index object
is created all the way with the opts we want
fixes #3455

doc/build/changelog/changelog_10.rst
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_compiler.py
test/dialect/postgresql/test_reflection.py

index a02a13ef9078cd5d4c547297ec32f241057c1f89..efb5187163bbd5fe9fa530c2979feb77c8373048 100644 (file)
 .. changelog::
     :version: 1.0.6
 
+    .. change::
+        :tags: feature, postgresql
+        :tickets: 3455
+        :pullreq: github:179
+
+        Added support for storage parameters under CREATE INDEX, using
+        a new keyword argument ``postgresql_with``.  Also added support for
+        reflection to support both the ``postgresql_with`` flag as well
+        as the ``postgresql_using`` flag, which will now be set on
+        :class:`.Index` objects that are reflected, as well present
+        in a new "dialect_options" dictionary in the result of
+        :meth:`.Inspector.get_indexes`.  Pull request courtesy Pete Hollobon.
+
+        .. seealso::
+
+            :ref:`postgresql_index_storage`
+
     .. change::
         :tags: bug, orm
         :tickets: 3462
index b46c6533528d7a09cf2f7e36233500360948f881..22c66dbbb402957eeb7cae6103a418dca0920e5f 100644 (file)
@@ -401,6 +401,8 @@ 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.
 
+.. _postgresql_index_storage:
+
 Index Storage Parameters
 ^^^^^^^^^^^^^^^^^^^^^^^^
 
@@ -411,6 +413,8 @@ keyword argument::
 
     Index('my_index', my_table.c.data, postgresql_with={"fillfactor": 50})
 
+.. versionadded:: 1.0.6
+
 .. _postgresql_index_concurrently:
 
 Indexes with CONCURRENTLY
@@ -2690,7 +2694,8 @@ class PGDialect(default.DefaultDialect):
 
         sv_idx_name = None
         for row in c.fetchall():
-            idx_name, unique, expr, prd, col, col_num, conrelid, idx_key, options, amname = row
+            (idx_name, unique, expr, prd, col,
+             col_num, conrelid, idx_key, options, amname) = row
 
             if expr:
                 if idx_name != sv_idx_name:
@@ -2717,7 +2722,13 @@ class PGDialect(default.DefaultDialect):
                 if conrelid is not None:
                     index['duplicates_constraint'] = idx_name
                 if options:
-                    index['options'] = dict([option.split("=") for option in options])
+                    index['options'] = dict(
+                        [option.split("=") for option in options])
+
+                # it *might* be nice to include that this is 'btree' in the
+                # reflection info.  But we don't want an Index object
+                # to have a ``postgresql_using`` in it that is just the
+                # default, so for the moment leaving this out.
                 if amname and amname != 'btree':
                     index['amname'] = amname
 
@@ -2731,9 +2742,11 @@ class PGDialect(default.DefaultDialect):
             if 'duplicates_constraint' in idx:
                 entry['duplicates_constraint'] = idx['duplicates_constraint']
             if 'options' in idx:
-                entry.setdefault('dialect_options', {})["postgresql_with"] = idx['options']
+                entry.setdefault(
+                    'dialect_options', {})["postgresql_with"] = idx['options']
             if 'amname' in idx:
-                entry.setdefault('dialect_options', {})["postgresql_using"] = idx['amname']
+                entry.setdefault(
+                    'dialect_options', {})["postgresql_using"] = idx['amname']
             result.append(entry)
         return result
 
index 7311416042b1d4a3f98b8f1892589c0de44b2537..9fa5c98043ed16badbd962cfe6147514a110f12f 100644 (file)
@@ -375,24 +375,22 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
         tbl = Table('testtbl', m, Column('data', String))
 
         idx1 = Index('test_idx1', tbl.c.data)
-        idx2 = Index('test_idx2', tbl.c.data, postgresql_with={"fillfactor": 50})
+        idx2 = Index(
+            'test_idx2', tbl.c.data, postgresql_with={"fillfactor": 50})
         idx3 = Index('test_idx3', tbl.c.data, postgresql_using="gist",
                      postgresql_with={"buffering": "off"})
 
         self.assert_compile(schema.CreateIndex(idx1),
                             'CREATE INDEX test_idx1 ON testtbl '
-                            '(data)',
-                            dialect=postgresql.dialect())
+                            '(data)')
         self.assert_compile(schema.CreateIndex(idx2),
                             'CREATE INDEX test_idx2 ON testtbl '
                             '(data) '
-                            'WITH (fillfactor = 50)',
-                            dialect=postgresql.dialect())
+                            'WITH (fillfactor = 50)')
         self.assert_compile(schema.CreateIndex(idx3),
                             'CREATE INDEX test_idx3 ON testtbl '
                             'USING gist (data) '
-                            'WITH (buffering = off)',
-                            dialect=postgresql.dialect())
+                            'WITH (buffering = off)')
 
     def test_create_index_expr_gets_parens(self):
         m = MetaData()
index ed8a88fd446027b097ce09d633160d7e2c0b9bc1..e72ddadad1ed448e5237eb593427250596a3d4ef 100644 (file)
@@ -679,18 +679,27 @@ class ReflectionTest(fixtures.TestBase):
 
         metadata = self.metadata
 
-        t1 = Table('t', metadata,
-                   Column('id', Integer, primary_key=True),
-                   Column('x', Integer)
-                   )
+        Table(
+            't', metadata,
+            Column('id', Integer, primary_key=True),
+            Column('x', Integer)
+        )
         metadata.create_all()
-        conn = testing.db.connect().execution_options(autocommit=True)
-        conn.execute("CREATE INDEX idx1 ON t (x) WITH (fillfactor = 50)")
 
-        ind = testing.db.dialect.get_indexes(conn, "t", None)
-        eq_(ind, [{'unique': False, 'column_names': ['x'], 'name': 'idx1',
-                   'dialect_options': {"postgresql_with": {"fillfactor": "50"}}}])
-        conn.close()
+        with testing.db.connect().execution_options(autocommit=True) as conn:
+            conn.execute("CREATE INDEX idx1 ON t (x) WITH (fillfactor = 50)")
+
+            ind = testing.db.dialect.get_indexes(conn, "t", None)
+            eq_(ind, [{'unique': False, 'column_names': ['x'], 'name': 'idx1',
+                       'dialect_options':
+                       {"postgresql_with": {"fillfactor": "50"}}}])
+
+            m = MetaData()
+            t1 = Table('t', m, autoload_with=conn)
+            eq_(
+                list(t1.indexes)[0].dialect_options['postgresql']['with'],
+                {"fillfactor": "50"}
+            )
 
     @testing.provide_metadata
     def test_index_reflection_with_access_method(self):
@@ -698,18 +707,24 @@ class ReflectionTest(fixtures.TestBase):
 
         metadata = self.metadata
 
-        t1 = Table('t', metadata,
-                   Column('id', Integer, primary_key=True),
-                   Column('x', ARRAY(Integer))
-                   )
+        Table(
+            't', metadata,
+            Column('id', Integer, primary_key=True),
+            Column('x', ARRAY(Integer))
+        )
         metadata.create_all()
-        conn = testing.db.connect().execution_options(autocommit=True)
-        conn.execute("CREATE INDEX idx1 ON t USING gin (x)")
-
-        ind = testing.db.dialect.get_indexes(conn, "t", None)
-        eq_(ind, [{'unique': False, 'column_names': ['x'], 'name': 'idx1',
-                   'dialect_options': {'postgresql_using': 'gin'}}])
-        conn.close()
+        with testing.db.connect().execution_options(autocommit=True) as conn:
+            conn.execute("CREATE INDEX idx1 ON t USING gin (x)")
+
+            ind = testing.db.dialect.get_indexes(conn, "t", None)
+            eq_(ind, [{'unique': False, 'column_names': ['x'], 'name': 'idx1',
+                       'dialect_options': {'postgresql_using': 'gin'}}])
+            m = MetaData()
+            t1 = Table('t', m, autoload_with=conn)
+            eq_(
+                list(t1.indexes)[0].dialect_options['postgresql']['using'],
+                'gin'
+            )
 
     @testing.provide_metadata
     def test_foreign_key_option_inspection(self):