]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add support for declarative partitioning in PostgreSQL 10
authorVsevolod Solovyov <vsevolod.solovyov@gmail.com>
Thu, 15 Mar 2018 13:00:47 +0000 (09:00 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 30 Mar 2018 16:18:09 +0000 (12:18 -0400)
Added support for "PARTITION BY" in Postgresql table definitions,
using "postgresql_partition_by".  Pull request courtesy
Vsevolod Solovyov.

Change-Id: Id74d6882d7193fae1e5fd44b6e12d6852866fcc4
Pull-request: https://github.com/zzzeek/sqlalchemy/pull/430

doc/build/changelog/unreleased_12/pg_partition.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_compiler.py

diff --git a/doc/build/changelog/unreleased_12/pg_partition.rst b/doc/build/changelog/unreleased_12/pg_partition.rst
new file mode 100644 (file)
index 0000000..57493d3
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: feature, postgresql
+    :versions: 1.3.0b1
+
+    Added support for "PARTITION BY" in Postgresql table definitions,
+    using "postgresql_partition_by".  Pull request courtesy
+    Vsevolod Solovyov.
index 5ae27a9963fcc07db9b8f20d40019cfd364714c8..1dffe8db93336b992f2c65e26d623852dbc4f9c3 100644 (file)
@@ -780,7 +780,14 @@ dialect in conjunction with the :class:`.Table` construct:
 
     Table("some_table", metadata, ..., postgresql_inherits=("t1", "t2", ...))
 
-.. versionadded:: 1.0.0
+    .. versionadded:: 1.0.0
+
+* ``PARTITION BY``::
+
+    Table("some_table", metadata, ...,
+          postgresql_partition_by='LIST (part_column)')
+
+    .. versionadded:: 1.2.6
 
 .. seealso::
 
@@ -1829,6 +1836,9 @@ class PGDDLCompiler(compiler.DDLCompiler):
                 ', '.join(self.preparer.quote(name) for name in inherits) +
                 ' )')
 
+        if pg_opts['partition_by']:
+            table_opts.append('\n PARTITION BY %s' % pg_opts['partition_by'])
+
         if pg_opts['with_oids'] is True:
             table_opts.append('\n WITH OIDS')
         elif pg_opts['with_oids'] is False:
@@ -2166,6 +2176,7 @@ class PGDialect(default.DefaultDialect):
         (schema.Table, {
             "ignore_search_path": False,
             "tablespace": None,
+            "partition_by": None,
             "with_oids": None,
             "on_commit": None,
             "inherits": None
index db142a6576673cc65d1b2d86270ceebb39a6e7f0..66764fcc21f5c1b72c5f6269da67a34a8d166c93 100644 (file)
@@ -233,6 +233,26 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             'CREATE TABLE atable (id INTEGER) INHERITS '
             '( "Quote Me", "quote Me Too" )')
 
+    def test_create_table_partition_by_list(self):
+        m = MetaData()
+        tbl = Table(
+            'atable', m, Column("id", Integer), Column("part_column", Integer),
+            postgresql_partition_by='LIST (part_column)')
+        self.assert_compile(
+            schema.CreateTable(tbl),
+            'CREATE TABLE atable (id INTEGER, part_column INTEGER) '
+            'PARTITION BY LIST (part_column)')
+
+    def test_create_table_partition_by_range(self):
+        m = MetaData()
+        tbl = Table(
+            'atable', m, Column("id", Integer), Column("part_column", Integer),
+            postgresql_partition_by='RANGE (part_column)')
+        self.assert_compile(
+            schema.CreateTable(tbl),
+            'CREATE TABLE atable (id INTEGER, part_column INTEGER) '
+            'PARTITION BY RANGE (part_column)')
+
     def test_create_table_with_oids(self):
         m = MetaData()
         tbl = Table(