Fixed: 12012: Add Support for `TABLESPACE` Specification in Table Definitions for Oracle
Fixes: #12016
**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)
```
<!-- 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.
**Have a nice day!**
Closes: #12013
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12013
Pull-request-sha:
e542dea6ced269cb188b06cbd04cecf1c400e29a
Change-Id: I4733b466f9486289e13dd7503d18b3b5c866e836