From c2ab89afa01da639006e3875ff8357ebb4659346 Mon Sep 17 00:00:00 2001 From: Miguel Grillo Date: Mon, 21 Oct 2024 00:13:58 +0200 Subject: [PATCH] Pull Request: Add Support for `TABLESPACE` Specification in Table Definitions for Oracle **Description** This PR adds support for specifying the `TABLESPACE` in table definitions in SQLAlchemy, specifically for Oracle. This feature is particularly useful for Oracle users who need to specify the tablespace where the table data will be stored. **Changes Made** 1. Updated `construct_arguments` in `OracleDialect`: - The `construct_arguments` list in the `OracleDialect` class has been updated to include the `tablespace` argument for the `Table` class. ```Python construct_arguments = [ ( sa_schema.Table, # old {"resolve_synonyms": False, "on_commit": None, "compress": False}, # new {"resolve_synonyms": False, "on_commit": None, "compress": False, "tablespace": None}, ), (sa_schema.Index, {"bitmap": False, "compress": False}), ] ``` **Path**: `lib/sqlalchemy/dialects/oracle/base.py` 2. Modified OracleDDLCompiler to Include TABLESPACE in post_create_table: - The OracleDDLCompiler class has been modified to include the TABLESPACE clause at the end of the CREATE TABLE statement if the tablespace option is provided. ```Python if opts["tablespace"]: tablespace_name = opts["tablespace"] table_opts.append( "\n TABLESPACE %s" % self.preparer.quote(tablespace_name) ) ``` **Path**: `lib/sqlalchemy/dialects/oracle/base.py` 3. Added tablespace Argument to the Table Class: - A new tablespace argument has been added to the Table class to allow specifying the tablespace in the table definition. 4. Documentation Update: - The documentation has been updated to reflect the new feature and provide usage examples. **Usage Example** ```Python from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData engine = create_engine('oracle+cx_oracle://user:password@dsn') metadata = MetaData() users = Table('users', metadata, Column('id', Integer, primary_key=True), Column('name', String), Column('email', String, unique=True), oracle_tablespace='my_tablespace' # New tablespace argument optional ) metadata.create_all(engine) ``` --- lib/sqlalchemy/dialects/oracle/base.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/sqlalchemy/dialects/oracle/base.py b/lib/sqlalchemy/dialects/oracle/base.py index 473e485a41..159d6eeb61 100644 --- a/lib/sqlalchemy/dialects/oracle/base.py +++ b/lib/sqlalchemy/dialects/oracle/base.py @@ -1345,7 +1345,10 @@ class OracleDDLCompiler(compiler.DDLCompiler): table_opts.append("\n COMPRESS") else: table_opts.append("\n COMPRESS FOR %s" % (opts["compress"])) - + if opts["tablespace"]: + table_opts.append( + "\n TABLESPACE %s" % self.preparer.quote(opts["tablespace"]) + ) return "".join(table_opts) def get_identity_options(self, identity_options): @@ -1470,7 +1473,7 @@ class OracleDialect(default.DefaultDialect): construct_arguments = [ ( sa_schema.Table, - {"resolve_synonyms": False, "on_commit": None, "compress": False}, + {"resolve_synonyms": False, "on_commit": None, "compress": False, "tablespace": None}, ), (sa_schema.Index, {"bitmap": False, "compress": False}), (sa_schema.Sequence, {"order": None}), -- 2.47.3