-
Notifications
You must be signed in to change notification settings - Fork 279
Add support for rendering dialect kwargs and info, and introduce keep-dialect-types option #438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
…alect-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 <[email protected]>
### 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 <[email protected]>
for more information, see https://pre-commit.ci
|
@jaogoy Thank you for your contribution. Could you please:
Then we can move forward with the code review |
|
@agronholm a drop of %1.6 in coverage should fail this PR. Why could you verify the coveralls configuration? |
Signed-off-by: jaogoy <[email protected]>
for more information, see https://pre-commit.ci
Signed-off-by: jaogoy <[email protected]>
for more information, see https://pre-commit.ci
Signed-off-by: jaogoy <[email protected]>
Signed-off-by: jaogoy <[email protected]>
|
@sheinbergon Please have a review when you are free. |
|
So, |
|
Summary
Table/Columndialect-specific kwargs andinfoin generated code.include-dialect-optionsandkeep-dialect-typesoption to render dialect options and preserve dialect-specific column types instead of adapting to generic SQLAlchemy types.Motivation
dialect_kwargsorinfobut weren’t emitted by sqlacodegen.include-dialect-optionsis a must for any dialect with any dialect options.keep-dialect-types, sqlacodegen will rendersqlatype.Integerfor dialect'sINTEGERif it inherits thesqlatype.Integer(for example). Then, when using Alembic (a Schema Migration Tool), it will compare the table schema in database and the table scheme defined in the metadata python file, which will be probably generated by using sqlacodegen for the initial stage, and modified by users manually to add or change some tables. If users write the metadta script with dialect's types (such asINTEGER), then Alembic will recoganize that there are differenct (one issqlatype.Integer, and the other isdialect.INTEGER), it will generate analter_table(...)operations, what is not expected by users.keep-dialect-types, sqlacodegen will render the dialect's types (such asdialect.INTEGER), then Alembic won't generate unnecessary altering operations.What’s changed
Render dialect kwargs and info
Table (Core): include table-level
dialect_kwargsandinfoinTable(...)kwargs.Column (Core/ORM): include column-level
dialect_kwargsandinfoinColumn(...)/mapped_column(...).Declarative: include table-level
dialect_kwargsandinfoin__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
__table_args__)When the
include-dialect-optionsflag is set, it will render the dialect options (e.g.starrocks_properties) andinfodict (which will store thetable_kind, to indicate it's a table or a view/mv, and thedefinitionfor a view/mv).When the
keep-dialect-typesflag 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
include-dialect-optionsis not set, type dialect options and info will not be rendered as before.keep-dialect-typesis not set, type adaptation continues as before.Changes
Fixes #.
Checklist
If this is a user-facing code change, like a bugfix or a new feature, please ensure that
you've fulfilled the following conditions (where applicable):
tests/) which would fail without your patchCHANGES.rst).If this is a trivial change, like a typo fix or a code reformatting, then you can ignore
these instructions.
Updating the changelog
If there are no entries after the last release, use
**UNRELEASED**as the version.If, say, your patch fixes issue #123, the entry should look like this:
If there's no issue linked, just link to your pull request instead by updating the
changelog after you've created the PR.