From: beenje Date: Tue, 12 Apr 2016 03:15:35 +0000 (-0400) Subject: Add postgresql_tablespace option on Index X-Git-Tag: rel_1_1_0b1~32^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ef0da7eb66d173a487adbee96311bf4996da0556;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Add postgresql_tablespace option on Index This complements the same-named parameter available on Table. Fixes: #3720 Change-Id: I56e081e2a551f37c3f392ca4b301c9ef82b94e59 Pull-request: https://github.com/zzzeek/sqlalchemy/pull/233 --- diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index f86a0f1dc3..898b8a0ba3 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -21,6 +21,15 @@ .. 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 diff --git a/doc/build/changelog/migration_11.rst b/doc/build/changelog/migration_11.rst index b32183a6e2..0d94f35e99 100644 --- a/doc/build/changelog/migration_11.rst +++ b/doc/build/changelog/migration_11.rst @@ -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 -------------------- diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index fe3d294506..136cb1b28c 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -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, diff --git a/test/dialect/postgresql/test_compiler.py b/test/dialect/postgresql/test_compiler.py index 87e48d3f29..c20e48b01d 100644 --- a/test/dialect/postgresql/test_compiler.py +++ b/test/dialect/postgresql/test_compiler.py @@ -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))