mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-03 20:13:02 +00:00
remove hardcoded db location
+ move Db engine to module
This commit is contained in:
+3
-17
@@ -1,7 +1,6 @@
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from sqlalchemy import (
|
from sqlalchemy import (
|
||||||
create_engine,
|
|
||||||
delete,
|
delete,
|
||||||
func,
|
func,
|
||||||
insert,
|
insert,
|
||||||
@@ -15,18 +14,7 @@ from sqlalchemy.orm import (
|
|||||||
MappedAsDataclass,
|
MappedAsDataclass,
|
||||||
)
|
)
|
||||||
|
|
||||||
# ============================================================
|
from app.db.engine import DbEngine
|
||||||
# TODO: Make sure the database is created before we run this.
|
|
||||||
fullpath = "/home/cwilvx/temp/swingmusic/swing.db"
|
|
||||||
engine = create_engine(
|
|
||||||
f"sqlite+pysqlite:///{fullpath}",
|
|
||||||
echo=False,
|
|
||||||
max_overflow=0,
|
|
||||||
pool_size=5,
|
|
||||||
)
|
|
||||||
|
|
||||||
# connection = engine.connect()
|
|
||||||
|
|
||||||
|
|
||||||
@event.listens_for(Engine, "connect")
|
@event.listens_for(Engine, "connect")
|
||||||
def set_sqlite_pragma(dbapi_connection, connection_record):
|
def set_sqlite_pragma(dbapi_connection, connection_record):
|
||||||
@@ -38,12 +26,10 @@ def set_sqlite_pragma(dbapi_connection, connection_record):
|
|||||||
class DbManager:
|
class DbManager:
|
||||||
def __init__(self, commit: bool = False):
|
def __init__(self, commit: bool = False):
|
||||||
self.commit = commit
|
self.commit = commit
|
||||||
self.engine = create_engine(f"sqlite+pysqlite:///{fullpath}", echo=True)
|
self.conn = DbEngine.engine.connect()
|
||||||
self.conn = self.engine.connect()
|
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
return self.conn.execution_options(preserve_rowcount=True)
|
return self.conn.execution_options(preserve_rowcount=True)
|
||||||
# return connection
|
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
if self.commit:
|
if self.commit:
|
||||||
@@ -92,4 +78,4 @@ class Base(MappedAsDataclass, DeclarativeBase):
|
|||||||
|
|
||||||
|
|
||||||
def create_all():
|
def create_all():
|
||||||
Base().metadata.create_all(engine)
|
Base().metadata.create_all(DbEngine.engine)
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
from sqlalchemy import Engine
|
||||||
|
|
||||||
|
|
||||||
|
class DbEngine:
|
||||||
|
engine: Engine = None
|
||||||
+2
-3
@@ -12,8 +12,7 @@ from app.db.utils import (
|
|||||||
)
|
)
|
||||||
from app.models import Album as AlbumModel
|
from app.models import Album as AlbumModel
|
||||||
from app.utils.remove_duplicates import remove_duplicates
|
from app.utils.remove_duplicates import remove_duplicates
|
||||||
from app.db import engine
|
from app.db.engine import DbEngine
|
||||||
|
|
||||||
from sqlalchemy import JSON, Boolean, Integer, String, delete, select, update
|
from sqlalchemy import JSON, Boolean, Integer, String, delete, select, update
|
||||||
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase
|
from sqlalchemy.orm import Mapped, mapped_column, DeclarativeBase
|
||||||
|
|
||||||
@@ -25,7 +24,7 @@ def create_all():
|
|||||||
"""
|
"""
|
||||||
Create all the tables defined in this file.
|
Create all the tables defined in this file.
|
||||||
"""
|
"""
|
||||||
Base.metadata.create_all(engine)
|
Base.metadata.create_all(DbEngine.engine)
|
||||||
|
|
||||||
|
|
||||||
class Base(MasterBase, DeclarativeBase):
|
class Base(MasterBase, DeclarativeBase):
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ Module to setup Sqlite databases and tables.
|
|||||||
Applies migrations.
|
Applies migrations.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from sqlalchemy import create_engine
|
||||||
from app.db.userdata import UserTable
|
from app.db.userdata import UserTable
|
||||||
from app.db.sqlite import create_connection, create_tables, queries
|
from app.db.sqlite import create_connection, create_tables, queries
|
||||||
from app.db.sqlite.auth import SQLiteAuthMethods as authdb
|
from app.db.sqlite.auth import SQLiteAuthMethods as authdb
|
||||||
@@ -12,6 +13,8 @@ from app.settings import Db
|
|||||||
from app.db import create_all
|
from app.db import create_all
|
||||||
from app.db.libdata import create_all as create_all_libdata
|
from app.db.libdata import create_all as create_all_libdata
|
||||||
|
|
||||||
|
from app.db.engine import DbEngine
|
||||||
|
|
||||||
|
|
||||||
def run_migrations():
|
def run_migrations():
|
||||||
"""
|
"""
|
||||||
@@ -24,6 +27,13 @@ def setup_sqlite():
|
|||||||
"""
|
"""
|
||||||
Create Sqlite databases and tables.
|
Create Sqlite databases and tables.
|
||||||
"""
|
"""
|
||||||
|
DbEngine.engine = create_engine(
|
||||||
|
f"sqlite+pysqlite:///{Db.get_app_db_path()}",
|
||||||
|
echo=False,
|
||||||
|
max_overflow=0,
|
||||||
|
pool_size=25,
|
||||||
|
)
|
||||||
|
|
||||||
create_all()
|
create_all()
|
||||||
create_all_libdata()
|
create_all_libdata()
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ name = "Swing music player"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
description = ""
|
description = ""
|
||||||
authors = ["geoffrey45 <geoffreymungai45@gmail.com>"]
|
authors = ["geoffrey45 <geoffreymungai45@gmail.com>"]
|
||||||
|
package-mode = false
|
||||||
|
|
||||||
[tool.poetry.dependencies]
|
[tool.poetry.dependencies]
|
||||||
python = ">=3.10,<3.12"
|
python = ">=3.10,<3.12"
|
||||||
|
|||||||
Reference in New Issue
Block a user