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
|
||||
Reference in New Issue
Block a user