-
Notifications
You must be signed in to change notification settings - Fork 3
Pool delivered at #5222
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
Merged
Merged
Pool delivered at #5222
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fc44eb6
Initial commit
islean 0598ea9
Initial commit
islean 92622ae
Merge branch 'master' into pool-delivered-at
islean ba5d0e8
Fix duplicated line
islean 0509150
Add test
islean c7fb917
Add docstring and property
islean ab40669
Fix tests
islean 21d30c5
Fix alembic revision
islean 5fc4cc6
Merge branch 'master' into pool-delivered-at
islean b98e2a9
Fix warnings
islean 254b613
Fix admin view
islean ea0ec98
Modify script now that each pool has a ticket
islean 7411ecd
Wrap type
islean bb99e1f
Fix ticket search
islean c097900
Fix types in tests
islean cad9bf7
Fix admin view
islean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
alembic/versions/2026_07_28_eb2e90a251c5_add_order_fk_to_pool.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| """Add order fk to pool | ||
|
|
||
| Revision ID: eb2e90a251c5 | ||
| Revises: 7e84083f6cb0 | ||
| Create Date: 2026-07-28 11:38:21.383286 | ||
|
|
||
| """ | ||
|
|
||
| import logging | ||
| from datetime import datetime | ||
| from typing import Annotated | ||
|
|
||
| import sqlalchemy as sa | ||
| from sqlalchemy import orm | ||
| from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column | ||
|
|
||
| from alembic import op | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "eb2e90a251c5" | ||
| down_revision = "7e84083f6cb0" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
| PrimaryKeyInt = Annotated[int, mapped_column(primary_key=True)] | ||
| Str32 = Annotated[str, 32] | ||
| Str64 = Annotated[str, 64] | ||
| Text = Annotated[str, None] | ||
|
|
||
|
|
||
| class Base(DeclarativeBase): | ||
| type_annotation_map = { | ||
| Str32: sa.String(32), | ||
| Str64: sa.String(64), | ||
| } | ||
|
|
||
|
|
||
| class Customer(Base): | ||
| __tablename__ = "customer" | ||
| id: Mapped[PrimaryKeyInt] | ||
|
|
||
|
|
||
| class Order(Base): | ||
| """Model for storing orders.""" | ||
|
|
||
| __tablename__ = "order" | ||
|
|
||
| id: Mapped[PrimaryKeyInt] | ||
| customer_id: Mapped[int] = mapped_column(sa.ForeignKey("customer.id")) | ||
| order_date: Mapped[datetime] = mapped_column(default=datetime.now) | ||
| ticket_id: Mapped[int] = mapped_column(unique=True, index=True) | ||
| is_open: Mapped[bool] = mapped_column(default=True) | ||
|
|
||
|
|
||
| class Pool(Base): | ||
| __tablename__ = "pool" | ||
| __table_args__ = (sa.UniqueConstraint("order", "name", name="_order_name_uc"),) | ||
| comment: Mapped[Text | None] | ||
| created_at: Mapped[datetime | None] = mapped_column(default=datetime.now) | ||
| customer_id: Mapped[int] = mapped_column(sa.ForeignKey("customer.id")) | ||
| id: Mapped[PrimaryKeyInt] | ||
| name: Mapped[Str32] | ||
| order: Mapped[Str64] | ||
| order_id: Mapped[int] = mapped_column(sa.ForeignKey("order.id")) | ||
| db_order: Mapped[Order] = orm.relationship(foreign_keys=[order_id]) | ||
| ordered_at: Mapped[datetime] | ||
| received_at: Mapped[datetime | None] | ||
| ticket: Mapped[Str32 | None] | ||
|
|
||
|
|
||
| LOG = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def upgrade(): | ||
| """ | ||
| Adds a relationship between the Pool and the order table and uses the ticket column to link the two, | ||
| before dropping the (now obsolete) ticket column. | ||
| """ | ||
| bind: sa.Connection = op.get_bind() | ||
| session = Session(bind=bind) | ||
| op.add_column( | ||
| table_name="pool", | ||
| column=sa.Column( | ||
| sa.ForeignKey( | ||
| "order.id", | ||
| name="pool_order_fk", | ||
| ), | ||
| name="order_id", | ||
| type_=sa.Integer, | ||
| nullable=True, | ||
| ), | ||
| ) | ||
| for pool in session.query(Pool).all(): | ||
| if pool.ticket: | ||
| order: Order | None = session.query(Order).filter_by(ticket_id=int(pool.ticket)).first() | ||
| if not order: | ||
| LOG.info(f"Creating order with ticket_id {pool.ticket}") | ||
| order = Order( | ||
| customer_id=pool.customer_id, | ||
|
islean marked this conversation as resolved.
|
||
| is_open=False, | ||
| order_date=pool.ordered_at, | ||
| ticket_id=int(pool.ticket), | ||
| ) | ||
| session.add(order) | ||
| pool.db_order = order | ||
| session.add(pool) | ||
| session.commit() | ||
| op.alter_column( | ||
| table_name="pool", column_name="order_id", nullable=False, existing_type=sa.Integer | ||
| ) | ||
| op.drop_column(table_name="pool", column_name="ticket") | ||
|
|
||
|
|
||
| def downgrade(): | ||
| bind: sa.Connection = op.get_bind() | ||
| session = Session(bind=bind) | ||
| op.add_column( | ||
| table_name="pool", column=sa.Column(name="ticket", type_=sa.VARCHAR(32), nullable=True) | ||
| ) | ||
| for pool in session.query(Pool): | ||
| if pool.db_order: | ||
|
islean marked this conversation as resolved.
|
||
| pool.ticket = str(pool.db_order.ticket_id) | ||
| session.add(pool) | ||
| session.commit() | ||
| op.drop_constraint(constraint_name="pool_order_fk", table_name="pool", type_="foreignkey") | ||
| op.drop_column(table_name="pool", column_name="order_id") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.