Skip to content

Commit

Permalink
[Issue #3446] Create database table for saved searches (#3461)
Browse files Browse the repository at this point in the history
## Summary
Fixes #3446

### Time to review: 10 mins

## Changes proposed
Add user saved search model
Add DB migration

## Context for reviewers
This lays the groundwork for an API to save user searched.

## Additional information
Generated table:
<img width="1035" alt="Screenshot 2025-01-08 at 11 21 58 AM"
src="https://github.com/user-attachments/assets/7d8c9f6c-2d72-4d2e-909e-f40178886837"
/>
  • Loading branch information
mikehgrantsgov authored Jan 8, 2025
1 parent 6f6fd88 commit 061c558
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""Create saved searches table
Revision ID: cc2912373d5b
Revises: 232a9223ed9b
Create Date: 2025-01-08 16:20:40.898481
"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "cc2912373d5b"
down_revision = "232a9223ed9b"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"user_saved_search",
sa.Column("saved_search_id", sa.UUID(), nullable=False),
sa.Column("user_id", sa.UUID(), nullable=False),
sa.Column("search_query", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
sa.Column("name", sa.Text(), nullable=False),
sa.Column(
"created_at",
sa.TIMESTAMP(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.Column(
"updated_at",
sa.TIMESTAMP(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
sa.ForeignKeyConstraint(
["user_id"], ["api.user.user_id"], name=op.f("user_saved_search_user_id_user_fkey")
),
sa.PrimaryKeyConstraint("saved_search_id", name=op.f("user_saved_search_pkey")),
schema="api",
)
op.create_index(
op.f("user_saved_search_user_id_idx"),
"user_saved_search",
["user_id"],
unique=False,
schema="api",
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(
op.f("user_saved_search_user_id_idx"), table_name="user_saved_search", schema="api"
)
op.drop_table("user_saved_search", schema="api")
# ### end Alembic commands ###
21 changes: 20 additions & 1 deletion api/src/db/models/user_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime

from sqlalchemy import BigInteger, ForeignKey
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import Mapped, mapped_column, relationship

from src.adapters.db.type_decorators.postgres_type_decorators import LookupColumn
Expand All @@ -24,6 +24,10 @@ class User(ApiSchemaTable, TimestampMixin):
cascade="all, delete-orphan",
)

saved_searches: Mapped[list["UserSavedSearch"]] = relationship(
"UserSavedSearch", back_populates="user", uselist=True, cascade="all, delete-orphan"
)


class LinkExternalUser(ApiSchemaTable, TimestampMixin):
__tablename__ = "link_external_user"
Expand Down Expand Up @@ -82,3 +86,18 @@ class UserSavedOpportunity(ApiSchemaTable, TimestampMixin):
opportunity: Mapped[Opportunity] = relationship(
"Opportunity", back_populates="saved_opportunities_by_users"
)


class UserSavedSearch(ApiSchemaTable, TimestampMixin):
"""Table for storing saved search queries for users"""

__tablename__ = "user_saved_search"

saved_search_id: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4)

user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey(User.user_id), index=True)
user: Mapped[User] = relationship(User, back_populates="saved_searches")

search_query: Mapped[dict] = mapped_column(JSONB)

name: Mapped[str]
Binary file modified documentation/api/database/erds/api-schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 061c558

Please sign in to comment.