Skip to content

Commit

Permalink
✨ NEW: Add indexes section
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Dec 1, 2021
1 parent 0bdf385 commit f497b18
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/example/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Address(Base):
__table_args__ = (CheckConstraint("number>0", name="check1"),)
pk = Column(types.Integer, primary_key=True)
number = Column(types.Integer, nullable=False, doc="The number of the address.")
postcode = Column(types.String, nullable=False)
postcode = Column(
types.String, nullable=False, index=True, doc="The postcode of the address."
)
user_id = Column(types.Integer, ForeignKey("dbusers.pk"))
user = orm.relationship("User")
13 changes: 13 additions & 0 deletions sphinx_sqlalchemy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from sqlalchemy.sql.schema import (
CheckConstraint,
ForeignKeyConstraint,
Index,
PrimaryKeyConstraint,
UniqueConstraint,
)
Expand Down Expand Up @@ -137,6 +138,13 @@ def add_content(self, mapper: Mapper, definition: nodes.definition) -> None:
for text in sorted(contraint_to_str(c) for c in constraints):
definition[-1] += nodes.list_item("", nodes.paragraph(text=text))

# table indexes
if mapper.local_table is not None and mapper.local_table.indexes:
definition += nodes.rubric(text="Indexes:")
definition += nodes.bullet_list()
for text in sorted(index_to_str(c) for c in mapper.local_table.indexes):
definition[-1] += nodes.list_item("", nodes.paragraph(text=text))


def contraint_to_str(constraint: Constraint) -> str:
"""Convert a constraint to a string."""
Expand All @@ -153,3 +161,8 @@ def contraint_to_str(constraint: Constraint) -> str:
if isinstance(constraint, CheckConstraint):
return f"CHECK ({constraint.sqltext.text})" # type: ignore
return "UNKNOWN"


def index_to_str(index: Index) -> str:
"""Convert an index to a string."""
return f"{index.name} ({', '.join(c.name for c in index.columns)})"

0 comments on commit f497b18

Please sign in to comment.