Skip to content

Commit 420430d

Browse files
committed
UUID v4 to v7
1 parent 36a7bb0 commit 420430d

File tree

8 files changed

+651
-603
lines changed

8 files changed

+651
-603
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies = [
1414
"uvloop>=0.19.0",
1515
"httptools>=0.6.1",
1616
"uuid>=1.30",
17+
"uuid6>=2024.1.12",
1718
"alembic>=1.13.1",
1819
"asyncpg>=0.29.0",
1920
"SQLAlchemy-Utils>=0.41.1",
@@ -117,4 +118,4 @@ explicit_package_bases = true
117118

118119
[[tool.mypy.overrides]]
119120
module = "src.app.*"
120-
disallow_untyped_defs = true
121+
disallow_untyped_defs = true

src/app/api/v1/posts.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ async def read_posts(
5959
page: int = 1,
6060
items_per_page: int = 10,
6161
) -> dict:
62-
db_user = await crud_users.get(db=db, username=username, is_deleted=False, schema_to_select=UserRead)
62+
db_user = await crud_users.get(
63+
db=db,
64+
username=username,
65+
is_deleted=False,
66+
schema_to_select=UserRead,
67+
return_as_model=True
68+
)
6369
if not db_user:
6470
raise NotFoundException("User not found")
6571

@@ -81,7 +87,13 @@ async def read_posts(
8187
async def read_post(
8288
request: Request, username: str, id: int, db: Annotated[AsyncSession, Depends(async_get_db)]
8389
) -> PostRead:
84-
db_user = await crud_users.get(db=db, username=username, is_deleted=False, schema_to_select=UserRead)
90+
db_user = await crud_users.get(
91+
db=db,
92+
username=username,
93+
is_deleted=False,
94+
schema_to_select=UserRead,
95+
return_as_model=True
96+
)
8597
if db_user is None:
8698
raise NotFoundException("User not found")
8799

@@ -105,7 +117,13 @@ async def patch_post(
105117
current_user: Annotated[dict, Depends(get_current_user)],
106118
db: Annotated[AsyncSession, Depends(async_get_db)],
107119
) -> dict[str, str]:
108-
db_user = await crud_users.get(db=db, username=username, is_deleted=False, schema_to_select=UserRead)
120+
db_user = await crud_users.get(
121+
db=db,
122+
username=username,
123+
is_deleted=False,
124+
schema_to_select=UserRead,
125+
return_as_model=True
126+
)
109127
if db_user is None:
110128
raise NotFoundException("User not found")
111129

@@ -130,7 +148,13 @@ async def erase_post(
130148
current_user: Annotated[dict, Depends(get_current_user)],
131149
db: Annotated[AsyncSession, Depends(async_get_db)],
132150
) -> dict[str, str]:
133-
db_user = await crud_users.get(db=db, username=username, is_deleted=False, schema_to_select=UserRead)
151+
db_user = await crud_users.get(
152+
db=db,
153+
username=username,
154+
is_deleted=False,
155+
schema_to_select=UserRead,
156+
return_as_model=True
157+
)
134158
if db_user is None:
135159
raise NotFoundException("User not found")
136160

@@ -152,7 +176,13 @@ async def erase_post(
152176
async def erase_db_post(
153177
request: Request, username: str, id: int, db: Annotated[AsyncSession, Depends(async_get_db)]
154178
) -> dict[str, str]:
155-
db_user = await crud_users.get(db=db, username=username, is_deleted=False, schema_to_select=UserRead)
179+
db_user = await crud_users.get(
180+
db=db,
181+
username=username,
182+
is_deleted=False,
183+
schema_to_select=UserRead,
184+
return_as_model=True
185+
)
156186
if db_user is None:
157187
raise NotFoundException("User not found")
158188

@@ -161,4 +191,4 @@ async def erase_db_post(
161191
raise NotFoundException("Post not found")
162192

163193
await crud_posts.db_delete(db=db, id=id)
164-
return {"message": "Post deleted from the database"}
194+
return {"message": "Post deleted from the database"}

src/app/api/v1/users.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ async def patch_user(
8585
raise NotFoundException("User not found")
8686

8787
db_user = cast(UserRead, db_user)
88-
if db_user.username != current_user["username"]:
88+
if db_user["username"] != current_user["username"]:
8989
raise ForbiddenException()
9090

91-
if values.username != db_user.username:
91+
if values.username != db_user["username"]:
9292
existing_username = await crud_users.exists(db=db, username=values.username)
9393
if existing_username:
9494
raise DuplicateValueException("Username not available")
9595

96-
if values.email != db_user.email:
96+
if values.email != db_user["email"]:
9797
existing_email = await crud_users.exists(db=db, email=values.email)
9898
if existing_email:
9999
raise DuplicateValueException("Email is already registered")

src/app/core/db/database.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ class Base(DeclarativeBase, MappedAsDataclass):
1111
pass
1212

1313

14-
DATABASE_URI = settings.POSTGRES_URI
14+
DATABASE_URI = settings.POSTGRES_URI
1515
DATABASE_PREFIX = settings.POSTGRES_ASYNC_PREFIX
1616
DATABASE_URL = f"{DATABASE_PREFIX}{DATABASE_URI}"
1717

18-
async_engine = create_async_engine(DATABASE_URL, echo=False, future=True)
18+
SQLITE_DB_URL='sqlite+aiosqlite:///./testing.db'
19+
20+
async_engine = create_async_engine(SQLITE_DB_URL, echo=False, future=True)
1921

2022
local_session = async_sessionmaker(bind=async_engine, class_=AsyncSession, expire_on_commit=False)
2123

src/app/models/post.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Post(Base):
1515
created_by_user_id: Mapped[int] = mapped_column(ForeignKey("user.id"), index=True)
1616
title: Mapped[str] = mapped_column(String(30))
1717
text: Mapped[str] = mapped_column(String(63206))
18-
uuid: Mapped[uuid_pkg.UUID] = mapped_column(default_factory=uuid7, primary_key=True, unique=True)
18+
uuid: Mapped[uuid_pkg.UUID] = mapped_column(default_factory=uuid7, unique=True)
1919
media_url: Mapped[str | None] = mapped_column(String, default=None)
2020

2121
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default_factory=lambda: datetime.now(UTC))

src/app/models/user.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from uuid6 import uuid7 #126
1+
from uuid6 import uuid7
22
from datetime import UTC, datetime
33
import uuid as uuid_pkg
44

5-
from sqlalchemy import DateTime, ForeignKey, String,UUID
5+
from sqlalchemy import DateTime, ForeignKey, String
6+
from sqlalchemy.dialects.postgresql import UUID
67
from sqlalchemy.orm import Mapped, mapped_column
78

89
from ..core.db.database import Base
@@ -11,19 +12,20 @@
1112
class User(Base):
1213
__tablename__ = "user"
1314

14-
id: Mapped[int] = mapped_column("id", autoincrement=True,primary_key=True, init=False)
15-
15+
# Option 1: Use integer ID as primary key (recommended for compatibility)
16+
id: Mapped[int] = mapped_column(autoincrement=True, primary_key=True, init=False)
17+
1618
name: Mapped[str] = mapped_column(String(30))
1719
username: Mapped[str] = mapped_column(String(20), unique=True, index=True)
1820
email: Mapped[str] = mapped_column(String(50), unique=True, index=True)
1921
hashed_password: Mapped[str] = mapped_column(String)
2022

2123
profile_image_url: Mapped[str] = mapped_column(String, default="https://profileimageurl.com")
22-
uuid: Mapped[uuid_pkg.UUID] = mapped_column(UUID(as_uuid=True), default_factory=uuid7, primary_key=True)
24+
uuid: Mapped[uuid_pkg.UUID] = mapped_column(UUID(as_uuid=True), default_factory=uuid7, unique=True)
2325
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default_factory=lambda: datetime.now(UTC))
2426
updated_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None)
2527
deleted_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None)
2628
is_deleted: Mapped[bool] = mapped_column(default=False, index=True)
2729
is_superuser: Mapped[bool] = mapped_column(default=False)
2830

29-
tier_id: Mapped[int | None] = mapped_column(ForeignKey("tier.id"), index=True, default=None, init=False)
31+
tier_id: Mapped[int | None] = mapped_column(ForeignKey("tier.id"), index=True, default=None, init=False)

src/scripts/create_first_superuser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async def create_first_user(session: AsyncSession) -> None:
3737
Column("email", String(50), nullable=False, unique=True, index=True),
3838
Column("hashed_password", String, nullable=False),
3939
Column("profile_image_url", String, default="https://profileimageurl.com"),
40-
Column("uuid", UUID(as_uuid=True), primary_key=True, default=uuid7, unique=True),
40+
Column("uuid", UUID(as_uuid=True), default=uuid7, unique=True),
4141
Column("created_at", DateTime(timezone=True), default=lambda: datetime.now(UTC), nullable=False),
4242
Column("updated_at", DateTime),
4343
Column("deleted_at", DateTime),

0 commit comments

Comments
 (0)