From: Edgar Ramírez Mondragón Date: Mon, 22 Jan 2024 07:29:44 +0000 (-0500) Subject: Support specifying access method when creating Postgres tables X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=46899918a6dda07cca07e30af2526134f9c38809;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Support specifying access method when creating Postgres tables ### Description ### Checklist This pull request is: - [ ] A documentation / typographical / small typing error fix - Good to go, no issue or tests are needed - [ ] A short code fix - please include the issue number, and create an issue if none exists, which must include a complete example of the issue. one line code fixes without an issue and demonstration will not be accepted. - Please include: `Fixes: #` in the commit message - please include tests. one line code fixes without tests will not be accepted. - [x] A new feature implementation - please include the issue number, and create an issue if none exists, which must include a complete example of how the feature would look. - Please include: `Fixes: #` in the commit message - please include tests. Fixes #10904 **Have a nice day!** Closes: #10905 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/10905 Pull-request-sha: 85f232a303a5543725dac42206cb2395fc34109e Change-Id: I5e2fc05a696eb6da71bbd695f0466e8552d203b6 --- diff --git a/doc/build/changelog/unreleased_20/10904.rst b/doc/build/changelog/unreleased_20/10904.rst new file mode 100644 index 0000000000..3dc744dc18 --- /dev/null +++ b/doc/build/changelog/unreleased_20/10904.rst @@ -0,0 +1,11 @@ +.. change:: + :tags: usecase, postgresql + :tickets: 10904 + + Support the ``USING `` option for PostgreSQL ``CREATE TABLE`` to + specify the access method to use to store the contents for the new table. + Pull request courtesy Edgar Ramírez-Mondragón. + + .. seealso:: + + :ref:`postgresql_table_options` diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index c39e8be75c..ef70000c1b 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1112,36 +1112,42 @@ PostgreSQL Table Options Several options for CREATE TABLE are supported directly by the PostgreSQL dialect in conjunction with the :class:`_schema.Table` construct: -* ``TABLESPACE``:: +* ``INHERITS``:: - Table("some_table", metadata, ..., postgresql_tablespace='some_tablespace') + Table("some_table", metadata, ..., postgresql_inherits="some_supertable") - The above option is also available on the :class:`.Index` construct. + Table("some_table", metadata, ..., postgresql_inherits=("t1", "t2", ...)) * ``ON COMMIT``:: Table("some_table", metadata, ..., postgresql_on_commit='PRESERVE ROWS') -* ``WITH OIDS``:: +* ``PARTITION BY``:: - Table("some_table", metadata, ..., postgresql_with_oids=True) + Table("some_table", metadata, ..., + postgresql_partition_by='LIST (part_column)') -* ``WITHOUT OIDS``:: + .. versionadded:: 1.2.6 - Table("some_table", metadata, ..., postgresql_with_oids=False) +* ``TABLESPACE``:: -* ``INHERITS``:: + Table("some_table", metadata, ..., postgresql_tablespace='some_tablespace') - Table("some_table", metadata, ..., postgresql_inherits="some_supertable") + The above option is also available on the :class:`.Index` construct. - Table("some_table", metadata, ..., postgresql_inherits=("t1", "t2", ...)) +* ``USING``:: -* ``PARTITION BY``:: + Table("some_table", metadata, ..., postgresql_using='heap') - Table("some_table", metadata, ..., - postgresql_partition_by='LIST (part_column)') + .. versionadded:: 2.0.26 - .. versionadded:: 1.2.6 +* ``WITH OIDS``:: + + Table("some_table", metadata, ..., postgresql_with_oids=True) + +* ``WITHOUT OIDS``:: + + Table("some_table", metadata, ..., postgresql_with_oids=False) .. seealso:: @@ -2395,6 +2401,9 @@ class PGDDLCompiler(compiler.DDLCompiler): if pg_opts["partition_by"]: table_opts.append("\n PARTITION BY %s" % pg_opts["partition_by"]) + if pg_opts["using"]: + table_opts.append("\n USING %s" % pg_opts["using"]) + if pg_opts["with_oids"] is True: table_opts.append("\n WITH OIDS") elif pg_opts["with_oids"] is False: @@ -3006,6 +3015,7 @@ class PGDialect(default.DefaultDialect): "with_oids": None, "on_commit": None, "inherits": None, + "using": None, }, ), ( diff --git a/test/dialect/postgresql/test_compiler.py b/test/dialect/postgresql/test_compiler.py index 5851a86e6d..f890b7ba9c 100644 --- a/test/dialect/postgresql/test_compiler.py +++ b/test/dialect/postgresql/test_compiler.py @@ -582,6 +582,19 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): "CREATE TABLE atable (id INTEGER) ON COMMIT DROP", ) + def test_create_table_with_using_option(self): + m = MetaData() + tbl = Table( + "atable", + m, + Column("id", Integer), + postgresql_using="heap", + ) + self.assert_compile( + schema.CreateTable(tbl), + "CREATE TABLE atable (id INTEGER) USING heap", + ) + def test_create_table_with_multiple_options(self): m = MetaData() tbl = Table( @@ -591,10 +604,11 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): postgresql_tablespace="sometablespace", postgresql_with_oids=False, postgresql_on_commit="preserve_rows", + postgresql_using="heap", ) self.assert_compile( schema.CreateTable(tbl), - "CREATE TABLE atable (id INTEGER) WITHOUT OIDS " + "CREATE TABLE atable (id INTEGER) USING heap WITHOUT OIDS " "ON COMMIT PRESERVE ROWS TABLESPACE sometablespace", )