diff --git a/migrations/versions/2023_11_19_2120-55f1606e7086_change_avatar_to_avatar_id.py b/migrations/versions/2023_11_19_2120-55f1606e7086_change_avatar_to_avatar_id.py new file mode 100644 index 00000000..25eb032f --- /dev/null +++ b/migrations/versions/2023_11_19_2120-55f1606e7086_change_avatar_to_avatar_id.py @@ -0,0 +1,36 @@ +"""change avatar to avatar_id + +Revision ID: 55f1606e7086 +Revises: d7454b0101c0 +Create Date: 2023-11-19 21:20:01.224200+00:00 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "55f1606e7086" +down_revision = "d7454b0101c0" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column("profile", sa.Column("avatar_id", sa.UUID(), nullable=True)) + op.create_unique_constraint("profile_avatar_id_key", "profile", ["avatar_id"]) + + op.drop_constraint("profile_avatar_key", "profile", type_="unique") + op.drop_column("profile", "avatar") + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column("profile", sa.Column("avatar", sa.VARCHAR(), autoincrement=False, nullable=True)) + op.create_unique_constraint("profile_avatar_key", "profile", ["avatar"]) + + op.drop_constraint("profile_avatar_id_key", "profile", type_="unique") + op.drop_column("profile", "avatar_id") + # ### end Alembic commands ### diff --git a/src/auth/routes.py b/src/auth/routes.py index 1e296033..e09a31ef 100644 --- a/src/auth/routes.py +++ b/src/auth/routes.py @@ -36,7 +36,7 @@ }, "profile": { "account_id": 42, - "avatar": None, + "avatar_id": None, "description": "Who da heck is John Doe?", "name": "John Doe", }, diff --git a/src/friendship/routes.py b/src/friendship/routes.py index bae7b030..dd69be22 100644 --- a/src/friendship/routes.py +++ b/src/friendship/routes.py @@ -36,7 +36,7 @@ "profile": { "name": "John Doe", "description": "Who da heck is John Doe?", - "avatar": "/files/0b928aaa-521f-47ec-8be5-396650e2a187", + "avatar_id": "0b928aaa-521f-47ec-8be5-396650e2a187", }, }, { @@ -49,7 +49,7 @@ "profile": { "name": "Alan Fresco", "description": "Who da heck is Alan Fresco?", - "avatar": "/files/595f1db3-cdc6-4ef7-bbb4-33c4cedfe172", + "avatar_id": "595f1db3-cdc6-4ef7-bbb4-33c4cedfe172", }, }, ], diff --git a/src/friendship/schemas.py b/src/friendship/schemas.py index ec806f8c..e2faf1d6 100644 --- a/src/friendship/schemas.py +++ b/src/friendship/schemas.py @@ -3,6 +3,7 @@ from __future__ import annotations from datetime import datetime +from uuid import UUID from pydantic import PositiveInt @@ -66,7 +67,7 @@ class FriendAccount(Schema): class FriendProfile(Schema): """Profile info of someone's friend.""" - avatar: profile_fields.Avatar + avatar_id: UUID | None description: profile_fields.Description name: profile_fields.Name diff --git a/src/profile/fields.py b/src/profile/fields.py index 837831ff..02b04091 100644 --- a/src/profile/fields.py +++ b/src/profile/fields.py @@ -5,10 +5,6 @@ from src.shared.fields import StrField -class Avatar(str): - """Avatar value field.""" - - class Description(StrField): """Description value field.""" diff --git a/src/profile/models.py b/src/profile/models.py index cc328ddf..e8d1559a 100644 --- a/src/profile/models.py +++ b/src/profile/models.py @@ -4,8 +4,10 @@ from datetime import datetime from typing import TYPE_CHECKING +from uuid import UUID from sqlalchemy import DateTime, ForeignKey, String +from sqlalchemy.dialects.postgresql import UUID as PG_UUID from sqlalchemy.orm import Mapped, mapped_column from src.shared.database import Base @@ -25,7 +27,7 @@ class Profile(Base): # pylint: disable=too-few-public-methods account_id: Mapped[int] = mapped_column(ForeignKey("account.id"), autoincrement=False, primary_key=True) - avatar: Mapped[str | None] = mapped_column(String, unique=True) + avatar_id: Mapped[UUID | None] = mapped_column(PG_UUID(as_uuid=True), unique=True) description: Mapped[String | None] = mapped_column(String) name: Mapped[str] = mapped_column(String, nullable=False) updated_at: Mapped[datetime] = mapped_column( diff --git a/src/profile/routes.py b/src/profile/routes.py index 82039b3d..6da4775b 100644 --- a/src/profile/routes.py +++ b/src/profile/routes.py @@ -28,7 +28,7 @@ "account_id": 42, "name": "John Doe", "description": "Who da heck is John Doe?", - "avatar": "/files/0b928aaa-521f-47ec-8be5-396650e2a187", + "avatar_id": "0b928aaa-521f-47ec-8be5-396650e2a187", }, }, }, @@ -60,7 +60,7 @@ def get_profile( "account_id": 42, "name": "John Doe", "description": "Who da heck is John Doe?", - "avatar": "/files/0b928aaa-521f-47ec-8be5-396650e2a187", + "avatar_id": "0b928aaa-521f-47ec-8be5-396650e2a187", }, }, }, @@ -81,7 +81,7 @@ def update_profile( example={ "name": "John Doe", "description": "Who da heck is John Doe?", - "avatar": "/files/0b928aaa-521f-47ec-8be5-396650e2a187", + "avatar_id": "0b928aaa-521f-47ec-8be5-396650e2a187", }, ), ], diff --git a/src/profile/schemas.py b/src/profile/schemas.py index 0287c464..e7a74dc0 100644 --- a/src/profile/schemas.py +++ b/src/profile/schemas.py @@ -2,9 +2,11 @@ from __future__ import annotations +from uuid import UUID + from pydantic import PositiveInt -from src.profile.fields import Avatar, Description, Name +from src.profile.fields import Description, Name from src.shared.schemas import Schema @@ -13,7 +15,7 @@ class Profile(Schema): account_id: PositiveInt - avatar: Avatar | None + avatar_id: UUID | None description: Description | None name: Name @@ -26,6 +28,6 @@ class Config: class ProfileUpdate(Schema): """Data for Profile update.""" - avatar: Avatar | None + avatar_id: UUID | None description: Description | None name: Name diff --git a/src/wish/fields.py b/src/wish/fields.py index d26e298d..0a7199f3 100644 --- a/src/wish/fields.py +++ b/src/wish/fields.py @@ -5,10 +5,6 @@ from src.shared.fields import StrField -class Avatar(str): - """Avatar value field.""" - - class Description(StrField): """Description value field.""" diff --git a/src/wish/routes.py b/src/wish/routes.py index b9289ac4..9878c55b 100644 --- a/src/wish/routes.py +++ b/src/wish/routes.py @@ -27,7 +27,7 @@ "example": { "id": 17, "account_id": 42, - "avatar": "/files/0b928aaa-521f-47ec-8be5-396650e2a187", + "avatar_id": "0b928aaa-521f-47ec-8be5-396650e2a187", "title": "Horse", "description": "I'm gonna take my horse to the old town road.", "created_at": "2023-06-17T11:47:02.823Z", @@ -51,7 +51,7 @@ async def create_wish( example={ "title": "Horse", "description": "I'm gonna take my horse to the old town road.", - "avatar": "/files/0b928aaa-521f-47ec-8be5-396650e2a187", + "avatar_id": "0b928aaa-521f-47ec-8be5-396650e2a187", }, ), ], @@ -71,7 +71,7 @@ async def create_wish( "example": { "id": 17, "account_id": 42, - "avatar": "/files/0b928aaa-521f-47ec-8be5-396650e2a187", + "avatar_id": "0b928aaa-521f-47ec-8be5-396650e2a187", "title": "Horse", "description": "I'm gonna take my horse to the old town road.", "created_at": "2023-06-17T11:47:02.823Z", @@ -94,7 +94,7 @@ async def update_wish( schemas.WishUpdate, Body( example={ - "avatar": "/files/0b928aaa-521f-47ec-8be5-396650e2a187", + "avatar_id": "0b928aaa-521f-47ec-8be5-396650e2a187", "title": "Horse", "description": "I'm gonna take my horse to the old town road.", }, @@ -141,7 +141,7 @@ async def delete_wish( { "id": 17, "account_id": 42, - "avatar": "/files/0b928aaa-521f-47ec-8be5-396650e2a187", + "avatar_id": "0b928aaa-521f-47ec-8be5-396650e2a187", "title": "Horse", "description": "I'm gonna take my horse to the old town road.", "created_at": "2023-06-17T11:47:02.823Z", @@ -149,7 +149,7 @@ async def delete_wish( { "id": 21, "account_id": 42, - "avatar": "/files/92f97bc4-c3d3-4980-86c8-0131c1bedffc", + "avatar_id": "92f97bc4-c3d3-4980-86c8-0131c1bedffc", "title": "Sleep", "description": "I need some sleep. Time to put the old horse down.", "created_at": "2023-10-11T18:31:42.715Z", diff --git a/src/wish/schemas.py b/src/wish/schemas.py index 1f596ec8..ff9a9bdf 100644 --- a/src/wish/schemas.py +++ b/src/wish/schemas.py @@ -3,11 +3,12 @@ from __future__ import annotations from datetime import datetime +from uuid import UUID from pydantic import PositiveInt from src.shared.schemas import Schema -from src.wish.fields import Avatar, Description, Title +from src.wish.fields import Description, Title class Wish(Schema): @@ -16,7 +17,7 @@ class Wish(Schema): id: PositiveInt # noqa: A003 account_id: PositiveInt - avatar: Avatar | None + avatar_id: UUID | None created_at: datetime description: Description title: Title @@ -31,7 +32,7 @@ class Wishes(Schema): class NewWish(Schema): """Wish which is going to be created.""" - avatar: Avatar | None + avatar_id: UUID | None description: Description title: Title @@ -39,7 +40,7 @@ class NewWish(Schema): class WishUpdate(Schema): """Update info for wish.""" - avatar: Avatar | None + avatar_id: UUID | None description: Description title: Title diff --git a/tests/test_auth/test_create_account.py b/tests/test_auth/test_create_account.py index 75b64e99..deb2fbd4 100644 --- a/tests/test_auth/test_create_account.py +++ b/tests/test_auth/test_create_account.py @@ -45,7 +45,7 @@ async def test_create_account_returns_201_with_correct_body(f): }, "profile": { "account_id": dirty_equals.IsPositiveInt, - "avatar": None, + "avatar_id": None, "description": "I'm the best guy for your mocks.", "name": "John Doe", }, @@ -90,7 +90,7 @@ async def test_create_account_creates_objects_in_db_correctly(f): assert profiles == [ Profile( account_id=dirty_equals.IsPositiveInt, - avatar=None, + avatar_id=None, description="I'm the best guy for your mocks.", name="John Doe", updated_at=datetime(2023, 10, 30, 20, 0, tzinfo=timezone.utc),