Refactor Playlist model and add timestamps migration

Simplifies the PlaylistBase id field to a non-optional int and updates database logic to use session.refresh for id assignment. Adds Alembic migration to include created_at and updated_at columns in room_playlists for timestamp tracking.
This commit is contained in:
咕谷酱
2025-08-24 04:03:32 +08:00
committed by MingxuanGame
parent 97dcc86d4d
commit fabc1e9e88
2 changed files with 43 additions and 10 deletions

View File

@@ -26,11 +26,7 @@ if TYPE_CHECKING:
class PlaylistBase(SQLModel, UTCBaseModel): class PlaylistBase(SQLModel, UTCBaseModel):
id: int | None = Field( id: int = Field(index=True)
default=None,
primary_key=True,
index=True,
)
owner_id: int = Field(sa_column=Column(BigInteger, ForeignKey("lazer_users.id"))) owner_id: int = Field(sa_column=Column(BigInteger, ForeignKey("lazer_users.id")))
ruleset_id: int = Field(ge=0, le=3) ruleset_id: int = Field(ge=0, le=3)
expired: bool = Field(default=False) expired: bool = Field(default=False)
@@ -120,11 +116,9 @@ class Playlist(PlaylistBase, table=True):
async def add_to_db(cls, playlist: PlaylistItem, room_id: int, session: AsyncSession): async def add_to_db(cls, playlist: PlaylistItem, room_id: int, session: AsyncSession):
db_playlist = await cls.from_hub(playlist, room_id, session) db_playlist = await cls.from_hub(playlist, room_id, session)
session.add(db_playlist) session.add(db_playlist)
await session.flush()
assert db_playlist.id is not None, "db_playlist.id should be set after flush"
playlist.id = db_playlist.id
await session.commit() await session.commit()
await session.refresh(db_playlist)
playlist.id = db_playlist.id
@classmethod @classmethod
async def delete_item(cls, item_id: int, room_id: int, session: AsyncSession): async def delete_item(cls, item_id: int, room_id: int, session: AsyncSession):
@@ -145,4 +139,4 @@ class PlaylistResp(PlaylistBase):
if "beatmap" in include: if "beatmap" in include:
data["beatmap"] = await BeatmapResp.from_db(playlist.beatmap) data["beatmap"] = await BeatmapResp.from_db(playlist.beatmap)
resp = cls.model_validate(data) resp = cls.model_validate(data)
return resp return resp

View File

@@ -0,0 +1,39 @@
"""add id data
Revision ID: 3f890a76f036
Revises: 8d2af11343b9
Create Date: 2025-08-24 04:00:02.063347
"""
from __future__ import annotations
from collections.abc import Sequence
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = "3f890a76f036"
down_revision: str | Sequence[str] | None = "8d2af11343b9"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
#op.drop_index(op.f("ix_lazer_user_achievements_achievement_id"), table_name="lazer_user_achievements")
#op.drop_index(op.f("uq_user_achievement"), table_name="lazer_user_achievements")
op.add_column("room_playlists", sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=True))
op.add_column("room_playlists", sa.Column("updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=True))
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("room_playlists", "updated_at")
op.drop_column("room_playlists", "created_at")
op.create_index(op.f("uq_user_achievement"), "lazer_user_achievements", ["user_id", "achievement_id"], unique=True)
op.create_index(op.f("ix_lazer_user_achievements_achievement_id"), "lazer_user_achievements", ["achievement_id"], unique=False)
# ### end Alembic commands ###