]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Support specifying access method when creating Postgres tables
authorEdgar Ramírez Mondragón <edgarrm358@gmail.com>
Mon, 22 Jan 2024 07:29:44 +0000 (02:29 -0500)
committersqla-tester <sqla-tester@sqlalchemy.org>
Mon, 22 Jan 2024 07:29:44 +0000 (02:29 -0500)
<!-- Provide a general summary of your proposed changes in the Title field above -->

### Description
<!-- Describe your changes in detail -->

### Checklist
<!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once)

-->

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: #<issue number>` 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: #<issue number>` 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

doc/build/changelog/unreleased_20/10904.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_20/10904.rst b/doc/build/changelog/unreleased_20/10904.rst
new file mode 100644 (file)
index 0000000..3dc744d
--- /dev/null
@@ -0,0 +1,11 @@
+.. change::
+    :tags: usecase, postgresql
+    :tickets: 10904
+
+    Support the ``USING <method>`` 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`
index c39e8be75cfd9a5975e49bc535f4100880992fe7..ef70000c1bc00302e931b8f0e85a6e3e98eb40ac 100644 (file)
@@ -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,
             },
         ),
         (
index 5851a86e6d647fd5eb02399d0da03bc4ff8308a6..f890b7ba9ceefa8c1c5dfa3dfa2f707840f5bc15 100644 (file)
@@ -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",
         )