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:
@@ -26,11 +26,7 @@ if TYPE_CHECKING:
|
||||
|
||||
|
||||
class PlaylistBase(SQLModel, UTCBaseModel):
|
||||
id: int | None = Field(
|
||||
default=None,
|
||||
primary_key=True,
|
||||
index=True,
|
||||
)
|
||||
id: int = Field(index=True)
|
||||
owner_id: int = Field(sa_column=Column(BigInteger, ForeignKey("lazer_users.id")))
|
||||
ruleset_id: int = Field(ge=0, le=3)
|
||||
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):
|
||||
db_playlist = await cls.from_hub(playlist, room_id, session)
|
||||
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.refresh(db_playlist)
|
||||
playlist.id = db_playlist.id
|
||||
|
||||
@classmethod
|
||||
async def delete_item(cls, item_id: int, room_id: int, session: AsyncSession):
|
||||
@@ -145,4 +139,4 @@ class PlaylistResp(PlaylistBase):
|
||||
if "beatmap" in include:
|
||||
data["beatmap"] = await BeatmapResp.from_db(playlist.beatmap)
|
||||
resp = cls.model_validate(data)
|
||||
return resp
|
||||
return resp
|
||||
39
migrations/versions/3f890a76f036_add_id_data.py
Normal file
39
migrations/versions/3f890a76f036_add_id_data.py
Normal 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 ###
|
||||
Reference in New Issue
Block a user