]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add postgresql_tablespace option on Index
authorbeenje <beenje@gmail.org>
Tue, 12 Apr 2016 03:15:35 +0000 (23:15 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 1 Jun 2016 21:20:41 +0000 (17:20 -0400)
This complements the same-named parameter
available on Table.

Fixes: #3720
Change-Id: I56e081e2a551f37c3f392ca4b301c9ef82b94e59
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/233

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

index f86a0f1dc3b96fb71971cd1aec3ba2c834a83da4..898b8a0ba32e70d87ad6e93a0ce2b6afc3ef13bb 100644 (file)
 .. changelog::
     :version: 1.1.0b1
 
+    .. change::
+        :tags: feature, postgresql
+        :tickets: 3720
+
+        Added ``postgresql_tablespace`` as an argument to :class:`.Index`
+        to allow specification of TABLESPACE for an index in Postgresql.
+        Complements the same-named parameter on :class:`.Table`.  Pull
+        request courtesy Benjamin Bertrand.
+
     .. change::
         :tags: bug, mssql
         :pullreq: bitbucket:58
index b32183a6e26f59b6a44de3451da4388849d2d0c1..0d94f35e99f8c8de064ebb8a3d05818ed78f63db 100644 (file)
@@ -2041,6 +2041,19 @@ both within the method :meth:`.Inspector.get_check_constraints` as well
 as within :class:`.Table` reflection within the :attr:`.Table.constraints`
 collection.
 
+Added tablespace option to Index
+--------------------------------
+
+The :class:`.Index` object now accepts the argument ``postgresql_tablespace``
+in order to specify TABLESPACE, the same way as accepted by the
+:class:`.Table` object.
+
+.. seealso::
+
+    :ref:`postgresql_index_storage`
+
+:ticket:`3720`
+
 Support for PyGreSQL
 --------------------
 
index fe3d29450669983e10fd0862206380dc0e1657e4..136cb1b28ca126b52c60bb89c86c9eb416dd7141 100644 (file)
@@ -414,6 +414,16 @@ keyword argument::
 
 .. versionadded:: 1.0.6
 
+PostgreSQL allows to define the tablespace in which to create the index.
+The tablespace can be specified on :class:`.Index` using the
+``postgresql_tablespace`` keyword argument::
+
+    Index('my_index', my_table.c.data, postgresql_tablespace='my_tablespace')
+
+.. versionadded:: 1.1
+
+Note that the same option is available on :class:`.Table` as well.
+
 .. _postgresql_index_concurrently:
 
 Indexes with CONCURRENTLY
@@ -482,6 +492,8 @@ dialect in conjunction with the :class:`.Table` construct:
 
     Table("some_table", metadata, ..., postgresql_tablespace='some_tablespace')
 
+  The above option is also available on the :class:`.Index` construct.
+
 * ``ON COMMIT``::
 
     Table("some_table", metadata, ..., postgresql_on_commit='PRESERVE ROWS')
@@ -1294,6 +1306,11 @@ class PGDDLCompiler(compiler.DDLCompiler):
                 ['%s = %s' % storage_parameter
                  for storage_parameter in withclause.items()]))
 
+        tablespace_name = index.dialect_options['postgresql']['tablespace']
+
+        if tablespace_name:
+            text += " TABLESPACE %s" % preparer.quote(tablespace_name)
+
         whereclause = index.dialect_options["postgresql"]["where"]
 
         if whereclause is not None:
@@ -1631,7 +1648,8 @@ class PGDialect(default.DefaultDialect):
             "where": None,
             "ops": {},
             "concurrently": False,
-            "with": {}
+            "with": {},
+            "tablespace": None
         }),
         (schema.Table, {
             "ignore_search_path": False,
index 87e48d3f2997eaaa09e505243b53351baeb496df..c20e48b01d52e17b9ad93abae64ac1f7980d577a 100644 (file)
@@ -412,6 +412,49 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
                             'USING gist (data) '
                             'WITH (buffering = off)')
 
+    def test_create_index_with_tablespace(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_tablespace='sometablespace')
+        idx3 = Index('test_idx3', tbl.c.data, postgresql_tablespace='another table space')
+
+        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 '
+                            '(data) '
+                            'TABLESPACE sometablespace',
+                            dialect=postgresql.dialect())
+        self.assert_compile(schema.CreateIndex(idx3),
+                            'CREATE INDEX test_idx3 ON testtbl '
+                            '(data) '
+                            'TABLESPACE "another table space"',
+                            dialect=postgresql.dialect())
+
+    def test_create_index_with_multiple_options(self):
+        m = MetaData()
+        tbl = Table('testtbl', m, Column('data', String))
+
+        idx1 = Index(
+                'test_idx1',
+                tbl.c.data,
+                postgresql_using='btree',
+                postgresql_tablespace='atablespace',
+                postgresql_with={"fillfactor": 60},
+                postgresql_where=and_(tbl.c.data > 5, tbl.c.data < 10))
+
+        self.assert_compile(schema.CreateIndex(idx1),
+                            'CREATE INDEX test_idx1 ON testtbl '
+                            'USING btree (data) '
+                            'WITH (fillfactor = 60) '
+                            'TABLESPACE atablespace '
+                            'WHERE data > 5 AND data < 10',
+                            dialect=postgresql.dialect())
+
     def test_create_index_expr_gets_parens(self):
         m = MetaData()
         tbl = Table('testtbl', m, Column('x', Integer), Column('y', Integer))