You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add support for rendering dialect kwargs and info, and introduce keep-dialect-types option (#438)
* [Add support for rendering dialect kwargs and info; introduce keep-dialect-types option
### Summary
- Render `Table`/`Column` dialect-specific kwargs and `info` in
generated code.
- Add `keep-dialect-types` generator option to preserve dialect-specific
column types instead of adapting to generic SQLAlchemy types.
### Motivation
- Some dialect features (e.g., engine/storage params, partitioning,
materialized view metadata) are carried in SQLAlchemy `dialect_kwargs`
or `info` but weren’t emitted by sqlacodegen.
- For custom dialects with their own Column types, the current type
“adaptation” step collapses them into generic SQLAlchemy types, losing
fidelity.
### What’s changed
- Render dialect kwargs and info
- Table (Core): include table-level `dialect_kwargs` and `info` in
`Table(...)` kwargs.
- Column (Core/ORM): include column-level `dialect_kwargs` and `info`
in `Column(...)` / `mapped_column(...)`.
- Declarative: include table-level `dialect_kwargs` and `info` in
`__table_args__` dict.
- New option: keep-dialect-types
- Generator option: `keep_dialect_types`.
- CLI flag: `--options=keep_dialect_types`.
- Behavior: gates only the type adaptation step in
`fix_column_types()`; Boolean/Enum inference and PostgreSQL sequence
handling remain intact.
### Usage examples
- Core (Table)
```python
t_orders = Table(
'orders',
metadata,
Column('id', INTEGER, primary_key=True, starrocks_aggr_type='SUM'),
schema='public',
comment='orders table',
info={'table_kind': 'view'}
)
```
- Declarative (`__table_args__`)
```python
class Orders(Base):
__tablename__ = 'orders'
__table_args__ = (
{
'schema': 'public',
'comment': 'orders table',
'info': {'table_kind': 'view'},
'starrocks_properties': {'replication_num': '1'},
}
)
id: Mapped[INTEGER] = mapped_column(primary_key=True,
starrocks_aggr_type='SUM')
```
- Preserve dialect types
```bash
sqlacodegen postgresql://... --options=keep_dialect_types
```
When the flag is set, dialect-specific types (e.g.,
`starrocks.datatype.INTEGER`) are preserved and imported from their
original module rather than adapted to generic SQLAlchemy types.
### Backward compatibility
- Default behavior is unchanged:
- If `keep_dialect_types` is not set, type adaptation continues as
before.
- Output remains stable except that existing `info`/`dialect_kwargs`
now render when present (additive enhancement).
- No breaking API changes.
Signed-off-by: jaogoy <jaogoy@gmail.com>
* Add support for rendering dialect kwargs and info; introduce option
### Summary
- Render `Table`/`Column` dialect-specific kwargs and `info` in
generated code.
- Add `include-dialect-options` and `keep-dialect-types` option to
render dialect options and preserve dialect-specific
column types instead of adapting to generic SQLAlchemy types.
### Motivation
- Some dialect features (e.g., engine/storage params, partitioning,
materialized view metadata) are carried in SQLAlchemy `dialect_kwargs`
or `info` but weren’t emitted by sqlacodegen.
- For custom dialects with their own Column types, the current type
“adaptation” step collapses them into generic SQLAlchemy types, losing
fidelity.
### What’s changed
- Render dialect kwargs and info
- Table (Core): include table-level `dialect_kwargs` and `info` in
`Table(...)` kwargs.
- Column (Core/ORM): include column-level `dialect_kwargs` and `info`
in `Column(...)` / `mapped_column(...)`.
- Declarative: include table-level `dialect_kwargs` and `info` in
`__table_args__` dict.
- New option: include-dialect-options, keep-dialect-types
- Behavior: gates only the type adaptation step in
`fix_column_types()`; Boolean/Enum inference and PostgreSQL sequence
handling remain intact.
### Usage examples
- Core (Table)
```python
t_orders = Table(
'orders',
metadata,
Column('id', INTEGER, primary_key=True, starrocks_aggr_type='SUM'),
schema='public',
comment='orders table',
info={'table_kind': 'view'}
)
```
- Declarative (`__table_args__`)
```python
class Orders(Base):
__tablename__ = 'orders'
__table_args__ = (
{
'schema': 'public',
'comment': 'orders table',
'info': {'table_kind': 'view'},
'starrocks_properties': {'replication_num': '1'},
}
)
id: Mapped[INTEGER] = mapped_column(primary_key=True,
starrocks_aggr_type='SUM')
```
- Preserve dialect types
```bash
sqlacodegen postgresql://...
--options=include-dialect-options,keep-dialect-types
```
When the `include-dialect-options` flag is set, it will render the
dialect options (e.g. `starrocks_properties`) and `info` dict (which
will store the `table_kind`, to indicate it's a table or a view/mv, and
the `definition` for a view/mv).
When the `keep-dialect-types` flag is set, dialect-specific types (e.g.,
`starrocks.datatype.INTEGER`) are preserved and imported from their
original module rather than adapted to generic SQLAlchemy types.
### Backward compatibility
- Default behavior is unchanged:
- If `include-dialect-options` is not set, type dialect options and info
will not be rendered as before.
- If `keep-dialect-types` is not set, type adaptation continues as
before.
- No breaking API changes.
Signed-off-by: jaogoy <jaogoy@gmail.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* add test cases for dialect options and readme
Signed-off-by: jaogoy <jaogoy@gmail.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix pre-commit format
Signed-off-by: jaogoy <jaogoy@gmail.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix format for readme and change
Signed-off-by: jaogoy <jaogoy@gmail.com>
* fix format for change
Signed-off-by: jaogoy <jaogoy@gmail.com>
* Wrapped the changelog lines
* remove redundent options, and refine format
Signed-off-by: jaogoy <jaogoy@gmail.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* use mock dialect in test only
remove importing StarRocksDialect in test
Signed-off-by: jaogoy <jaogoy@gmail.com>
* change option name to be underscores
about include_dialect_options and keep_dialect_types
Signed-off-by: jaogoy <jaogoy@gmail.com>
* fix option names in README and CHANGES
fix `include_dialect_options` and `keep_dialect_types`
Signed-off-by: jaogoy <jaogoy@gmail.com>
---------
Signed-off-by: jaogoy <jaogoy@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Alex Grönholm <alex.gronholm@nextday.fi>
0 commit comments