feat: Add unique constraint on sound hash and update related tests
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlmodel import Field, Relationship
|
||||
from sqlmodel import Field, Relationship, UniqueConstraint
|
||||
|
||||
from app.models.base import BaseModel
|
||||
|
||||
@@ -29,6 +29,11 @@ class Sound(BaseModel, table=True):
|
||||
is_music: bool = Field(default=False, nullable=False)
|
||||
is_deletable: bool = Field(default=True, nullable=False)
|
||||
|
||||
# constraints
|
||||
__table_args__ = (
|
||||
UniqueConstraint("hash", name="uq_sound_hash"),
|
||||
)
|
||||
|
||||
# relationships
|
||||
playlist_sounds: list["PlaylistSound"] = Relationship(back_populates="sound")
|
||||
extractions: list["Extraction"] = Relationship(back_populates="sound")
|
||||
|
||||
@@ -116,7 +116,7 @@ class SoundRepository:
|
||||
async def get_popular_sounds(self, limit: int = 10) -> list[Sound]:
|
||||
"""Get the most played sounds."""
|
||||
try:
|
||||
statement = select(Sound).order_by(desc(Sound.play_count)).limit(limit)
|
||||
statement = select(Sound).order_by(Sound.play_count.desc()).limit(limit)
|
||||
result = await self.session.exec(statement)
|
||||
return list(result.all())
|
||||
except Exception:
|
||||
|
||||
Reference in New Issue
Block a user