port search to stores

+ fix favorites
This commit is contained in:
cwilvx
2024-07-27 21:44:33 +03:00
parent 5d32536758
commit b0e904c84f
25 changed files with 428 additions and 666 deletions
+3 -34
View File
@@ -7,41 +7,10 @@ from sqlalchemy import (
select,
)
from sqlalchemy.engine import Engine
from sqlalchemy import event
from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass, Session
from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass
from app.db.engine import DbEngine
# Enable foreign key constraints for SQLite
@event.listens_for(Engine, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA foreign_keys=ON")
cursor.close()
class DbManager:
""" """
def __init__(self, commit: bool = False):
self.commit = commit
self.conn = DbEngine.engine.connect()
with Session(DbEngine.engine) as session:
session.connection
def __enter__(self):
return self.conn.execution_options(preserve_rowcount=True)
def __exit__(self, exc_type, exc_val, exc_tb):
if self.commit:
self.conn.commit()
self.conn.close()
class Base(MappedAsDataclass, DeclarativeBase):
"""
Base class for all database models.
@@ -51,8 +20,8 @@ class Base(MappedAsDataclass, DeclarativeBase):
@classmethod
def execute(cls, stmt: Any, commit: bool = False):
with DbEngine.manager(commit=commit) as conn:
return conn.execute(stmt)
with DbEngine.manager(commit=commit) as session:
return session.execute(stmt)
@classmethod
def insert_many(cls, items: list[dict[str, Any]]):
+20 -5
View File
@@ -1,5 +1,18 @@
from contextlib import contextmanager
from sqlalchemy import Engine
import gc
from sqlalchemy import Engine, event
@event.listens_for(Engine, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA journal_mode=WAL")
cursor.execute("PRAGMA synchronous=NORMAL")
cursor.execute("PRAGMA cache_size=10000")
cursor.execute("PRAGMA foreign_keys=ON")
cursor.execute("PRAGMA temp_store=MEMORY")
cursor.execute("PRAGMA mmap_size=30000000000")
cursor.close()
class DbEngine:
@@ -11,20 +24,22 @@ class DbEngine:
@classmethod
@contextmanager
def manager(cls, commit: bool):
def manager(cls, commit: bool = False):
"""
This context manager manages access to the database.
When the context manager is entered, it returns a connection object that can be used to execute SQL statements.
When the context manager is entered, it returns a session object that can be used to execute SQL statements.
If the `commit` parameter is set to `True`, the context manager will commit the transaction when it exits.
"""
conn = cls.engine.connect()
try:
conn = cls.engine.connect()
yield conn.execution_options(preserve_rowcount=True)
if commit:
conn.commit()
except Exception as e:
conn.rollback()
raise e
finally:
conn.close()
+22 -26
View File
@@ -1,6 +1,5 @@
from app.db import (
Base as MasterBase,
DbManager,
)
from app.db.utils import (
album_to_dataclass,
@@ -13,7 +12,7 @@ from app.db.utils import (
from app.models import Album as AlbumModel
from app.utils.remove_duplicates import remove_duplicates
from app.db.engine import DbEngine
from sqlalchemy import JSON, Boolean, Integer, String, delete, select, update
from sqlalchemy import JSON, Integer, String, delete, select, update
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase
@@ -33,7 +32,7 @@ def create_all():
class Base(MasterBase, DeclarativeBase):
@classmethod
def get_all_hashes(cls, create_date: int | None = None):
with DbManager() as conn:
with DbEngine.manager() as conn:
if create_date:
if cls.__tablename__ == "track":
stmt = select(TrackTable.trackhash).where(
@@ -67,7 +66,7 @@ class Base(MasterBase, DeclarativeBase):
hash (str): The hash value.
is_favorite (bool): The value of the 'is_favorite' flag.
"""
with DbManager(commit=True) as conn:
with DbEngine.manager(commit=True) as conn:
if cls.__tablename__ == "track":
stmt = (
update(cls)
@@ -129,7 +128,6 @@ class TrackTable(Base):
title: Mapped[str] = mapped_column(String())
track: Mapped[int] = mapped_column(Integer())
trackhash: Mapped[str] = mapped_column(String(), index=True)
# is_favorite: Mapped[Optional[bool]] = mapped_column(Boolean())
lastplayed: Mapped[int] = mapped_column(Integer(), default=0)
playcount: Mapped[int] = mapped_column(Integer(), default=0)
playduration: Mapped[int] = mapped_column(Integer(), default=0)
@@ -139,13 +137,13 @@ class TrackTable(Base):
@classmethod
def get_all(cls):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(select(cls))
return tracks_to_dataclasses(result.fetchall())
@classmethod
def get_tracks_by_filepaths(cls, filepaths: list[str]):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(
select(TrackTable)
.where(TrackTable.filepath.in_(filepaths))
@@ -155,7 +153,7 @@ class TrackTable(Base):
@classmethod
def get_tracks_by_albumhash(cls, albumhash: str):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(
select(TrackTable).where(TrackTable.albumhash == albumhash)
)
@@ -164,7 +162,7 @@ class TrackTable(Base):
@classmethod
def get_track_by_trackhash(cls, hash: str, filepath: str = ""):
with DbManager() as conn:
with DbEngine.manager() as conn:
if filepath:
result = conn.execute(
select(TrackTable)
@@ -186,7 +184,7 @@ class TrackTable(Base):
@classmethod
def get_tracks_by_artisthash(cls, artisthash: str):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(
select(TrackTable).where(TrackTable.artists.contains(artisthash))
)
@@ -194,7 +192,7 @@ class TrackTable(Base):
@classmethod
def get_tracks_in_path(cls, path: str):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(
select(TrackTable)
.where(TrackTable.filepath.contains(path))
@@ -204,7 +202,7 @@ class TrackTable(Base):
@classmethod
def get_tracks_by_trackhashes(cls, hashes: Iterable[str], limit: int | None = None):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(
select(TrackTable)
.where(TrackTable.trackhash.in_(hashes))
@@ -221,7 +219,7 @@ class TrackTable(Base):
@classmethod
def get_recently_added(cls, start: int, limit: int):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(
select(TrackTable)
.order_by(TrackTable.last_mod.desc())
@@ -243,7 +241,7 @@ class TrackTable(Base):
@classmethod
def remove_tracks_by_filepaths(cls, filepaths: set[str]):
with DbManager(commit=True) as conn:
with DbEngine.manager(commit=True) as conn:
conn.execute(delete(TrackTable).where(TrackTable.filepath.in_(filepaths)))
@classmethod
@@ -270,7 +268,6 @@ class AlbumTable(Base):
og_title: Mapped[str] = mapped_column(String())
title: Mapped[str] = mapped_column(String())
trackcount: Mapped[int] = mapped_column(Integer())
# is_favorite: Mapped[Optional[bool]] = mapped_column(Boolean())
lastplayed: Mapped[int] = mapped_column(Integer(), default=0)
playcount: Mapped[int] = mapped_column(Integer(), default=0)
playduration: Mapped[int] = mapped_column(Integer(), default=0)
@@ -280,14 +277,14 @@ class AlbumTable(Base):
@classmethod
def get_all(cls):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(select(AlbumTable))
all = result.fetchall()
return albums_to_dataclasses(all)
@classmethod
def get_album_by_albumhash(cls, hash: str):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(
select(AlbumTable).where(AlbumTable.albumhash == hash)
)
@@ -298,7 +295,7 @@ class AlbumTable(Base):
@classmethod
def get_albums_by_albumhashes(cls, hashes: Iterable[str], limit: int | None = None):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(
select(AlbumTable).where(AlbumTable.albumhash.in_(hashes)).limit(limit)
)
@@ -312,7 +309,7 @@ class AlbumTable(Base):
@classmethod
def get_albums_by_artisthashes(cls, artisthashes: list[str]):
with DbManager() as conn:
with DbEngine.manager() as conn:
albums: dict[str, list[AlbumModel]] = {}
for artist in artisthashes:
@@ -325,7 +322,7 @@ class AlbumTable(Base):
@classmethod
def get_albums_by_base_title(cls, base_title: str):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(
select(AlbumTable).where(AlbumTable.base_title == base_title)
)
@@ -333,7 +330,7 @@ class AlbumTable(Base):
@classmethod
def get_albums_by_artisthash(cls, artisthash: str):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(
select(AlbumTable).where(AlbumTable.artisthashes.contains(artisthash))
)
@@ -359,7 +356,6 @@ class ArtistTable(Base):
genres: Mapped[str] = mapped_column(JSON())
name: Mapped[str] = mapped_column(String(), index=True)
trackcount: Mapped[int] = mapped_column(Integer())
# is_favorite: Mapped[Optional[bool]] = mapped_column(Boolean())
lastplayed: Mapped[int] = mapped_column(Integer(), default=0)
playcount: Mapped[int] = mapped_column(Integer(), default=0)
playduration: Mapped[int] = mapped_column(Integer(), default=0)
@@ -369,14 +365,14 @@ class ArtistTable(Base):
@classmethod
def get_all(cls):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(select(cls))
all = result.fetchall()
return artists_to_dataclasses(all)
@classmethod
def get_artist_by_hash(cls, artisthash: str):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(
select(ArtistTable).where(ArtistTable.artisthash == artisthash)
)
@@ -384,7 +380,7 @@ class ArtistTable(Base):
@classmethod
def get_artisthashes_not_in(cls, artisthashes: list[str]):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(
select(ArtistTable.artisthash, ArtistTable.name).where(
~ArtistTable.artisthash.in_(artisthashes)
@@ -396,7 +392,7 @@ class ArtistTable(Base):
def get_artists_by_artisthashes(
cls, hashes: Iterable[str], limit: int | None = None
):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(
select(ArtistTable)
.where(ArtistTable.artisthash.in_(hashes))
+5 -3
View File
@@ -1,9 +1,11 @@
from app.db import Base, DbManager
from app.db import Base
from sqlalchemy import Integer, insert, select, update
from sqlalchemy.orm import Mapped, mapped_column
from app.db.engine import DbEngine
class MigrationTable(Base):
__tablename__ = "dbmigration"
@@ -13,7 +15,7 @@ class MigrationTable(Base):
@classmethod
def set_version(cls, version: int):
with DbManager(commit=True) as conn:
with DbEngine.manager(commit=True) as conn:
result = conn.execute(
update(cls).where(cls.id == 1).values(version=version)
)
@@ -23,7 +25,7 @@ class MigrationTable(Base):
@classmethod
def get_version(cls):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(select(cls.version).where(cls.id == 1))
result = result.fetchone()
+44 -63
View File
@@ -18,6 +18,7 @@ from sqlalchemy import (
from sqlalchemy.orm import Mapped, mapped_column
from app.db.engine import DbEngine
from app.db.utils import (
albums_to_dataclasses,
artists_to_dataclasses,
@@ -27,14 +28,13 @@ from app.db.utils import (
plugin_to_dataclasses,
similar_artist_to_dataclass,
similar_artists_to_dataclass,
tracklog_to_dataclass,
tracklog_to_dataclasses,
tracks_to_dataclasses,
user_to_dataclass,
user_to_dataclasses,
)
from app.db import Base, DbManager
from app.db import Base
from app.utils.auth import get_current_userid, hash_password
@@ -77,7 +77,7 @@ class UserTable(Base):
@classmethod
def get_by_id(cls, id: int):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(select(cls).where(cls.id == id))
res = result.fetchone()
@@ -86,7 +86,7 @@ class UserTable(Base):
@classmethod
def get_by_username(cls, username: str):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(select(cls).where(cls.username == username))
res = result.fetchone()
@@ -95,7 +95,7 @@ class UserTable(Base):
@classmethod
def update_one(cls, user: dict[str, Any]):
with DbManager(commit=True) as conn:
with DbEngine.manager(commit=True) as conn:
conn.execute(update(cls).where(cls.id == user["id"]).values(user))
@classmethod
@@ -126,7 +126,7 @@ class SimilarArtistTable(Base):
@classmethod
def get_all(cls):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(select(cls))
return similar_artists_to_dataclass(result.fetchall())
@@ -136,7 +136,7 @@ class SimilarArtistTable(Base):
Check whether an artisthash exists in the database.
"""
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(
select(cls.artisthash).where(cls.artisthash == artisthash)
)
@@ -148,7 +148,7 @@ class SimilarArtistTable(Base):
Get a single artist by hash.
"""
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(select(cls).where(cls.artisthash == artisthash))
result = result.fetchone()
@@ -160,7 +160,7 @@ class FavoritesTable(Base):
__tablename__ = "favorite"
id: Mapped[int] = mapped_column(primary_key=True)
hash: Mapped[str] = mapped_column(String())
hash: Mapped[str] = mapped_column(String(), unique=True)
type: Mapped[str] = mapped_column(String(), index=True)
timestamp: Mapped[int] = mapped_column(Integer(), index=True)
userid: Mapped[int] = mapped_column(
@@ -172,7 +172,7 @@ class FavoritesTable(Base):
@classmethod
def get_all(cls):
with DbManager() as conn:
with DbEngine.manager() as conn:
result = conn.execute(select(cls))
return favorites_to_dataclass(result.fetchall())
@@ -181,12 +181,12 @@ class FavoritesTable(Base):
item["timestamp"] = int(datetime.datetime.now().timestamp())
item["userid"] = get_current_userid()
with DbManager(commit=True) as conn:
with DbEngine.manager(commit=True) as conn:
conn.execute(insert(cls).values(item))
@classmethod
def remove_item(cls, item: dict[str, Any]):
with DbManager(commit=True) as conn:
with DbEngine.manager(commit=True) as conn:
conn.execute(
delete(cls).where(
(cls.hash == item["hash"]) & (cls.type == item["type"])
@@ -199,12 +199,13 @@ class FavoritesTable(Base):
return result.fetchone() is not None
@classmethod
def get_all_of_type(cls, table: Any, field: Any, type: str, start: int, limit: int):
def get_all_of_type(cls, type: str, start: int, limit: int):
result = cls.execute(
select(table)
.select_from(join(table, cls, field == cls.hash))
.where(and_(cls.type == type, cls.userid == get_current_userid()))
.offset(start)
select(cls)
# .select_from(join(table, cls, field == cls.hash))
.where(and_(cls.type == type, cls.userid == get_current_userid())).offset(
start
)
# INFO: If start is 0, fetch all so we can get the total count
.limit(limit if start != 0 else None)
)
@@ -218,30 +219,18 @@ class FavoritesTable(Base):
@classmethod
def get_fav_tracks(cls, start: int, limit: int):
from .libdata import TrackTable
result, total = cls.get_all_of_type(
TrackTable, TrackTable.trackhash, "track", start, limit
)
return tracks_to_dataclasses(result), total
result, total = cls.get_all_of_type("track", start, limit)
return favorites_to_dataclass(result), total
@classmethod
def get_fav_albums(cls, start: int, limit: int):
from .libdata import AlbumTable
result, total = cls.get_all_of_type(
AlbumTable, AlbumTable.albumhash, "album", start, limit
)
return albums_to_dataclasses(result), total
result, total = cls.get_all_of_type("album", start, limit)
return favorites_to_dataclass(result), total
@classmethod
def get_fav_artists(cls, start: int, limit: int):
from .libdata import ArtistTable
result, total = cls.get_all_of_type(
ArtistTable, ArtistTable.artisthash, "artist", start, limit
)
return artists_to_dataclasses(result), total
result, total = cls.get_all_of_type("artist", start, limit)
return favorites_to_dataclass(result), total
class ScrobbleTable(Base):
@@ -265,7 +254,7 @@ class ScrobbleTable(Base):
return cls.insert_one(item)
@classmethod
def get_all(cls, start: int, limit: int):
def get_all(cls, start: int, limit: int | None):
result = cls.execute(
select(cls)
.where(cls.userid == get_current_userid())
@@ -325,7 +314,7 @@ class PlaylistTable(Base):
)
@classmethod
def get_trackhashes(cls, id: int) -> list[str]:
def get_trackhashes(cls, id: int):
result = cls.execute(
select(cls.trackhashes).where(
(cls.id == id) & (cls.userid == get_current_userid())
@@ -388,32 +377,24 @@ class PlaylistTable(Base):
)
# class PlaylistTrackTable(Base):
# __tablename__ = "playlisttrack"
class ArtistData(Base):
__tablename__ = "artistdata"
# id: Mapped[int] = mapped_column(primary_key=True)
# trackhash: Mapped[str] = mapped_column(String(), index=True)
# playlistid: Mapped[int] = mapped_column(
# Integer(), ForeignKey("playlist.id", ondelete="cascade")
# )
# index: Mapped[int] = mapped_column(Integer())
# userid: Mapped[int] = mapped_column(
# Integer(), ForeignKey("user.id", ondelete="cascade")
# )
id: Mapped[int] = mapped_column(primary_key=True)
artisthash: Mapped[str] = mapped_column(String(), index=True)
color: Mapped[str] = mapped_column(String(), nullable=True)
bio: Mapped[str] = mapped_column(String(), nullable=True)
info: Mapped[dict[str, Any]] = mapped_column(JSON(), nullable=True)
extra: Mapped[dict[str, Any]] = mapped_column(
JSON(), nullable=True, default_factory=dict
)
# @classmethod
# def count_by_playlist()
@classmethod
def find_one(cls, artisthash: str):
result = cls.execute(select(cls).where(cls.artisthash == artisthash))
return result.fetchone()
# @classmethod
# def insert_many(cls, playlistid: int, trackhashes: list[str]):
# userid = get_current_userid()
# items = [
# {
# "index": index,
# "userid": userid,
# "trackhash": trackhash,
# "playlistid": playlistid,
# }
# for index, trackhash in enumerate(trackhashes)
# ]
# return cls.execute(insert(cls).values(items), commit=True)
@classmethod
def get_all_colors(cls) -> dict[str, str]:
result = cls.execute(select(cls.artisthash, cls.color))
return dict(result.fetchall())