Skip to content

Commit

Permalink
feat: Adding constraints for multiple auth methods (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
gromdimon authored Feb 19, 2024
1 parent fa33652 commit a74cb15
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
58 changes: 58 additions & 0 deletions backend/alembic/versions/6f14afa8ea47_update_auth_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Update User and OAuthAccount models
Revision ID: 6f14afa8ea47
Revises: 397feb4e1315
Create Date: 2024-02-19 17:43:45.473226+01:00
"""
import fastapi_users_db_sqlalchemy.generics # noqa
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision = "6f14afa8ea47"
down_revision = "397feb4e1315"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column(
"caseinfo",
"diseases",
existing_type=postgresql.JSONB(astext_type=sa.Text()),
type_=sa.JSON(),
existing_nullable=True,
)
op.alter_column(
"caseinfo",
"hpo_terms",
existing_type=postgresql.JSONB(astext_type=sa.Text()),
type_=sa.JSON(),
existing_nullable=True,
)
op.create_unique_constraint(None, "oauth_account", ["oauth_name", "user_id"])
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, "oauth_account", type_="unique")
op.alter_column(
"caseinfo",
"hpo_terms",
existing_type=sa.JSON(),
type_=postgresql.JSONB(astext_type=sa.Text()),
existing_nullable=True,
)
op.alter_column(
"caseinfo",
"diseases",
existing_type=sa.JSON(),
type_=postgresql.JSONB(astext_type=sa.Text()),
existing_nullable=True,
)
# ### end Alembic commands ###
8 changes: 7 additions & 1 deletion backend/app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
SQLAlchemyBaseOAuthAccountTableUUID,
SQLAlchemyBaseUserTableUUID,
)
from sqlalchemy import BigInteger, String
from sqlalchemy import BigInteger, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.db.base import Base
Expand All @@ -14,6 +14,12 @@


class OAuthAccount(SQLAlchemyBaseOAuthAccountTableUUID, Base):
"""Base OAuth account table definition."""

__tablename__ = "oauth_account"

__table_args__ = (UniqueConstraint("oauth_name", "user_id"),)

if TYPE_CHECKING: # pragma: no cover
access_token: str
refresh_token: Optional[str]
Expand Down
7 changes: 6 additions & 1 deletion backend/app/schemas/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ class UserCreate(schemas.BaseUserCreate):


class UserUpdate(schemas.BaseUserUpdate):
pass
"""Model to use for updateing users.
We expose the public OAuth account information.
"""

oauth_accounts: typing.List[PublicOAuthAccount] = []

0 comments on commit a74cb15

Please sign in to comment.