]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- pep8 formatting for pg table opts feature, tests
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 23 Aug 2014 19:21:16 +0000 (15:21 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 23 Aug 2014 19:21:16 +0000 (15:21 -0400)
- add support for PG INHERITS
- fix mis-named tests
- changelog
fixes #2051

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

index fb639ddf73e13894e799b8d035eeff2389feb228..4e75181dacb8bf61fef4199ad92574f77a4e373e 100644 (file)
 .. changelog::
        :version: 1.0.0
 
+    .. change::
+        :tags: feature, postgresql
+        :tickets: 2051
+
+        Added support for PG table options TABLESPACE, ON COMMIT,
+        WITH(OUT) OIDS, and INHERITS, when rendering DDL via
+        the :class:`.Table` construct.   Pull request courtesy
+        malikdiarra.
+
+        .. seealso::
+
+            :ref:`postgresql_table_options`
+
     .. change::
         :tags: bug, orm, py3k
 
index 34932520f47bd8c6e90ecc530e4d4add9fdbfe78..39de0cf924bfe3fb303aa71c401f2a73b78846cf 100644 (file)
@@ -417,22 +417,42 @@ of :class:`.PGInspector`, which offers additional methods::
 .. autoclass:: PGInspector
     :members:
 
-PostgreSQL specific table options
----------------------------------
+.. postgresql_table_options:
+
+PostgreSQL Table Options
+-------------------------
+
+Several options for CREATE TABLE are supported directly by the PostgreSQL
+dialect in conjunction with the :class:`.Table` construct:
+
+* ``TABLESPACE``::
+
+    Table("some_table", metadata, ..., postgresql_tablespace='some_tablespace')
+
+* ``ON COMMIT``::
+
+    Table("some_table", metadata, ..., postgresql_on_commit='PRESERVE ROWS')
 
-PostgreSQL provides several CREATE TABLE specific options allowing to 
-specify how table data are stored. The following options are currently 
-supported: ``TABLESPACE``, ``ON COMMIT``, ``WITH OIDS``.
+* ``WITH OIDS``::
 
-``postgresql_tablespace`` is probably the more common and allows to specify 
-where in the filesystem the data files for the table will be created (see
-http://www.postgresql.org/docs/9.3/static/manage-ag-tablespaces.html)
+    Table("some_table", metadata, ..., postgresql_with_oids=True)
+
+* ``WITHOUT OIDS``::
+
+    Table("some_table", metadata, ..., postgresql_with_oids=False)
+
+* ``INHERITS``::
+
+    Table("some_table", metadata, ..., postgresql_inherits="some_supertable")
+
+    Table("some_table", metadata, ..., postgresql_inherits=("t1", "t2", ...))
+
+.. versionadded:: 1.0.0
 
 .. seealso::
 
     `Postgresql CREATE TABLE options
-    <http://www.postgresql.org/docs/9.3/static/sql-createtable.html>`_ -
-    on the PostgreSQL website
+    <http://www.postgresql.org/docs/9.3/static/sql-createtable.html>`_
 
 """
 from collections import defaultdict
@@ -1466,19 +1486,33 @@ class PGDDLCompiler(compiler.DDLCompiler):
 
     def post_create_table(self, table):
         table_opts = []
-        if table.dialect_options['postgresql']['with_oids'] is not None:
-            if table.dialect_options['postgresql']['with_oids']:
-                table_opts.append('WITH OIDS')
-            else:
-                table_opts.append('WITHOUT OIDS')
-        if table.dialect_options['postgresql']['on_commit']:
-            on_commit_options = table.dialect_options['postgresql']['on_commit'].replace("_", " ").upper()
-            table_opts.append('ON COMMIT %s' % on_commit_options) 
-        if table.dialect_options['postgresql']['tablespace']:
-            tablespace_name = table.dialect_options['postgresql']['tablespace']
-            table_opts.append('TABLESPACE %s' % self.preparer.quote(tablespace_name))
+        pg_opts = table.dialect_options['postgresql']
+
+        inherits = pg_opts.get('inherits')
+        if inherits is not None:
+            if not isinstance(inherits, (list, tuple)):
+                inherits = (inherits, )
+            table_opts.append(
+                '\n INHERITS ( ' +
+                ', '.join(self.preparer.quote(name) for name in inherits) +
+                ' )')
+
+        if pg_opts['with_oids'] is True:
+            table_opts.append('\n WITH OIDS')
+        elif pg_opts['with_oids'] is False:
+            table_opts.append('\n WITHOUT OIDS')
+
+        if pg_opts['on_commit']:
+            on_commit_options = pg_opts['on_commit'].replace("_", " ").upper()
+            table_opts.append('\n ON COMMIT %s' % on_commit_options)
+
+        if pg_opts['tablespace']:
+            tablespace_name = pg_opts['tablespace']
+            table_opts.append(
+                '\n TABLESPACE %s' % self.preparer.quote(tablespace_name)
+            )
 
-        return ' '.join(table_opts)
+        return ''.join(table_opts)
 
 
 class PGTypeCompiler(compiler.GenericTypeCompiler):
@@ -1741,8 +1775,9 @@ class PGDialect(default.DefaultDialect):
         (schema.Table, {
             "ignore_search_path": False,
             "tablespace": None,
-            "with_oids" : None,
-            "on_commit" : None,
+            "with_oids": None,
+            "on_commit": None,
+            "inherits": None
         })
     ]
 
index 5d1fddb49f42cb070d855f5fb950bc8d4bad1123..6c4f3c8cccf270309f2c3086a53116845d183bf2 100644 (file)
@@ -168,37 +168,88 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
 
     def test_create_table_with_tablespace(self):
         m = MetaData()
-        tbl = Table('atable', m, Column("id", Integer), postgresql_tablespace = 'sometablespace')
-        self.assert_compile(schema.CreateTable(tbl),
-                "CREATE TABLE atable (id INTEGER)TABLESPACE sometablespace")
+        tbl = Table(
+            'atable', m, Column("id", Integer),
+            postgresql_tablespace='sometablespace')
+        self.assert_compile(
+            schema.CreateTable(tbl),
+            "CREATE TABLE atable (id INTEGER) TABLESPACE sometablespace")
 
+    def test_create_table_with_tablespace_quoted(self):
         # testing quoting of tablespace name
-        tbl = Table('anothertable', m, Column("id", Integer), postgresql_tablespace = 'table')
-        self.assert_compile(schema.CreateTable(tbl),
-                'CREATE TABLE anothertable (id INTEGER)TABLESPACE "table"')
+        m = MetaData()
+        tbl = Table(
+            'anothertable', m, Column("id", Integer),
+            postgresql_tablespace='table')
+        self.assert_compile(
+            schema.CreateTable(tbl),
+            'CREATE TABLE anothertable (id INTEGER) TABLESPACE "table"')
+
+    def test_create_table_inherits(self):
+        m = MetaData()
+        tbl = Table(
+            'atable', m, Column("id", Integer),
+            postgresql_inherits='i1')
+        self.assert_compile(
+            schema.CreateTable(tbl),
+            "CREATE TABLE atable (id INTEGER) INHERITS ( i1 )")
+
+    def test_create_table_inherits_tuple(self):
+        m = MetaData()
+        tbl = Table(
+            'atable', m, Column("id", Integer),
+            postgresql_inherits=('i1', 'i2'))
+        self.assert_compile(
+            schema.CreateTable(tbl),
+            "CREATE TABLE atable (id INTEGER) INHERITS ( i1, i2 )")
+
+    def test_create_table_inherits_quoting(self):
+        m = MetaData()
+        tbl = Table(
+            'atable', m, Column("id", Integer),
+            postgresql_inherits=('Quote Me', 'quote Me Too'))
+        self.assert_compile(
+            schema.CreateTable(tbl),
+            'CREATE TABLE atable (id INTEGER) INHERITS '
+            '( "Quote Me", "quote Me Too" )')
 
     def test_create_table_with_oids(self):
         m = MetaData()
-        tbl = Table('atable', m, Column("id", Integer), postgresql_with_oids = True, )
-        self.assert_compile(schema.CreateTable(tbl),
-                "CREATE TABLE atable (id INTEGER)WITH OIDS")
+        tbl = Table(
+            'atable', m, Column("id", Integer),
+            postgresql_with_oids=True, )
+        self.assert_compile(
+            schema.CreateTable(tbl),
+            "CREATE TABLE atable (id INTEGER) WITH OIDS")
 
-        tbl2 = Table('anothertable', m, Column("id", Integer), postgresql_with_oids = False, )
-        self.assert_compile(schema.CreateTable(tbl2),
-                "CREATE TABLE anothertable (id INTEGER)WITHOUT OIDS")
+        tbl2 = Table(
+            'anothertable', m, Column("id", Integer),
+            postgresql_with_oids=False)
+        self.assert_compile(
+            schema.CreateTable(tbl2),
+            "CREATE TABLE anothertable (id INTEGER) WITHOUT OIDS")
 
-    def create_table_with_oncommit_option(self):
+    def test_create_table_with_oncommit_option(self):
         m = MetaData()
-        tbl = Table('atable', m, Column("id", Integer), postgresql_on_commit = "drop")
-        self.assert_compile(schema.CreateTable(tbl),
-                "CREATE TABLE atable (id INTEGER) ON COMMIT DROP")
-    
-    def create_table_with_multiple_options(self):
+        tbl = Table(
+            'atable', m, Column("id", Integer),
+            postgresql_on_commit="drop")
+        self.assert_compile(
+            schema.CreateTable(tbl),
+            "CREATE TABLE atable (id INTEGER) ON COMMIT DROP")
+
+    def test_create_table_with_multiple_options(self):
         m = MetaData()
-        tbl = Table('atable', m, Column("id", Integer), postgresql_tablespace = 'sometablespace', postgresql_with_oids = False, postgresql_on_commit = "preserve_rows")
-        self.assert_compile(schema.CreateTable(tbl),
-                "CREATE TABLE atable (id INTEGER)WITHOUT OIDS ON COMMIT PRESERVE ROWS TABLESPACE sometablespace")
-    
+        tbl = Table(
+            'atable', m, Column("id", Integer),
+            postgresql_tablespace='sometablespace',
+            postgresql_with_oids=False,
+            postgresql_on_commit="preserve_rows")
+        self.assert_compile(
+            schema.CreateTable(tbl),
+            "CREATE TABLE atable (id INTEGER) WITHOUT OIDS "
+            "ON COMMIT PRESERVE ROWS TABLESPACE sometablespace")
+
     def test_create_partial_index(self):
         m = MetaData()
         tbl = Table('testtbl', m, Column('data', Integer))