-
Notifications
You must be signed in to change notification settings - Fork 93
Backend: Add public_trips models and migration #7723
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
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6411c4f
Add public trips models and migration
nabramow afe9c86
Add RFC for public trips
nabramow f4be2a6
Merge branch 'develop' into na/backend/public-trips-table
nabramow 47dd442
Merge branch 'develop' into na/backend/public-trips-table
nabramow 2ad2025
Address review comments
nabramow c2da495
Update app/backend/src/couchers/migrations/versions/981fb62b20ce_add_…
nabramow 1dd6263
Address a few missing points
nabramow 73ee179
Remove strong verification requirement from plan
nabramow 02a8408
Merge branch 'develop' into na/backend/public-trips-table
nabramow 84fb95c
Address review comments
nabramow d892ac7
Merge branch 'develop' into na/backend/public-trips-table
nabramow 0e53fd7
Merge branch 'develop' into na/backend/public-trips-table
nabramow ce7fbba
Fix format
nabramow 72cb048
Update app/backend/src/couchers/migrations/versions/981fb62b20ce_add_…
nabramow 3cef163
Update app/backend/src/couchers/migrations/versions/981fb62b20ce_add_…
nabramow 87eb56f
Update app/backend/src/couchers/migrations/versions/981fb62b20ce_add_…
nabramow c1d34cb
Rename migration to ordinal convention (0139)
github-actions[bot] 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
75 changes: 75 additions & 0 deletions
75
app/backend/src/couchers/migrations/versions/981fb62b20ce_add_public_trips.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,75 @@ | ||
| """Add public trips | ||
|
|
||
| Revision ID: 981fb62b20ce | ||
| Revises: e9190b051324 | ||
nabramow marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Create Date: 2026-01-22 12:00:00.000000 | ||
|
|
||
| """ | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "981fb62b20ce" | ||
nabramow marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| down_revision = "0138" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| # Create public_trips table | ||
| op.create_table( | ||
| "public_trips", | ||
| sa.Column("id", sa.BigInteger(), nullable=False), | ||
| sa.Column("user_id", sa.BigInteger(), nullable=False), | ||
| sa.Column("node_id", sa.BigInteger(), nullable=False), | ||
| sa.Column("from_date", sa.Date(), nullable=False), | ||
| sa.Column("to_date", sa.Date(), nullable=False), | ||
| sa.Column("description", sa.String(), nullable=False), | ||
| sa.Column( | ||
| "status", | ||
| sa.Enum("searching_for_host", "closed", name="publictripstatus"), | ||
| nullable=False, | ||
| ), | ||
| sa.Column("created", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False), | ||
| sa.CheckConstraint("from_date <= to_date", name=op.f("ck_public_trips_valid_date_range")), | ||
| sa.ForeignKeyConstraint(["node_id"], ["nodes.id"], name=op.f("fk_public_trips_node_id_nodes")), | ||
| sa.ForeignKeyConstraint(["user_id"], ["users.id"], name=op.f("fk_public_trips_user_id_users")), | ||
| sa.PrimaryKeyConstraint("id", name=op.f("pk_public_trips")), | ||
| ) | ||
| op.create_index(op.f("ix_public_trips_node_id"), "public_trips", ["node_id"], unique=False) | ||
| op.create_index(op.f("ix_public_trips_user_id"), "public_trips", ["user_id"], unique=False) | ||
| op.create_index( | ||
| "ix_public_trips_node_from_date_active", | ||
| "public_trips", | ||
| ["node_id", "from_date"], | ||
| unique=False, | ||
| postgresql_where=sa.text("status = 'searching_for_host'"), | ||
| ) | ||
|
|
||
| # Add public_trip_id to host_requests | ||
| op.add_column("host_requests", sa.Column("public_trip_id", sa.BigInteger(), nullable=True)) | ||
| op.create_index(op.f("ix_host_requests_public_trip_id"), "host_requests", ["public_trip_id"], unique=False) | ||
| op.create_foreign_key( | ||
| op.f("fk_host_requests_public_trip_id_public_trips"), | ||
| "host_requests", | ||
| "public_trips", | ||
| ["public_trip_id"], | ||
| ["id"], | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| # Remove public_trip_id from host_requests | ||
| op.drop_constraint(op.f("fk_host_requests_public_trip_id_public_trips"), "host_requests", type_="foreignkey") | ||
| op.drop_index(op.f("ix_host_requests_public_trip_id"), table_name="host_requests") | ||
| op.drop_column("host_requests", "public_trip_id") | ||
|
|
||
| # Drop public_trips table | ||
| op.drop_index("ix_public_trips_node_from_date_active", table_name="public_trips") | ||
| op.drop_index(op.f("ix_public_trips_user_id"), table_name="public_trips") | ||
| op.drop_index(op.f("ix_public_trips_node_id"), table_name="public_trips") | ||
| op.drop_table("public_trips") | ||
|
|
||
| # Drop enum | ||
| sa.Enum(name="publictripstatus").drop(op.get_bind(), checkfirst=True) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import enum | ||
| from datetime import date, datetime | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from sqlalchemy import BigInteger, CheckConstraint, Date, DateTime, Enum, ForeignKey, Index, String, func | ||
| from sqlalchemy.orm import Mapped, mapped_column, relationship | ||
|
|
||
| from couchers.models.base import Base | ||
|
|
||
| if TYPE_CHECKING: | ||
| from couchers.models import Node, User | ||
| from couchers.models.host_requests import HostRequest | ||
|
|
||
|
|
||
| class PublicTripStatus(enum.Enum): | ||
| searching_for_host = enum.auto() | ||
| closed = enum.auto() | ||
|
|
||
|
|
||
| class PublicTrip(Base, kw_only=True): | ||
| """ | ||
| A public trip posted by a traveler looking for a host in a community. | ||
| """ | ||
|
|
||
| __tablename__ = "public_trips" | ||
|
|
||
| id: Mapped[int] = mapped_column(BigInteger, primary_key=True, init=False) | ||
|
|
||
| # The traveler posting the trip | ||
| user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| # The community/location (city-level node) | ||
| node_id: Mapped[int] = mapped_column(ForeignKey("nodes.id"), index=True) | ||
|
|
||
| # Trip dates | ||
| from_date: Mapped[date] = mapped_column(Date) | ||
| to_date: Mapped[date] = mapped_column(Date) | ||
|
|
||
| # User's message about their trip | ||
| description: Mapped[str] = mapped_column(String) | ||
|
|
||
| # Current status | ||
| status: Mapped[PublicTripStatus] = mapped_column( | ||
| Enum(PublicTripStatus), default=PublicTripStatus.searching_for_host | ||
| ) | ||
|
|
||
| # Timestamps | ||
| created: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), init=False) | ||
|
|
||
| # Relationships | ||
| user: Mapped[User] = relationship(init=False, back_populates="public_trips") | ||
| node: Mapped[Node] = relationship(init=False, back_populates="public_trips") | ||
| host_requests: Mapped[list[HostRequest]] = relationship(init=False, back_populates="public_trip") | ||
|
|
||
| __table_args__ = ( | ||
| # Ensure from_date is not after to_date | ||
| CheckConstraint("from_date <= to_date", name="valid_date_range"), | ||
| # Index for querying active trips in a community | ||
| # Using partial index since we mostly query for active trips | ||
| Index( | ||
| "ix_public_trips_node_from_date_active", | ||
| node_id, | ||
| from_date, | ||
WouldYouKindly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| postgresql_where=status == PublicTripStatus.searching_for_host, | ||
| ), | ||
| ) | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docstring mismatch (LOW): This says
Revises: e9190b051324but the actualdown_revisionon line 14 is"0138". Cosmetic only — Alembic uses the variable — but should be corrected for clarity.