summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormike bayer <mike_mp@zzzcomputing.com>2016-06-02 13:57:24 -0400
committerGerrit Code Review <gerrit2@ln3.zzzcomputing.com>2016-06-02 13:57:24 -0400
commit31699bd1866bbfc36f1501e5e1b54d3c06cf3b4c (patch)
tree0b1780fbb46974955f48ce30411fe1b334889204
parent7a4aa46ae49f3ddcdadd4f3f10ef9cbce1fbf9aa (diff)
parentef0da7eb66d173a487adbee96311bf4996da0556 (diff)
downloadsqlalchemy-31699bd1866bbfc36f1501e5e1b54d3c06cf3b4c.tar.gz
Merge "Add postgresql_tablespace option on Index"
-rw-r--r--doc/build/changelog/changelog_11.rst9
-rw-r--r--doc/build/changelog/migration_11.rst13
-rw-r--r--lib/sqlalchemy/dialects/postgresql/base.py20
-rw-r--r--test/dialect/postgresql/test_compiler.py43
4 files changed, 84 insertions, 1 deletions
diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst
index f86a0f1dc..898b8a0ba 100644
--- a/doc/build/changelog/changelog_11.rst
+++ b/doc/build/changelog/changelog_11.rst
@@ -22,6 +22,15 @@
: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 b32183a6e..0d94f35e9 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 fe3d29450..136cb1b28 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 87e48d3f2..c20e48b01 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))