document + rename stuff

This commit is contained in:
cwilvx
2024-07-07 16:07:27 +03:00
parent 32a2684ea2
commit 2ba5d6c1d7
11 changed files with 72 additions and 72 deletions
+20 -12
View File
@@ -9,14 +9,12 @@ from sqlalchemy import (
from sqlalchemy.engine import Engine
from sqlalchemy import event
from sqlalchemy.orm import (
DeclarativeBase,
MappedAsDataclass,
Session
)
from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass, Session
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()
@@ -25,9 +23,12 @@ def set_sqlite_pragma(dbapi_connection, connection_record):
class DbManager:
""" """
def __init__(self, commit: bool = False):
self.commit = commit
self.conn = DbEngine.engine.connect()
with Session(DbEngine.engine) as session:
session.connection
@@ -42,9 +43,15 @@ class DbManager:
class Base(MappedAsDataclass, DeclarativeBase):
"""
Base class for all database models.
It has methods common to all tables. eg. `insert_one`, `insert_many`, `remove_all`, `remove_one`, `all`, `count`.
"""
@classmethod
def execute(cls, stmt: Any, commit: bool = False):
with DbManager(commit=commit) as conn:
with DbEngine.manager(commit=commit) as conn:
return conn.execute(stmt)
@classmethod
@@ -52,8 +59,7 @@ class Base(MappedAsDataclass, DeclarativeBase):
"""
Inserts multiple items into the database.
"""
with DbManager(commit=True) as conn:
return conn.execute(insert(cls).values(items))
return cls.execute(insert(cls).values(items), commit=True)
@classmethod
def insert_one(cls, item: dict[str, Any]):
@@ -64,12 +70,11 @@ class Base(MappedAsDataclass, DeclarativeBase):
@classmethod
def remove_all(cls):
with DbManager(commit=True) as conn:
conn.execute(delete(cls))
return cls.execute(delete(cls), commit=True)
@classmethod
def remove_one(cls, id: int):
cls.execute(delete(cls).where(cls.id == id), commit=True)
return cls.execute(delete(cls).where(cls.id == id), commit=True)
@classmethod
def all(cls):
@@ -80,5 +85,8 @@ class Base(MappedAsDataclass, DeclarativeBase):
return cls.execute(select(func.count()).select_from(cls)).scalar()
def create_all():
def create_all_tables():
"""
Creates all the tables that build on the Base class.
"""
Base().metadata.create_all(DbEngine.engine)
+26 -1
View File
@@ -1,5 +1,30 @@
from contextlib import contextmanager
from sqlalchemy import Engine
class DbEngine:
engine: Engine = None
"""
The database engine instance.
"""
engine: Engine
@classmethod
@contextmanager
def manager(cls, commit: bool):
"""
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.
If the `commit` parameter is set to `True`, the context manager will commit the transaction when it exits.
"""
try:
conn = cls.engine.connect()
yield conn.execution_options(preserve_rowcount=True)
if commit:
conn.commit()
finally:
conn.close()
+4 -1
View File
@@ -23,6 +23,9 @@ from typing import Any, Iterable, Optional
def create_all():
"""
Create all the tables defined in this file.
NOTE: We need this function because the MasterBase does not collect
the tables defined here (as they are grand-children of the MasterBase)
"""
Base.metadata.create_all(DbEngine.engine)
@@ -317,7 +320,7 @@ class AlbumTable(Base):
# NOTE: The artist dict keys need to in the same order they appear in the db for this to work!
select(AlbumTable).where(AlbumTable.artisthashes.contains(artist))
)
albums[artist] = (albums_to_dataclasses(result.fetchall()))
albums[artist] = albums_to_dataclasses(result.fetchall())
return albums
+2 -2
View File
@@ -90,10 +90,10 @@ class SQLiteManager:
if self.test_db_path:
db_path = self.test_db_path
else:
db_path = settings.Db.get_app_db_path()
db_path = settings.DbPaths.get_app_db_path()
if self.userdata_db:
db_path = settings.Db.get_userdata_db_path()
db_path = settings.DbPaths.get_userdata_db_path()
self.conn = sqlite3.connect(
db_path,