Skip to content

Commit e9bfd34

Browse files
luliangcesheinbergonpre-commit-ci[bot]agronholm
authored
WIP: Fix ignoring kwargs and ensure deterministic output (#439) (#440)
* Fix ignoring kwargs and ensure deterministic output (#439) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update src/sqlacodegen/generators.py Co-authored-by: Alex Grönholm <alex.gronholm@nextday.fi> --------- Co-authored-by: Idan Sheinberg <ishinberg0@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>
1 parent bdcc4b8 commit e9bfd34

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

CHANGES.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ Version history
77
connect to the same target table. Regenerating models will break existing code.
88
- Improved relationship naming: one-to-many uses FK column names (e.g.,
99
``simple_items_parent_container``), many-to-many uses junction table names (e.g.,
10-
``students_enrollments``). Use ``--options nofknames`` to revert to old behavior.
10+
``students_enrollments``). Use ``--options nofknames`` to revert to old behavior. (PR by @sheinbergon)
11+
- Fixed ``Index`` kwargs (e.g. ``mysql_length``) being ignored during code generation
12+
(PR by @luliangce)
1113

1214
**4.0.0rc2**
1315

src/sqlacodegen/generators.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,10 @@ def render_table(self, table: Table) -> str:
424424

425425
def render_index(self, index: Index) -> str:
426426
extra_args = [repr(col.name) for col in index.columns]
427-
kwargs = {}
427+
kwargs = {
428+
key: repr(value) if isinstance(value, str) else value
429+
for key, value in sorted(index.kwargs.items(), key=lambda item: item[0])
430+
}
428431
if index.unique:
429432
kwargs["unique"] = True
430433

tests/test_generator_declarative.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,41 @@ class SimpleItems(Base):
7777
)
7878

7979

80+
def test_index_with_kwargs(generator: CodeGenerator) -> None:
81+
simple_items = Table(
82+
"simple_items",
83+
generator.metadata,
84+
Column("id", INTEGER, primary_key=True),
85+
Column("name", VARCHAR),
86+
)
87+
simple_items.indexes.add(
88+
Index("idx_name", simple_items.c.name, postgresql_using="gist", mysql_length=10)
89+
)
90+
91+
validate_code(
92+
generator.generate(),
93+
"""\
94+
from typing import Optional
95+
96+
from sqlalchemy import Index, Integer, String
97+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
98+
99+
class Base(DeclarativeBase):
100+
pass
101+
102+
103+
class SimpleItems(Base):
104+
__tablename__ = 'simple_items'
105+
__table_args__ = (
106+
Index('idx_name', 'name', mysql_length=10, postgresql_using='gist'),
107+
)
108+
109+
id: Mapped[int] = mapped_column(Integer, primary_key=True)
110+
name: Mapped[Optional[str]] = mapped_column(String)
111+
""",
112+
)
113+
114+
80115
def test_constraints(generator: CodeGenerator) -> None:
81116
Table(
82117
"simple_items",
@@ -2382,8 +2417,8 @@ class Base(DeclarativeBase):
23822417
class SpatialTable(Base):
23832418
__tablename__ = 'spatial_table'
23842419
__table_args__ = (
2385-
Index('idx_spatial_table_geog', 'geog'),
2386-
Index('idx_spatial_table_geom', 'geom')
2420+
Index('idx_spatial_table_geog', 'geog', postgresql_using='gist'),
2421+
Index('idx_spatial_table_geom', 'geom', postgresql_using='gist')
23872422
)
23882423
23892424
id: Mapped[int] = mapped_column(Integer, primary_key=True)

0 commit comments

Comments
 (0)